Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve type definition #76

Closed
435352980 opened this issue Feb 26, 2020 · 7 comments
Closed

improve type definition #76

435352980 opened this issue Feb 26, 2020 · 7 comments
Labels
enhancement New feature or request need more info question Further information is requested

Comments

@435352980
Copy link

435352980 commented Feb 26, 2020

declare module "react-dropdown-select" {
  import React, { KeyboardEvent, CSSProperties, ChangeEvent } from "react";

  export interface SetStateFnArgs<T> {
    dropdown?: boolean;
    values?: T[];
    search?: string;
    selectBounds?: DOMRect | {};
    cursor?: number | null;
    activeCursorItem?: any;
  }

  export interface SelectState<T> {
    dropdown: boolean;
    values: T[];
    search: string;
    selectBounds: object;
    cursor: number;
  }

  export interface SelectMethods<T> {
    removeItem: (
      event: React.MouseEvent<HTMLElement, MouseEvent> | null,
      item: T,
      close: boolean
    ) => void;
    dropDown: (
      action: string,
      event?: React.MouseEvent<HTMLElement, MouseEvent> | null
    ) => void;
    addItem: (item: T) => void;
    setSearch: (event: ChangeEvent<HTMLInputElement>) => void;
    getInputSize: () => number;
    toggleSelectAll: () => void;
    clearAll: () => void;
    selectAll: (items?: T[]) => void;
    searchResults: () => T[];
    getSelectRef: () => HTMLDivElement;
    isSelected: (item: T) => boolean;
    getSelectBounds: () => {} | DOMRect;
    areAllSelected: () => boolean;
    handleKeyDown: (event: KeyboardEvent) => void;
    activeCursorItem: (activeCursorItem: any) => void;
    createNew: (searchText: string) => void;
    sortBy: () => T[];
    safeString: (input: string) => string;
  }

  export interface SelectRenderer<T> {
    props: SelectProps<T>;
    state: SelectState<T>;
    methods: SelectMethods<T>;
  }

  export interface SelectKeyDown<T> {
    event: KeyboardEvent;
    props: SelectProps<T>;
    state: SelectState<T>;
    methods: SelectMethods<T>;
    setState: (
      setter:
        | ((args: SetStateFnArgs<T>) => SetStateFnArgs<T>)
        | SetStateFnArgs<T>
    ) => void;
  }

  export interface SelectItemRenderer<T> {
    item: T;
    itemIndex?: number;
    props: SelectProps<T>;
    state: SelectState<T>;
    methods: SelectMethods<T>;
  }

  export interface SelectProps<T> {
    backspaceDelete?: boolean;
    className?: string;
    addPlaceholder?: string;
    placeholder?: string;
    loading?: boolean;
    style?: CSSProperties;
    values: T[];
    options: T[];
    multi?: boolean;
    disabled?: boolean;
    searchBy?: string;
    sortBy?: string;
    clearable?: boolean;
    searchable?: boolean;
    dropdownHandle?: boolean;
    separator?: boolean;
    keepOpen?: undefined;
    noDataLabel?: string;
    createNewLabel?: string;
    disabledLabel?: string;
    dropdownGap?: number;
    closeOnScroll?: boolean;
    debounceDelay?: number;
    labelField?: string;
    valueField?: string;
    color?: string;
    keepSelectedInList?: boolean;
    closeOnSelect?: boolean;
    clearOnBlur?: boolean;
    clearOnSelect?: boolean;
    dropdownPosition?: "top" | "bottom" | "auto";
    dropdownHeight?: string;
    autoFocus?: boolean;
    portal?: HTMLElement;
    create?: boolean;
    direction?: "ltr" | "rtl";
    name?: string;
    required?: boolean;
    pattern?: string;
    onChange: (value: T[]) => void;
    onDropdownOpen?: () => void;
    onDropdownClose?: () => void;
    onClearAll?: () => void;
    onSelectAll?: () => void;
    onCreateNew?: (item: T) => void;
    searchFn?: ({ props, state, methods }: SelectRenderer<T>) => T[];
    handleKeyDownFn?: ({
      event,
      props,
      state,
      methods,
      setState
    }: SelectKeyDown<T>) => void;
    clearRenderer?: ({
      props,
      state,
      methods
    }: SelectRenderer<T>) => JSX.Element;
    contentRenderer?: ({
      props,
      state,
      methods
    }: SelectRenderer<T>) => JSX.Element;
    dropdownRenderer?: ({
      props,
      state,
      methods
    }: SelectRenderer<T>) => JSX.Element;
    dropdownHandleRenderer?: ({
      props,
      state,
      methods
    }: SelectRenderer<T>) => JSX.Element;
    inputRenderer?: ({
      props,
      state,
      methods,
      inputRef
    }: SelectRenderer<T> & {
      inputRef: React.RefObject<HTMLInputElement>;
    }) => JSX.Element;
    itemRenderer?: ({
      item,
      itemIndex,
      props,
      state,
      methods
    }: SelectItemRenderer<T>) => JSX.Element;
    loadingRenderer?: ({ props }: SelectItemRenderer<T>) => JSX.Element;
    noDataRenderer?: ({
      props,
      state,
      methods
    }: SelectRenderer<T>) => JSX.Element;
    optionRenderer?: ({
      item,
      props,
      state,
      methods
    }: SelectItemRenderer<T>) => JSX.Element;
    separatorRenderer?: ({
      props,
      state,
      methods
    }: SelectRenderer<T>) => JSX.Element;
    additionalProps?: React.HTMLAttributes<HTMLDivElement>;
    wrapperClassName?: string;
  }

  export interface DropDownProps {
    selectBounds: DOMRect;
    dropdownGap: number;
    portal: HTMLElement;
    dropdownHeight: string;
    dropdownPosition: "auto" | "top" | "bottom";
  }

  const Select: <T extends object | string = {}>(
    props: React.PropsWithRef<SelectProps<T>>
  ) => JSX.Element;
  export default Select;
}
@sanusart
Copy link
Owner

Hey, thanks, why not pull request?
Looks nice but a few questions:

  • What is JSX.Element I don't see it imported.
  • Why DropDownProps and DropDownState are there but not used?
  • Was it tested in real editor?

Thank you.

@sanusart sanusart added enhancement New feature or request need more info question Further information is requested labels Feb 26, 2020
@435352980
Copy link
Author

435352980 commented Feb 27, 2020

You are wright :)
I forget to delete DropDownState and some optional declaration, also missing backspaceDelete prop.
I use JSX.Element to suitable some function component width parameter type The Link
9F4F50BE-3CA2-4EB2-B5CF-7DF912D536D5

About DropDownProps

const DropDown = styled.div`
position: absolute;
${({ selectBounds, dropdownGap, dropdownPosition }) =>
dropdownPosition === 'top'
? `bottom: ${selectBounds.height + 2 + dropdownGap}px`
: `top: ${selectBounds.height + 2 + dropdownGap}px`};
${({ selectBounds, dropdownGap, portal }) =>
portal
? `
position: fixed;
top: ${selectBounds.bottom + dropdownGap}px;
left: ${selectBounds.left - 1}px;`
: 'left: -1px;'};
border: 1px solid #ccc;
width: ${({ selectBounds }) => selectBounds.width}px;
padding: 0;
display: flex;
flex-direction: column;
background: #fff;
border-radius: 2px;
box-shadow: 0 0 10px 0 ${() => hexToRGBA('#000000', 0.2)};
max-height: ${({ dropdownHeight }) => dropdownHeight};
overflow: auto;
z-index: 9;
:focus {
outline: none;
}
}
`;

About Test


All types in types.d.ts
2C336A5D-5812-4696-8BE8-D66222C0FC70

@435352980
Copy link
Author

Sorry, I'm a new user of GitHub. I don't know how to use pull request correctly.

@sanusart
Copy link
Owner

sanusart commented Feb 27, 2020 via email

@435352980
Copy link
Author

It's a global type in @types/react
image

@sanusart
Copy link
Owner

Cool, did not knew that. Thanks.

@sanusart
Copy link
Owner

Thank you! Published v4.1.1

Regarding PRs. Every time you clone a repository, make changes and try to commit them. Github will create a fork in your account and will suggest you to open a pool request.

sanusart added a commit that referenced this issue Mar 27, 2020
* origin/master:
  v4.2.0 See changelog: https://github.com/sanusart/react-dropdown-select/blob/master/CHANGELOG.md
  FEATURE (props): add onDropdownCloseRequest prop to notify about clos… (#80)
  v4.1.1 See changelog: https://github.com/sanusart/react-dropdown-select/blob/master/CHANGELOG.md
  FIX (types): types improvement by @435352980 closes #76
  v4.1.0 See changelog: https://github.com/sanusart/react-dropdown-select/blob/master/CHANGELOG.md
  Fixed an issue where top positioning would not work in portal mode (#77) closes #73 Thanks to @akdjr
  FIX (changelog): update changelog
  v4.0.1 See changelog: https://github.com/sanusart/react-dropdown-select/blob/master/CHANGELOG.md
  FIX (props): fix prop type warnings (#75) thanks to @kkkrist
  DOCS (docs): fix docs and update packages
  FIX (ssr) [#66]: incompatible with SSR closes #66 (#72)
  CHORE (cleanup): cleanup unused
  v3.12.0 See changelog: https://github.com/sanusart/react-dropdown-select/blob/master/CHANGELOG.md
  FEATURE (events) [#70]: Remove selected option on key press of backspace button, closes #70 (#71)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request need more info question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants