Skip to content

refactor: improve enum typesafety for filter component#340

Merged
TeeGoood merged 3 commits into
mainfrom
chayapon/feat/filter-components
Mar 12, 2026
Merged

refactor: improve enum typesafety for filter component#340
TeeGoood merged 3 commits into
mainfrom
chayapon/feat/filter-components

Conversation

@TeeGoood
Copy link
Copy Markdown
Collaborator

@TeeGoood TeeGoood commented Mar 10, 2026

Why did you create this PR

What did you do

  • change props type of Filter component to Record instead of flat array.
  • This improve typesafty when using getSelectedValues() for options with enum value

Example usage:

Before change:

need to duplicately define column for each option

const [filterItems, setFilterItems] = useState<FilterItem[]>([
    { column: 'color', value: COLOR.RED, label: 'Red' },
    { column: 'color', value: COLOR.BLUE, label: 'Blue', description: 'A calming blue shade' },
    {
      column: 'animal',
      value: ANIMAL.CAT,
      label: 'Cat',
      description: 'A small domesticated carnivorous mammal',
    },
    {
      column: 'animal',
      value:  ANIMAL.DOG,
      label: 'Dog',
      description: 'A domesticated carnivorous mammal that typically has a long snout',
    },
  ])

...

<Filter items={filterItems} onChange={setFilterItems} />

const selectedValues = getSelectedValues(filterItems, 'color') // get type string[] not COLOR[]

After change:

const [filterOptions, setFilterOptions] = useState({
    color:  [
	  { value: COLOR.RED, label: 'Red },
	  { value: COLOR.BLUE, label: 'Blue', description: 'A calming blue shade' },
	],
    animal: [
	  { value: ANIMAL.CAT, label: 'Cat', description: 'A small domesticated carnivorous mammal' },
	  {
	    value: ANIMAL.DOG,
	    label: 'Dog',
	    description: 'A domesticated carnivorous mammal that typically has a long snout',
	  },
	],
  })

...

<Filter options={filterOptions} onChange={setFilterOptions} />

const selectedValues = getSelectedValues(filterOptions.color) // get tyoe COLOR[]

Screenshots / Recordings

Checklist

  • Self-reviewed your code
  • Wrote coverage tests
  • Added screenshots or recordings if applicable

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Mar 10, 2026

🦋 Changeset detected

Latest commit: 3dd0ab4

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@example/ui-playground Patch
@genseki/ui Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@TeeGoood TeeGoood changed the title refactor: make filter component receive Record prop refactor: improve enum typesafety for filter component Mar 10, 2026
@TeeGoood TeeGoood merged commit aee1dde into main Mar 12, 2026
1 check passed
@TeeGoood TeeGoood deleted the chayapon/feat/filter-components branch March 12, 2026 03:36
@github-actions github-actions Bot mentioned this pull request Mar 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants