feat(netlify-cms-widget-select): add support for multiple selection (#1901)
This commit is contained in:
committed by
Shawn Erquhart
parent
944fe1b370
commit
88bf287221
@ -23,6 +23,8 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"cross-env": "^5.2.0",
|
||||
"jest-dom": "^2.1.1",
|
||||
"react-testing-library": "^5.3.0",
|
||||
"webpack": "^4.16.1",
|
||||
"webpack-cli": "^3.1.0"
|
||||
},
|
||||
|
@ -1,20 +1,20 @@
|
||||
import React from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
import ImmutablePropTypes from 'react-immutable-proptypes';
|
||||
import { Map } from 'immutable';
|
||||
import { Map, List, fromJS } from 'immutable';
|
||||
import { find } from 'lodash';
|
||||
import Select from 'react-select';
|
||||
import { colors } from 'netlify-cms-ui-default';
|
||||
|
||||
const styles = {
|
||||
control: provided => ({
|
||||
...provided,
|
||||
control: styles => ({
|
||||
...styles,
|
||||
border: 0,
|
||||
boxShadow: 'none',
|
||||
padding: '9px 0 9px 12px',
|
||||
}),
|
||||
option: (provided, state) => ({
|
||||
...provided,
|
||||
option: (styles, state) => ({
|
||||
...styles,
|
||||
backgroundColor: state.isSelected
|
||||
? `${colors.active}`
|
||||
: state.isFocused
|
||||
@ -22,12 +22,44 @@ const styles = {
|
||||
: 'transparent',
|
||||
paddingLeft: '22px',
|
||||
}),
|
||||
menu: provided => ({ ...provided, right: 0 }),
|
||||
container: provided => ({ ...provided, padding: '0 !important' }),
|
||||
indicatorSeparator: () => ({ display: 'none' }),
|
||||
dropdownIndicator: provided => ({ ...provided, color: `${colors.controlLabel}` }),
|
||||
menu: styles => ({ ...styles, right: 0, zIndex: 2 }),
|
||||
container: styles => ({ ...styles, padding: '0 !important' }),
|
||||
indicatorSeparator: (styles, state) =>
|
||||
state.hasValue && state.selectProps.isClearable
|
||||
? { ...styles, backgroundColor: `${colors.textFieldBorder}` }
|
||||
: { display: 'none' },
|
||||
dropdownIndicator: styles => ({ ...styles, color: `${colors.controlLabel}` }),
|
||||
clearIndicator: styles => ({ ...styles, color: `${colors.controlLabel}` }),
|
||||
multiValue: styles => ({
|
||||
...styles,
|
||||
backgroundColor: colors.background,
|
||||
}),
|
||||
multiValueLabel: styles => ({
|
||||
...styles,
|
||||
color: colors.textLead,
|
||||
fontWeight: 500,
|
||||
}),
|
||||
multiValueRemove: styles => ({
|
||||
...styles,
|
||||
color: colors.controlLabel,
|
||||
':hover': {
|
||||
color: colors.errorText,
|
||||
backgroundColor: colors.errorBackground,
|
||||
},
|
||||
}),
|
||||
};
|
||||
|
||||
function optionToString(option) {
|
||||
return option && option.value ? option.value : '';
|
||||
}
|
||||
|
||||
function convertToOption(raw) {
|
||||
if (typeof raw === 'string') {
|
||||
return { label: raw, value: raw };
|
||||
}
|
||||
return Map.isMap(raw) ? raw.toJS() : raw;
|
||||
}
|
||||
|
||||
export default class SelectControl extends React.Component {
|
||||
static propTypes = {
|
||||
onChange: PropTypes.func.isRequired,
|
||||
@ -49,33 +81,48 @@ export default class SelectControl extends React.Component {
|
||||
}),
|
||||
};
|
||||
|
||||
static defaultProps = {
|
||||
value: '',
|
||||
handleChange = selectedOption => {
|
||||
const { onChange } = this.props;
|
||||
|
||||
if (Array.isArray(selectedOption)) {
|
||||
onChange(fromJS(selectedOption.map(optionToString)));
|
||||
} else {
|
||||
onChange(optionToString(selectedOption));
|
||||
}
|
||||
};
|
||||
|
||||
handleChange = selectedOption => {
|
||||
this.props.onChange(selectedOption['value']);
|
||||
getSelectedValue = ({ value, options, isMultiple }) => {
|
||||
if (isMultiple) {
|
||||
const selectedOptions = List.isList(value) ? value.toJS() : value;
|
||||
|
||||
if (!selectedOptions || !Array.isArray(selectedOptions)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return selectedOptions
|
||||
.filter(i => options.find(o => o.value === (i.value || i)))
|
||||
.map(convertToOption);
|
||||
} else {
|
||||
return find(options, ['value', value]) || null;
|
||||
}
|
||||
};
|
||||
|
||||
render() {
|
||||
const { field, value, forID, classNameWrapper, setActiveStyle, setInactiveStyle } = this.props;
|
||||
const fieldOptions = field.get('options');
|
||||
const isMultiple = field.get('multiple', false);
|
||||
const isClearable = !field.get('required', true) || isMultiple;
|
||||
|
||||
if (!fieldOptions) {
|
||||
return <div>Error rendering select control for {field.get('name')}: No options</div>;
|
||||
}
|
||||
|
||||
const options = [
|
||||
...(field.get('default', false) ? [] : [{ label: '', value: '' }]),
|
||||
...fieldOptions.map(option => {
|
||||
if (typeof option === 'string') {
|
||||
return { label: option, value: option };
|
||||
}
|
||||
return Map.isMap(option) ? option.toJS() : option;
|
||||
}),
|
||||
];
|
||||
|
||||
const selectedValue = find(options, ['value', value]);
|
||||
const options = [...fieldOptions.map(convertToOption)];
|
||||
const selectedValue = this.getSelectedValue({
|
||||
options,
|
||||
value,
|
||||
isMultiple,
|
||||
});
|
||||
|
||||
return (
|
||||
<Select
|
||||
@ -87,6 +134,9 @@ export default class SelectControl extends React.Component {
|
||||
onBlur={setInactiveStyle}
|
||||
options={options}
|
||||
styles={styles}
|
||||
isMulti={isMultiple}
|
||||
isClearable={isClearable}
|
||||
placeholder=""
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
118
packages/netlify-cms-widget-select/src/__tests__/select.spec.js
Normal file
118
packages/netlify-cms-widget-select/src/__tests__/select.spec.js
Normal file
@ -0,0 +1,118 @@
|
||||
import React from 'react';
|
||||
import { fromJS } from 'immutable';
|
||||
import { render, fireEvent } from 'react-testing-library';
|
||||
import 'react-testing-library/cleanup-after-each';
|
||||
import 'jest-dom/extend-expect';
|
||||
import { SelectControl } from '../';
|
||||
|
||||
const options = [
|
||||
{ value: 'Foo', label: 'Foo' },
|
||||
{ value: 'Bar', label: 'Bar' },
|
||||
{ value: 'Baz', label: 'Baz' },
|
||||
];
|
||||
|
||||
class SelectController extends React.Component {
|
||||
state = {
|
||||
value: this.props.defaultValue,
|
||||
};
|
||||
|
||||
handleOnChange = jest.fn(value => {
|
||||
this.setState({ value });
|
||||
});
|
||||
|
||||
componentDidUpdate() {
|
||||
this.props.onStateChange(this.state);
|
||||
}
|
||||
|
||||
render() {
|
||||
return this.props.children({
|
||||
value: this.state.value,
|
||||
handleOnChange: this.handleOnChange,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function setup({ field, defaultValue }) {
|
||||
let renderArgs;
|
||||
const stateChangeSpy = jest.fn();
|
||||
const setActiveSpy = jest.fn();
|
||||
const setInactiveSpy = jest.fn();
|
||||
|
||||
const helpers = render(
|
||||
<SelectController defaultValue={defaultValue} onStateChange={stateChangeSpy}>
|
||||
{({ value, handleOnChange }) => {
|
||||
renderArgs = { value, onChangeSpy: handleOnChange };
|
||||
return (
|
||||
<SelectControl
|
||||
field={field}
|
||||
value={value}
|
||||
onChange={handleOnChange}
|
||||
forID="basic-select"
|
||||
classNameWrapper=""
|
||||
setActiveStyle={setActiveSpy}
|
||||
setInactiveStyle={setInactiveSpy}
|
||||
/>
|
||||
);
|
||||
}}
|
||||
</SelectController>,
|
||||
);
|
||||
|
||||
const input = helpers.container.querySelector('input');
|
||||
|
||||
return {
|
||||
...helpers,
|
||||
...renderArgs,
|
||||
stateChangeSpy,
|
||||
setActiveSpy,
|
||||
setInactiveSpy,
|
||||
input,
|
||||
};
|
||||
}
|
||||
|
||||
describe('Select widget', () => {
|
||||
it('should call onChange with correct selectedItem', () => {
|
||||
const field = fromJS({ options });
|
||||
const { getByText, input, onChangeSpy } = setup({ field });
|
||||
|
||||
fireEvent.focus(input);
|
||||
fireEvent.keyDown(input, { key: 'ArrowDown' });
|
||||
fireEvent.click(getByText('Foo'));
|
||||
|
||||
expect(onChangeSpy).toHaveBeenCalledTimes(1);
|
||||
expect(onChangeSpy).toHaveBeenCalledWith(options[0].value);
|
||||
});
|
||||
|
||||
it('should respect value', () => {
|
||||
const field = fromJS({ options });
|
||||
const { getByText } = setup({ field, defaultValue: options[2].value });
|
||||
|
||||
expect(getByText('Baz')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
describe('with multiple', () => {
|
||||
it('should call onChange with correct selectedItem', () => {
|
||||
const field = fromJS({ options, multiple: true });
|
||||
const { getByText, input, onChangeSpy } = setup({ field });
|
||||
|
||||
fireEvent.keyDown(input, { key: 'ArrowDown' });
|
||||
fireEvent.click(getByText('Foo'));
|
||||
fireEvent.keyDown(input, { key: 'ArrowDown' });
|
||||
fireEvent.click(getByText('Baz'));
|
||||
|
||||
expect(onChangeSpy).toHaveBeenCalledTimes(2);
|
||||
expect(onChangeSpy).toHaveBeenCalledWith(fromJS([options[0].value]));
|
||||
expect(onChangeSpy).toHaveBeenCalledWith(fromJS([options[0].value, options[2].value]));
|
||||
});
|
||||
|
||||
it('should respect value', () => {
|
||||
const field = fromJS({ options, multiple: true });
|
||||
const { getByText } = setup({
|
||||
field,
|
||||
defaultValue: fromJS([options[1].value, options[2].value]),
|
||||
});
|
||||
|
||||
expect(getByText('Bar')).toBeInTheDocument();
|
||||
expect(getByText('Baz')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user