From edbbc0dd567edfd88d90b98c51dbbfa10919a215 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martynas=20=C5=BDilinskas?= Date: Tue, 1 Aug 2017 00:54:59 +0300 Subject: [PATCH] [react-select] Fixed and updated (#18287) * Added tslint. Fixed some dtslint errors. * Changed from empty style object to React.CSSProperties. * Changed from any to dictionary props. * Created prop handlers types. * Fixed props return types and Handlers return types. * Added options. Added missing focus method in ReactSelect. * Rewritten tests. --- types/react-select/index.d.ts | 165 +++++---- types/react-select/react-select-tests.tsx | 431 +++++++--------------- types/react-select/tslint.json | 1 + 3 files changed, 231 insertions(+), 366 deletions(-) create mode 100644 types/react-select/tslint.json diff --git a/types/react-select/index.d.ts b/types/react-select/index.d.ts index fd07b4545c..24ef712a05 100644 --- a/types/react-select/index.d.ts +++ b/types/react-select/index.d.ts @@ -1,6 +1,12 @@ // Type definitions for react-select 1.0 // Project: https://github.com/JedWatson/react-select -// Definitions by: ESQUIBET Hugo , Gilad Gray , Izaak Baker , Tadas Dailyda , Mark Vujevits , Mike Deverell +// Definitions by: ESQUIBET Hugo +// Gilad Gray +// Izaak Baker +// Tadas Dailyda +// Mark Vujevits +// Mike Deverell +// MartynasZilinskas // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.3 @@ -9,21 +15,61 @@ import * as React from 'react'; export = ReactSelectClass; declare namespace ReactSelectClass { - export interface AutocompleteResult { - /** the search-results to be displayed */ - options: Option[], - /** Should be set to true, if and only if a longer query with the same prefix + // Other components + class Creatable extends React.Component { } + class Async extends React.Component { } + class AsyncCreatable extends React.Component { } + + // Handlers + type FocusOptionHandler = (option: Option) => void; + type SelectValueHandler = (option: Option) => void; + type ArrowRendererHandler = (props: ArrowRendererProps) => JSX.Element; + type FilterOptionHandler = (option: Option, filter: string) => Option; + type FilterOptionsHandler = (options: Options, filter: string, currentValues: Options) => Options; + type InputRendererHandler = (props: { [key: string]: any }) => JSX.Element; + type MenuRendererHandler = (props: MenuRendererProps) => JSX.Element; + type OnCloseHandler = () => void; + type OnInputChangeHandler = (inputValue: string) => void; + type OnInputKeyDownHandler = React.KeyboardEventHandler; + type OnMenuScrollToBottomHandler = () => void; + type OnOpenHandler = () => void; + type OnFocusHandler = React.FocusEventHandler; + type OnBlurHandler = React.FocusEventHandler; + type OptionRendererHandler = (option: Option) => JSX.Element; + type ValueRendererHandler = (option: Option) => JSX.Element; + type OnValueClickHandler = (value: string, event: React.MouseEvent) => void; + type IsOptionUniqueHandler = (arg: { option: Option, options: Options, labelKey: string, valueKey: string }) => boolean; + type IsValidNewOptionHandler = (arg: { label: string }) => boolean; + type NewOptionCreatorHandler = (arg: { label: string, labelKey: string, valueKey: string }) => Option; + type PromptTextCreatorHandler = (filterText: string) => string; + type ShouldKeyDownEventCreateNewOptionHandler = (arg: { keyCode: number }) => boolean; + + type OnChangeSingleHandler = OnChangeHandler>; + type OnChangeMultipleHandler = OnChangeHandler>; + type OnChangeHandler = (newValue: TOption | null) => void; + + type LoadOptionsHandler = LoadOptionsAsyncHandler | LoadOptionsLegacyHandler; + type LoadOptionsAsyncHandler = (input: string) => Promise; + type LoadOptionsLegacyHandler = (input: string, callback: (err: any, result: AutocompleteResult) => void) => void; + + interface AutocompleteResult { + /** The search-results to be displayed */ + options: Options; + /** + * Should be set to true, if and only if a longer query with the same prefix * would return a subset of the results * If set to true, more specific queries will not be sent to the server. - **/ + */ complete: boolean; } - export interface Option { + type Options = Array>; + + interface Option { /** Text for rendering */ label?: string; /** Value for searching */ - value?: string | number | boolean; + value?: TValue; /** * Allow this option to be cleared * @default true @@ -36,7 +82,9 @@ declare namespace ReactSelectClass { disabled?: boolean; } - export interface MenuRendererProps { + type OptionValues = string | number | boolean; + + interface MenuRendererProps { /** * The currently focused option; should be visible in the menu by default. * default {} @@ -46,7 +94,7 @@ declare namespace ReactSelectClass { /** * Callback to focus a new option; receives the option as a parameter. */ - focusOption: (option: Option) => void; + focusOption: FocusOptionHandler; /** * Option labels are accessible with this string key. @@ -56,27 +104,27 @@ declare namespace ReactSelectClass { /** * Ordered array of options to render. */ - options: Option[]; + options: Options; /** * Callback to select a new option; receives the option as a parameter. */ - selectValue: (option: Option) => void; + selectValue: SelectValueHandler; /** * Array of currently selected options. */ - valueArray: Option[]; + valueArray: Options; } - export interface ArrowRendererProps { + interface ArrowRendererProps { /** * Arrow mouse down event handler. */ - onMouseDown: React.MouseEventHandler<{}>; + onMouseDown: React.MouseEventHandler; } - export interface ReactSelectProps extends React.Props { + interface ReactSelectProps extends React.Props { /** * text to display when `allowCreate` is true. * @default 'Add "{label}"?' @@ -86,7 +134,7 @@ declare namespace ReactSelectClass { * renders a custom drop-down arrow to be shown in the right-hand side of the select. * @default undefined */ - arrowRenderer?: (props: ArrowRendererProps) => React.ReactElement; + arrowRenderer?: ArrowRendererHandler; /** * blurs the input element after a selection has been made. Handy for lowering the keyboard on mobile devices. * @default false @@ -149,11 +197,11 @@ declare namespace ReactSelectClass { /** * method to filter a single option */ - filterOption?: (option: Option, filter: string) => Option; + filterOption?: FilterOptionHandler; /** * method to filter the options array */ - filterOptions?: (options: Array