Skip to content
This repository was archived by the owner on Aug 5, 2026. It is now read-only.

[CLOV-64] BpkStarRating Migrate JS to TypeScript - #4523

Merged
Vincent Liu (xiaogliu) merged 7 commits into
mainfrom
xiaogliu/star-rating-to-ts
Jun 8, 2026
Merged

Vincent Liu (xiaogliu) merged 7 commits into
mainfrom
xiaogliu/star-rating-to-ts

Conversation

@xiaogliu

Copy link
Copy Markdown
Contributor

Summary

  • Convert the bpk-component-star-rating package from JavaScript/Flow to TypeScript
  • Rename source/test files (.js -> .ts/.tsx) and snapshot files (*-test.js.snap -> *-test.tsx.snap) so Jest still finds them under the new test filenames
  • Replace propTypes + Flow Props with typed Props exports for BpkStar, BpkStarRating, BpkInteractiveStar, BpkInteractiveStarRating, re-exported from index.ts as BpkStarProps, BpkStarRatingProps, BpkInteractiveStarProps, BpkInteractiveStarRatingProps
  • Convert STAR_TYPES to as const and derive a StarType union
  • Migrate withInteractiveStarRatingState to a generic class component (P extends object) with a public static displayName: string field
  • Drop the now-unused @ts-expect-error suppressions on the index/component imports in the stories file
  • No business-logic, JSX, or rendering changes - existing snapshots remain valid

Test plan

  • npm run jest -- packages/backpack-web/src/bpk-component-star-rating - 7 suites, 42 tests, 30 snapshots all pass
  • npx tsc --noEmit - no errors in bpk-component-star-rating
  • npx eslint packages/backpack-web/src/bpk-component-star-rating - no errors, no warnings

🤖 Generated with Claude Code

Convert the bpk-component-star-rating package from JavaScript/Flow to
TypeScript. Replace prop-types and Flow types with typed Props exports
and rename all source/test/snapshot files (.js -> .ts/.tsx). No
business-logic, JSX, or rendering changes - existing snapshots remain
valid.

- Rename index.js -> index.ts and re-export per-component *Props types
- Rename BpkStar/BpkStarRating/BpkInteractiveStar/BpkInteractiveStarRating
  to .tsx with typed Props exports; keep className?: string | null and
  destructuring defaults in place of defaultProps
- Convert STAR_TYPES to as const and derive a StarType union
- Migrate withInteractiveStarRatingState to .tsx with a generic class
  component and a public static displayName field
- Rename themeAttributes/accessibility/themeAttributes-test files
- Rename snapshot files to *-test.tsx.snap so Jest finds them under the
  new test filenames
- Drop the now-unused @ts-expect-error suppressions on the index/component
  imports in the stories file

Co-Authored-By: Claude Opus 4.7 <[email protected]>
Copilot AI review requested due to automatic review settings May 21, 2026 06:29
@xiaogliu Vincent Liu (xiaogliu) added the minor Non breaking change label May 21, 2026
@skyscanner-backpack-bot

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@skyscanner-backpack-bot

Copy link
Copy Markdown
Contributor

After typing the star-rating package, the @ts-expect-error suppressions
on its imports in BpkRating.stories and BpkCardV2.stories became unused
directives, breaking `tsc --noEmit`. Remove them.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
@skyscanner-backpack-bot

Copy link
Copy Markdown
Contributor

@skyscanner-backpack-bot

skyscanner-backpack-bot Bot commented Jun 4, 2026 •

Copy link
Copy Markdown
Contributor
Warnings
⚠️

Package source files (e.g. packages/package-name/src/Component.js) were updated, but snapshots weren't. Have you checked that the tests still pass?

Browser support

If this is a visual change, make sure you've tested it in multiple browsers.

Generated by 🚫 dangerJS against cd48530

@skyscanner-backpack-bot

Copy link
Copy Markdown
Contributor

) => {
if (event) {
event.persist();
(event as SyntheticEvent).persist();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: event.persist() was needed in React 16 to opt out of event pooling, but React 17 removed synthetic event pooling entirely — it's been a no-op ever since.

The as SyntheticEvent cast is only here to make TS accept .persist() on a MouseEvent. Both the cast and the call can go:

onRatingSelect = (
  rating: number,
  event?: MouseEvent<HTMLButtonElement> | null,
) => {
  const callback = () => {
    if (this.props.onRatingSelect) {
      this.props.onRatingSelect(rating, event);
    }
  };

  this.setState(() => ({ rating }), callback);
};

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 83a76582b — dropped both the cast and the persist call. Thanks for the catch.

return (
<InteractiveStarRating
{...this.props}
{...(this.props as P)}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: worth destructuring out the HOC-owned prop before spreading, so it's clear only P props are passed down to InteractiveStarRating:

render() {
  const { onRatingSelect, ...rest } = this.props;
  return (
    <InteractiveStarRating
      {...(rest as P)}
      rating={this.state.rating}
      hoverRating={this.state.hoverRating}
      onRatingHover={this.onRatingHover}
      onMouseLeave={this.onMouseLeave}
      onRatingSelect={this.onRatingSelect}
    />
  );
}

as P is still needed (TS can't infer Omit<P & WithInteractiveStarRatingStateProps, 'onRatingSelect'> = P), but the intent is more explicit.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 83a76582b — destructured onRatingSelect out of this.props before spreading.

@Faye-Xiao

Copy link
Copy Markdown
Contributor

Hi Vincent Liu (@xiaogliu), we're moving away from snapshot tests — they tend to pass silently after an accidental --updateSnapshot and don't communicate intent. Since this is a JS→TS migration with no behaviour changes, the snapshots are still valid, but it's a good opportunity to replace them with explicit assertions.

For example, in BpkStar-test.tsx:

// instead of
expect(asFragment()).toMatchSnapshot();

// prefer

const { getByRole } = render(<BpkStar type={STAR_TYPES.FULL} />);
expect(getByRole('img', { hidden: true })).toHaveClass('bpk-star--filled');
30 occurrences across 5 test files — not asking to do all of them in this PR, but worth a follow-up ticket if not already tracked.

return (
// $FlowFixMe[cannot-spread-inexact] - inexact rest. See decisions/flowfixme.md
<span className={[containerClassNames, halfIconClassNames]} {...rest}>
<span className={[containerClassNames, halfIconClassNames] as unknown as string} {...rest}>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Vincent Liu (@xiaogliu) , could you improve this code as it's also a type question?

as unknown as string is hiding a real type error — React's className expects a string, not an array. The cast makes TS happy but at runtime this becomes bpk-star__container bpk-star__container--half-star,bpk-star bpk-star--half bpk-star--filled (comma-separated), which would break the styling.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch — fixed in 83a76582b by switching to .join(' '). Worth flagging that this comma-separated class string was actually a pre-existing bug from the JS code (the original snapshot had bpk-icon--rtl-support,bpk-star ... too); my as unknown as string cast was just hiding it. The half-star snapshots are updated to the corrected, space-separated form.

| typeof ROUNDING_TYPES.down
| typeof ROUNDING_TYPES.up
| typeof ROUNDING_TYPES.nearest,
type RatingLabel = string | ((rating: number, maxRating: number) => string);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small question on the typing here: Flow previously allowed the callback to return mixed, while this TS migration narrows it to string.

Given the value is used as aria-label, string seems semantically correct and probably better than preserving the overly-wide Flow type. Could we just confirm this is an intentional tightening rather than an accidental change from the migration? If yes, I think this is fine as-is.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, intentional. The return value is only ever used as the aria-label on the rendered <div role="img">, so string is the right type. The Flow mixed was wider than the actual contract.


type NativeDivProps = HTMLAttributes<HTMLDivElement>;

export type Props = Omit<NativeDivProps, 'className' | 'aria-label'> & {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 Question: are we intentionally expanding the public TS surface to include native div/button props here?

Flow previously described only the component-specific props, while the new TS Props extends HTMLAttributes<HTMLDivElement> / ButtonHTMLAttributes<HTMLButtonElement>. Since these Props are now exported from the package, consumers can rely on native props such as role, tabIndex, onClick, aria-*, etc. That may be fine if Backpack’s convention is to type all pass-through props this way, but it would make this a deliberate API contract rather than just an implementation detail.

Could we confirm this matches the TS migration pattern for Backpack components?

@xiaogliu Vincent Liu (xiaogliu) Jun 5, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, intentional — native pass-through props (data-testid, role, aria-*, etc.) flow through ...rest onto the underlying DOM element, so the type should reflect that. Happy to discussion more as a separate convention discussion.

- BpkStar: fix half-star className being a comma-separated string at
  runtime. Pre-existing bug from JS code that the TS migration's
  `as unknown as string` cast was hiding. Use `.join(' ')` so the half
  star gets a valid space-separated class list. Updates 4 half-star
  snapshots accordingly.
- withInteractiveStarRatingState: drop the `event.persist()` call and
  its `as SyntheticEvent` cast — `persist()` has been a no-op since
  React 17.
- withInteractiveStarRatingState: destructure HOC-owned `onRatingSelect`
  out of `this.props` before spreading, so only `P` props are forwarded
  to the wrapped component.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
@skyscanner-backpack-bot

Copy link
Copy Markdown
Contributor

@xiaogliu

Vincent Liu (xiaogliu) commented Jun 5, 2026 •

Copy link
Copy Markdown
Contributor Author

Agreed on the direction — explicit assertions are clearer about intent. I'd prefer to keep this PR scoped to the JS→TS rename (no behaviour change) and tackle the snapshot rewrite as a follow-up because more files need to update for BpkStarRating. I created another PR to track the 30 occurrences across the 5 test files. #4670

Faye (Faye-Xiao) and others added 2 commits June 5, 2026 18:41
The 30 toMatchSnapshot() calls across the 5 test files in
bpk-component-star-rating verified rendered HTML by full-DOM diff,
which obscures intent and tends to pass silently after an accidental
--updateSnapshot. Replace them with assertions that name the behaviour
each test is checking:

- BpkStar: assert presence/absence of star modifier classes per type
  and size, plus className passthrough.
- BpkInteractiveStar: assert the role=button, aria-label, aria-pressed,
  and the inner star fill state per type/selected combo.
- BpkStarRating: assert the role=img, aria-label, and the count of
  full/half/empty stars per rating, including maxRating and rounding
  behaviour.
- BpkInteractiveStarRating: assert the rendered button count, fill
  count per rating/maxRating/hoverRating, and that onRatingHover /
  onRatingSelect get called with the correct star number.
- withInteractiveStarRatingState: assert the initial state, that
  click selects (aria-pressed=true) and that hover only previews
  (aria-pressed stays false).

Also delete the now-orphaned __snapshots__/*.snap files. No production
code changes; jest now reports 0 snapshots for this package.

Co-authored-by: Vincent Liu <[email protected]>
Co-authored-by: Claude Opus 4.7 <[email protected]>
@skyscanner-backpack-bot

Copy link
Copy Markdown
Contributor

@Faye-Xiao Faye (Faye-Xiao) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@xiaogliu
Vincent Liu (xiaogliu) merged commit 7b532da into main Jun 8, 2026
15 checks passed
@xiaogliu
Vincent Liu (xiaogliu) deleted the xiaogliu/star-rating-to-ts branch June 8, 2026 02:20
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

minor Non breaking change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants