react-select: update props to match Select and Async source files (#9127)

* Add an export for Option so we can use it directly

* Update tests to import the newly exported Option

* Add missing props from latest `react-select` 1.0.0-beta12

* Add menuRenderer function and interface for its named props

* This argument should be optional.

* `tabSelectsValue` should be a boolean, not a string

* Update tests with new props

* Export menu renderer props so devs can write typesafe `menuRenderer` functions

* Make sure to check exports in tests, since it's likely users won't be using <reference... >
This commit is contained in:
Derrick Liu
2016-05-06 03:17:45 +09:00
committed by Masahiro Wakame
parent f86e99d05c
commit cc14a78043
2 changed files with 167 additions and 37 deletions
+34 -1
View File
@@ -5,16 +5,25 @@
import * as React from "react";
import * as ReactDOM from "react-dom";
import Select, { Option } from "react-select";
import Select, { Option, MenuRendererProps } from "react-select";
class SelectTest extends React.Component<React.Props<{}>, {}> {
render() {
const options: Option[] = [{ label: "Foo", value: "bar" }];
const onChange = (value: any) => console.log(value);
const renderMenu = ({
focusedOption,
focusOption,
labelKey,
options,
selectValue,
valueArray
}: MenuRendererProps) => { return <div></div> };
const onOpen = () => { return; };
const onClose = () => { return; };
const optionRenderer = (option: Option) => <span>{option.label}</span>
return <div>
<Select
name="test-select"
@@ -22,17 +31,34 @@ class SelectTest extends React.Component<React.Props<{}>, {}> {
key="1"
options={options}
optionRenderer={optionRenderer}
allowCreate={true}
autofocus={true}
autosize={true}
clearable={true}
escapeClearsValue={true}
ignoreAccents={true}
joinValues={false}
matchPos={"any"}
matchProp={"any"}
menuContainerStyle={{}}
menuRenderer={renderMenu}
menuStyle={{}}
multi={true}
onMenuScrollToBottom={() => {}}
onValueClick={onChange}
onOpen={onOpen}
onClose={onClose}
openAfterFocus={false}
optionComponent={<div></div>}
required={false}
resetValue={"resetValue"}
scrollMenuIntoView={false}
valueKey="github"
labelKey="name"
onChange={onChange}
simpleValue
value={options}
valueComponent={<span></span>}
/>
</div>;
}
@@ -63,6 +89,13 @@ class SelectAsyncTest extends React.Component<React.Props<{}>, {}> {
simpleValue
value={options}
loadOptions={getOptions}
cache={{}}
ignoreAccents={false}
ignoreCase={{}}
isLoading={false}
minimumInput={5}
searchPromptText={"search..."}
searchingText={"searching..."}
/>
</div>;
}
+133 -36
View File
@@ -19,17 +19,45 @@ declare namespace ReactSelect {
clearableValue?: boolean;
}
interface MenuRendererProps {
/**
* The currently focused option; should be visible in the menu by default.
* default {}
*/
focusedOption: Option;
/**
* Callback to focus a new option; receives the option as a parameter.
*/
focusOption: (option: Option) => void;
/**
* Option labels are accessible with this string key.
*/
labelKey: string;
/**
* Ordered array of options to render.
*/
options: Option[];
/**
* Callback to select a new option; receives the option as a parameter.
*/
selectValue: (option: Option) => void;
/**
* Array of currently selected options.
*/
valueArray: Option[];
}
interface ReactSelectProps extends __React.Props<ReactSelect> {
/**
* text to display when `allowCreate` is true.
* @default 'Add "{label}"?'
*/
addLabelText?: string;
/**
* blurs the input element after a selection has been made. Handy for lowering the keyboard on mobile devices.
* @default false
*/
autoBlur?: boolean;
/**
* allow new options to be created in multi mode (displays an "Add <option> ?" item
* when a value not already in the options array is entered)
@@ -37,29 +65,28 @@ declare namespace ReactSelect {
*/
allowCreate?: boolean;
/**
* whether to auto-load the default async options set
* @default true
* blurs the input element after a selection has been made. Handy for lowering the keyboard on mobile devices.
* @default false
*/
autoload?: boolean;
autoBlur?: boolean;
/**
* autofocus the component on mount
* @default false
*/
autofocus?: boolean;
/**
* If enabled, the input will expand as the length of its value increases
*/
autosize?: boolean;
/**
* whether pressing backspace removes the last item when there is no input value
* @default true
*/
backspaceRemoves?: boolean;
/**
* enables the options cache for `asyncOptions`
* @default true
*/
cacheAsyncResults?: boolean;
/**
* CSS className for the outer element
*/
className?: string;
/**
* whether it is possible to reset value. if enabled, an X button will appear at the right side.
* @default true
*/
clearable?: boolean;
/**
* title for the "clear" control when `multi` is true
* @default "Clear all"
@@ -70,6 +97,11 @@ declare namespace ReactSelect {
* @default "Clear value"
*/
clearValueText?: string;
/**
* whether it is possible to reset value. if enabled, an X button will appear at the right side.
* @default true
*/
clearable?: boolean;
/**
* delimiter to use to join multiple values
* @default ","
@@ -80,6 +112,11 @@ declare namespace ReactSelect {
* @default false
*/
disabled?: boolean;
/**
* whether escape clears the value when the menu is closed
* @default true
*/
escapeClearsValue?: boolean;
/**
* method to filter a single option
*/
@@ -88,6 +125,11 @@ declare namespace ReactSelect {
* method to filter the options array
*/
filterOptions?: (options: Array<Option>, filter: string, currentValues: (string | number)[]) => Array<Option>;
/**
* whether to strip diacritics when filtering
* @default true
*/
ignoreAccents?: boolean;
/**
* whether to perform case-insensitive filtering
* @default true
@@ -104,15 +146,16 @@ declare namespace ReactSelect {
* @default false
*/
isLoading?: boolean;
/**
* (legacy mode) joins multiple values into a single form field with the delimiter
* @default false
*/
joinValues?: boolean;
/**
* the option property to use for the label
* @default "label"
*/
labelKey?: string;
/**
* function that calls a callback with the options
*/
loadOptions?: (input: string, callback: (options: Option[]) => any) => any;
/**
* (any, start) match the start or entire string when filtering
* @default "any"
@@ -128,6 +171,18 @@ declare namespace ReactSelect {
* @default 0
*/
menuBuffer?: number;
/**
* optional style to apply to the menu container
*/
menuContainerStyle?: {}
/**
* renders a custom menu with options
*/
menuRenderer?: (props: MenuRendererProps) => __React.ReactElement<any>;
/**
* optional style to apply to the menu
*/
menuStyle?: {}
/**
* multi-value input
* @default false
@@ -147,19 +202,52 @@ declare namespace ReactSelect {
* @default "No results found"
*/
noResultsText?: string;
/**
* onBlur handler: function (event) {}
*/
onBlur?: __React.FocusEventHandler;
/**
* whether to clear input on blur or not
* @default true
*/
onBlurResetsInput?: boolean;
/**
* onChange handler: function (newValue) {}
*/
onChange?: (newValue: Option | Option[]) => void;
/**
* fires when the menu is closed
*/
onClose?: () => void;
/**
* onFocus handler: function (event) {}
*/
onFocus?: __React.FocusEventHandler;
/**
* onInputChange handler: function (inputValue) {}
*/
onInputChange?: (inputValue: string) => void;
/**
* fires when the menu is scrolled to the bottom; can be used to paginate options
*/
onMenuScrollToBottom?: () => void;
/**
* fires when the menu is opened
*/
onOpen?: () => void;
/**
* @deprecated use onValueClick isntead
*/
onOptionLabelClick?: (value: string, event: Event) => void;
/**
* boolean to enable opening dropdown when focused
* @default false
*/
openAfterFocus?: boolean;
/**
* option component to render in dropdown
*/
optionComponent?: __React.ReactElement<any>;
/**
* function which returns a custom way to render the options in the menu
*/
@@ -174,21 +262,29 @@ declare namespace ReactSelect {
* @default "Select..."
*/
placeholder?: string | __React.ReactElement<any>;
/**
* applies HTML5 required attribute when needed
* @default false
*/
required?: boolean;
/**
* value to use when you clear the control
*/
resetValue?: any;
/**
* whether the viewport will shift to display the entire menu when engaged
* @default true
*/
scrollMenuIntoView?: boolean;
/**
* whether to enable searching feature or not
* @default true;
*/
searchable?: boolean;
/**
* message to display whilst options are loading via asyncOptions, or when `isLoading` is true
* @default "Searching..."
* whether to select the currently focused value when the [tab] key is pressed
*/
searchingText?: string;
/**
* label to prompt for search input
* @default "Type to search"
*/
searchPromptText?: string;
tabSelectsValue?: boolean;
/**
* initial field value
*/
@@ -216,7 +312,7 @@ declare namespace ReactSelect {
/**
* value component to render
*/
valueComponent?: Function;
valueComponent?: __React.ReactElement<any>;
/**
* optional style to apply to the component wrapper
@@ -226,7 +322,7 @@ declare namespace ReactSelect {
/**
* onClick handler for value labels: function (value, event) {}
*/
onValueClick?: Function;
onValueClick?: (value: string, event: Event) => void;
/**
* pass the value to onChange as a simple value (legacy pre 1.0 mode), defaults to false
@@ -238,7 +334,7 @@ declare namespace ReactSelect {
/**
* object to use to cache results; can be null to disable cache
*/
cache?: any;
cache?: Object | boolean;
/**
* whether to strip diacritics when filtering (shared with Select)
@@ -303,7 +399,8 @@ declare namespace ReactSelect {
declare module "react-select" {
const select: ReactSelect.ReactSelectClass;
interface Option extends ReactSelect.Option {}
interface MenuRendererProps extends ReactSelect.MenuRendererProps {}
export default select;
export { Option };
export { MenuRendererProps, Option };
}