buildo / react-components

Collection of general React components used in buildo projects.
http://react-components.buildo.io/
MIT License
157 stars 21 forks source link

Dropdown doesn't type-check with some valid prop combinations #1327

Closed giogonzo closed 5 years ago

giogonzo commented 5 years ago

Project card

There are probably multiple issues: one is for sure that we are not currently exporting the correctly "defaulted" props. For this, an initial solution could be:

type DefaultProps = {
  delimiter: NonNullable<SelectNS.Props["delimiter"]>;
  size: "medium" | "small";
  isSearchable: NonNullable<SelectNS.Props["isSearchable"]>;
  menuPlacement: NonNullable<SelectNS.Props["menuPlacement"]>;
  allowCreate: false
};

type InternalProps<OptionType> = ObjectOmit<
    SelectNS.Props<OptionType>,
    "isMulti" | "isClearable" | "onChange" | "value"
  > &
    DefaultProps & {
      flat?: boolean;
      innerRef?: (ref: Select<OptionType> | null) => void;
    } & (
      | {
          type: "multi" | "multi-clearable";
          value: OptionType[];
          onChange: (value: OptionType[]) => void;
        }
      | {
          type: "single";
          value: OptionType | null;
          onChange: (value: OptionType) => void;
        }
      | {
          type: "single-clearable";
          value: OptionType | null;
          onChange: (value: OptionType | null) => void;
        }) &
    (
      | ({
          allowCreate: true;
          isSearchable?: never;
        } & CreatableProps<OptionType>)
      | {
          allowCreate?: never;
        });

export class Dropdown<OptionType> extends React.Component<
  InternalProps<OptionType>
> { ... }

export namespace Dropdown {
  export type Props<T> = ObjectOmit<InternalProps<T>, keyof DefaultProps> & Partial<DefaultProps>
}

But this in turn is causing issues (e.g. in DropdownField)