diff --git a/source/components/Select/README.md b/source/components/Select/README.md index 5551c41..dcab7c8 100644 --- a/source/components/Select/README.md +++ b/source/components/Select/README.md @@ -6,6 +6,8 @@ Use `Select.createOption` to create options to feed to the `options` prop. Basic usage: ```js console.log('blur', val)} + onChange={(val) => console.log('change', val)} options={[ Select.createOption(1, "label 1"), Select.createOption(2, "label 2"), diff --git a/source/components/Select/SelectContainer.js b/source/components/Select/SelectContainer.js deleted file mode 100644 index f0e9151..0000000 --- a/source/components/Select/SelectContainer.js +++ /dev/null @@ -1,10 +0,0 @@ -import React from 'react'; -import { components } from 'react-select'; - -const SelectContainer = props => ( -
- -
-); - -export default SelectContainer; diff --git a/source/components/Select/createSelectContainer.js b/source/components/Select/createSelectContainer.js new file mode 100644 index 0000000..2c83d4e --- /dev/null +++ b/source/components/Select/createSelectContainer.js @@ -0,0 +1,16 @@ +import React from 'react'; +import { components } from 'react-select'; + +function createSelectContainer(classNamePrefix, customClassName) { + const className = [`${classNamePrefix}__wrapper`, customClassName] + .filter(Boolean) + .join(' '); + + return props => ( +
+ +
+ ); +} + +export default createSelectContainer; diff --git a/source/components/Select/index.js b/source/components/Select/index.js index 3c69e77..cfa14f3 100644 --- a/source/components/Select/index.js +++ b/source/components/Select/index.js @@ -3,7 +3,7 @@ import PropTypes from 'prop-types'; import ReactSelect from 'react-select'; import IndicatorsContainer from './IndicatorsContainer'; -import SelectContainer from './SelectContainer'; +import createSelectContainer from './createSelectContainer'; import { DefaultDropdownIndicator, SearchDropdownIndicator } from './DropdownIndicator'; import LoadingIndicator from './LoadingIndicator'; @@ -97,10 +97,14 @@ export default class Select extends React.Component { super(props); this.selectRef = React.createRef(); + // react-select blur immediately follows change, but the updated value isn't available yet, so we're + // going to manually fire the onBlur handler on a change so that we can reliably pass the value + this.preventBlur = false; } onBlur = () => { - if (!this.props.onBlur || !this.selectRef.current) { + if (this.preventBlur || !this.selectRef.current) { + this.preventBlur = false; return; } @@ -108,8 +112,10 @@ export default class Select extends React.Component { } onChange = (values) => { + this.preventBlur = true; const valuesAsArray = this.props.multi ? values : [values]; callWithValues(this.props.onChange, valuesAsArray, this.props.multi); + callWithValues(this.props.onBlur, valuesAsArray, this.props.multi); } onFocus = () => { @@ -135,12 +141,6 @@ export default class Select extends React.Component { } } - getRootClassName() { - return ['fandom-select', this.props.className] - .filter(Boolean) - .join(' '); - } - getValueFromProps() { const { value, options } = this.props; @@ -169,13 +169,16 @@ export default class Select extends React.Component { } render() { + const className = 'fandom-select'; + return ( ); diff --git a/source/components/Select/index.spec.js b/source/components/Select/index.spec.js index 189bf40..499ffd5 100644 --- a/source/components/Select/index.spec.js +++ b/source/components/Select/index.spec.js @@ -203,3 +203,8 @@ test('onTextInputChange tests', () => { input.props.onChange({ currentTarget: { value: '😀' } }); expect(onTextInputChangeSpy.withArgs('😀').calledOnce).toBe(true); }); + +test('custom className is at root level', () => { + const wrapper = mount(