Upgrade Guide
Summary
- Convert to TypeScript (#4489) - TypeScript types now come packaged with
react-selectso you no longer need to have@types/react-selectinstalled; we no longer include Flow types - Drop IE11 support (#4625, #4720, #4634) - this allows us to make changes to our CSS that we've wanted to make for a long time as well as remove unnecessary JS solutions (those changes are noted below)
- Use
forwardReffor all wrapped components (#4489) - this means that if you were accessing anything on theSelectinstance using aref, therefwill now reference the internalSelectdirectly (see below for how to upgrade) - Replace HOCs with hooks (#4489) - if you were using our HOCs to create custom
Selects (i.e.,makeCreatableSelect,mangeState,makeAsyncSelect) these have now been replaced by hooks (i.e.,useCreatable,useStateManager,useAsync) - Remove dependency on AutosizeInput (#4625) - our new solution uses CSS grid which IE11 does not fully support; also
.prefix__inputnow targets the input and NOT the container - Improve screen reader experience (#4676) - this isn't a breaking change in the API but it does change the screen reader announcements
- Use CSS grid for single value layout (#4720) - this also isn't a breaking change in the API but is it a change in the styles since it switches to using CSS grid (not fully supported by IE11) for single-value
Selects - Remove
readonlyattribute on ourDummyInput(#4634) - this results in better accessibility but usescaret-colorwhich is not available on IE11
Details
Convert to TypeScript
We've rewritten react-select in TypeScript which means you can remove any dependencies on @types/react-select. If you were using the Flow types than look into contributing types for v5 to flow-typed.
Here are the most notable changes when replacing @types/react-select with our packaged types:
| @types/react-select | react-select | Notes |
|---|---|---|
OptinTypeBase |
no replacement | Options can be any type (if using getOptionValue and getOptionLabel) so there's no longer a base type for options |
OptionsType |
Options |
|
GroupTypeBase |
GroupBase |
|
GroupedOptionsType |
no replacement | This is equivalent to ReadonlyArray<Group> |
ValueType |
OnChangeValue |
|
InputActionTypes |
InputAction |
|
NamedProps |
Props |
|
Select (the ref type) |
SelectInstance |
See "Use forwardRef for all wrapped components" for more details |
AsyncSelect (the ref type) |
SelectInstance |
See "Use forwardRef for all wrapped components" for more details |
CreatableSelect (the ref type) |
SelectInstance |
See "Use forwardRef for all wrapped components" for more details |
AsyncCreatableSelect (the ref type) |
SelectInstance |
See "Use forwardRef for all wrapped components" for more details |
If you were previously importing a type from the src directory when using @types/react-select:
import { ... } from 'react-select/src/...';
These should now be imported from the dist/declarations/src directory:
import { ... } from 'react-select/dist/declarations/src/...';
We export any types from the main entry point that we think might be useful to the user. If you are using a type that is not exported from the main entry point please open a PR or issue so that we can add it.
If you are using custom props for the Select component you can use module augmentation to add them to the Select prop types:
declare module 'react-select/dist/declarations/src/Select' {
export interface Props<
Option,
IsMulti extends boolean,
Group extends GroupBase<Option>
> {
myCustomProp: string;
}
}Drop IE11 support
This allows us to use modern CSS in order to improve the quality of react-select and remove excessive JavaScript code to work around not having the ability to use modern CSS. If you need IE11 support either:
- Stay on v4.
- Attempt to use our Styles API and/or Components API to override styles and/or components that don't support IE11. The only change that might be hard to override is "Remove
readonlyattribute on ourDummyInput" since you can't override theDummyInputcomponent with the Styles or Components API. - If there are simple changes that make
react-selectcompatible with IE11, open a PR. We are unlikely to provide official support for IE11 since supporting IE11 makes maintainingreact-selecta lot more difficult and holds us back from changes we want to make, but we also are glad to accept simple changes if they make your life easier.
Use forwardRef for all wrapped components
NOTE: Accessing any of the internals of the Select component using refs is not part of our public API. The internals of the Select component can change at any time (including in minor and patch releases). The only methods on the Select component that are part of the public API are the focus() and blur() methods.
react-select exports five components: BaseSelect, CreatableSelect, Select (the default export), AsyncSelect, AsyncCreatableSelect. The last four components are wrappers around the BaseSelect. Previously the ref for each of these components was pointing to itself, but now we use forwardRef which means that the refs for all five components now point to BaseSelect.
The focus() and blur() methods are untouched by this change. However if you were accessing the internals of the BaseSelect component, you will need to update how you were accessing the BaseSelect. Here is how to update access to the BaseSelect component:
| Component | v4 | v5 |
|---|---|---|
BaseSelect |
ref |
ref |
CreatableSelect |
ref.select.select |
ref |
Select |
ref.select |
ref |
AsyncSelect |
ref.select.select |
ref |
AsyncCreatableSelect |
ref.select.select.select |
ref |
Replace HOCs with hooks
The primary reason for this change is that hooks combined with generic components are easier to type in TypeScript than HOCs combined with generic components. These HOCs/hooks are considered advanced usage.
If you were using the HOCs, it shouldn't be too hard to replace them with its corresponding hook (i.e., useStateManager, useCreatable, or useAsync). As an example, here is how the state managed Select (the default export) used to be constructed with the mangeState HOC:
const Select = manageState(SelectBase);With hooks it is now constructed like this:
const Select = (props) => {
const baseSelectProps = useStateManager(props);
return <SelectBase {...baseSelectProps} />;
}Consult the source code for how the other components are constructed.
Remove dependency on AutosizeInput
This is an exciting change because we get to drop our dependency on react-input-autosize. We now use a pure CSS solution to auto-size the input, however this means that we have to drop support for IE11 since IE11 does not fully support CSS grid. As part of this refactor the .prefix__input CSS class now targets the input itself and NOT the container. See #4625 for more details.
Improve screen reader experience
The following improvements have been made for screen reader users:
- NVDA now announces the context text when initially focused
- Selected option(s) (single and multi) are now announced when initially focused
- VoiceOver now announces the context text when re-focusing
- The clear action is now announced
- Placeholder text is now announced
- Mobile VoiceOver is now able to remove selected multi options
Also we've added the role of combobox and the required ARIA attributes to the Input and DummyInput components to allow JAWS support and a better screen reader experience overall. See #4695 for more details.
Use CSS grid for single value layout
Previously the absolution positioning of both the value and the placeholder pulled them out of the flow, and thus the component itself collapsed down when used as a flex child. To solve this we are now using CSS grid for the single value layout.
Remove readonly attribute on our DummyInput
Previously we added the readonly attribute to the DummyInput (when isSearchable is set to false) in order to hide the flashing cursor and prevent devices from showing a virtual keyboard. However the readonly attribute causes the DummyInput to be removed from the tab order in iOS Safari. In order to solve this we're replacing the readonly attribute with setting the caret-color CSS prop (which IE11 does not support) to transparent and setting the inputMode attribute on the <input> to none.
Changelog
Major Changes
-
ef87c3ac #4683 Thanks @Methuselah96! - React-Select has been converted from Flow to TypeScript.
Other changes for v5 include usage of
forwardRef, new hooks forstateManager,asyncandcreatablecomponents, and more reliable filtering implementaion with new options in the creatable variant.
Patch Changes
-
10225290 #4720 - Updated the layout for the singleValue input/placeholder/container so that it works better when used in flex layouts.
-
53f1972b #4731 Thanks @JedWatson! - MultiValue key now includes a hyphen between the value and the index to prevent edge cases where you could get a duplicate key error
-
b41f4ceb #4704 Thanks @Rall3n! - Fix findDOMNode deprecation by adding refs to transition components
-
4b028829 #4634 - The readonly attribute has been removed from the DummyInput to improve accessibility
-
7fcec537 #4697 - Add the role of combobox and the required ARIA attributes to the Input and DummyInput components to allow JAWS support and a better screen reader experience overall.
-
ca2c0e5b #4756 Thanks @fdcds! - Add
optionfield to type ofCreateOptionActionMeta -
9e82aadc #4676 - The following improvements have been made for screen reader users:
- NVDA now announces the context text when initially focused
- Selected option/s (single and multi) are now announced when initially focused
- VoiceOver now announces the context text when re-focusing
- The clear action is now announced
- Placeholder text is now announced
- Mobile VoiceOver is now able to remove selected multi options
-
638f5455 #4702 Thanks @Methuselah96! - The Option generic is no longer required to extend the OptionBase type
-
23cea0b5 #4782 Thanks @Methuselah96! - Fix type of loadingMessage prop to allow it to return any ReactNode
