design-react-kit icon indicating copy to clipboard operation
design-react-kit copied to clipboard

[FEATURE] Improve ergonomic of Select types

Open dej611 opened this issue 2 years ago • 1 comments

Is your feature request related to a problem? Please describe. Declare the types of a Select component is quite tedius, a semplification of the process might be useful.

For instance if the user wants to wrap the Select component with a custom component (say to add a label node next to it), extends the current types with the new label: string can be very hard

type SelectStringOptionShape = {
  label: string;
  value: string;
}
type SelectComponentProps = SelectProps<SelectStringOptionShape, false, GroupTypeBase<SelectStringOptionShape>>

export function SelectComponent({label, ...props}: SelectComponentProps & {label: string}) {
  return (
    <div className="bootstrap-select-wrapper">
      <label>{label}</label>
      <Select
        {...props}
      />
    </div>
  );
}

Note that the GroupTypeBase is not directly exported by the kit, so the user has to add the "@types/react-select": "^4.0.15" dependency and be careful about the version. It makes sense, to me, to export the type directly by the kit.

Also SelectStringOptionShape can be useful as quick ready to use type (maybe use a better naming), so it could be exported by the kit as well.

dej611 avatar Mar 20 '22 11:03 dej611

Another use case of non-string values:

type SelectStringOptionShape<T extends unknown = string> = {
  label: string;
  value: T;
}
type SelectComponentProps<T extends unknown = string> = SelectProps<SelectStringOptionShape<T>, false, GroupTypeBase<SelectStringOptionShape<T>>>

export function SelectComponent<T extends unknown = string>({label, ...props}: SelectComponentProps<T> & {label: string}) {
  return (
    <div className="bootstrap-select-wrapper">
      <label>{label}</label>
      <Select
        {...props}
      />
    </div>
  );
}

dej611 avatar Mar 20 '22 11:03 dej611

Fixed via #879

dej611 avatar Oct 26 '22 07:10 dej611