shoreline icon indicating copy to clipboard operation
shoreline copied to clipboard

fix(filter): setValue prop type

Open viniciuslagedo opened this issue 6 months ago • 1 comments

Summary

Resolves #1504

Examples

We have made a significant improvement in terms of consistency for those using the component. The types can be assumed based on props if you are using a multi- or single-value filter. So, if you pass a callback using an array of strings, it infers the value prop and the default value with the same type. Besides that, it is now possible to use custom handles on the setValue prop.

type Props = {
  onFilterChange: (value: string[]) => void
  filterValue: string[]
  defaultFilterValue: string[]
}

const MyFilter = (props: Props) => {
  const { onFilterChange, defaultFilterValue, filterValue } = props

  return (
    <Filter
        value={filterValue} // Based on setValue prop it infers the string[] type here
        setValue={onFilterChange} // No more type errors here
        label="Multiple status"
        defaultValue={defaultFilterValue} // Based on setValue prop it infers the string[] type here
      >
      <FilterItem value="Stable">Stable</FilterItem>
      <FilterItem value="Experimental">Experimental</FilterItem>
      <FilterItem value="Deprecated">Deprecated</FilterItem>
    </Filter>
  )
}

viniciuslagedo avatar Aug 26 '24 18:08 viniciuslagedo