feat(netlify-cms-widget-relation): use react-select and add support for multiple entries (#1936)
This commit is contained in:
committed by
Shawn Erquhart
parent
944290ea1d
commit
518f6fb1c0
@ -396,7 +396,6 @@ class Backend {
|
||||
const entries = await this.listAllEntries(collection);
|
||||
const hits = fuzzy
|
||||
.filter(searchTerm, entries, { extract: extractSearchFields(searchFields) })
|
||||
.filter(entry => entry.score > 5)
|
||||
.sort(sortByScore)
|
||||
.map(f => f.original);
|
||||
return { query: searchTerm, hits };
|
||||
|
@ -66,8 +66,7 @@ export default class Widget extends Component {
|
||||
return (
|
||||
this.props.value !== nextProps.value ||
|
||||
this.props.classNameWrapper !== nextProps.classNameWrapper ||
|
||||
this.props.hasActiveStyle !== nextProps.hasActiveStyle ||
|
||||
this.props.queryHits !== nextProps.queryHits
|
||||
this.props.hasActiveStyle !== nextProps.hasActiveStyle
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -16,4 +16,5 @@ export {
|
||||
shadows,
|
||||
borders,
|
||||
transitions,
|
||||
reactSelectStyles,
|
||||
} from './styles';
|
||||
|
@ -1,6 +1,17 @@
|
||||
import { css, injectGlobal } from 'react-emotion';
|
||||
|
||||
export { fonts, colorsRaw, colors, lengths, components, buttons, shadows, borders, transitions };
|
||||
export {
|
||||
fonts,
|
||||
colorsRaw,
|
||||
colors,
|
||||
lengths,
|
||||
components,
|
||||
buttons,
|
||||
shadows,
|
||||
borders,
|
||||
transitions,
|
||||
reactSelectStyles,
|
||||
};
|
||||
|
||||
/**
|
||||
* Font Stacks
|
||||
@ -297,6 +308,49 @@ const components = {
|
||||
`,
|
||||
};
|
||||
|
||||
const reactSelectStyles = {
|
||||
control: styles => ({
|
||||
...styles,
|
||||
border: 0,
|
||||
boxShadow: 'none',
|
||||
padding: '9px 0 9px 12px',
|
||||
}),
|
||||
option: (styles, state) => ({
|
||||
...styles,
|
||||
backgroundColor: state.isSelected
|
||||
? `${colors.active}`
|
||||
: state.isFocused
|
||||
? `${colors.activeBackground}`
|
||||
: 'transparent',
|
||||
paddingLeft: '22px',
|
||||
}),
|
||||
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,
|
||||
},
|
||||
}),
|
||||
};
|
||||
|
||||
injectGlobal`
|
||||
*, *:before, *:after {
|
||||
box-sizing: border-box;
|
||||
|
@ -21,7 +21,7 @@
|
||||
"build": "cross-env NODE_ENV=production webpack"
|
||||
},
|
||||
"dependencies": {
|
||||
"react-autosuggest": "^9.3.2"
|
||||
"react-select": "^2.3.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"cross-env": "^5.2.0",
|
||||
|
@ -1,83 +1,62 @@
|
||||
import React from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
import { injectGlobal } from 'react-emotion';
|
||||
import Autosuggest from 'react-autosuggest';
|
||||
import uuid from 'uuid/v4';
|
||||
import { List } from 'immutable';
|
||||
import { debounce } from 'lodash';
|
||||
import { Loader, components } from 'netlify-cms-ui-default';
|
||||
import ImmutablePropTypes from 'react-immutable-proptypes';
|
||||
import AsyncSelect from 'react-select/lib/Async';
|
||||
import { find, isEmpty, last, debounce } from 'lodash';
|
||||
import { List, Map, fromJS } from 'immutable';
|
||||
import { reactSelectStyles } from 'netlify-cms-ui-default';
|
||||
|
||||
injectGlobal`
|
||||
.react-autosuggest__container {
|
||||
position: relative;
|
||||
}
|
||||
function optionToString(option) {
|
||||
return option && option.value ? option.value : '';
|
||||
}
|
||||
|
||||
.react-autosuggest__suggestions-container {
|
||||
display: none;
|
||||
function convertToOption(raw) {
|
||||
if (typeof raw === 'string') {
|
||||
return { label: raw, value: raw };
|
||||
}
|
||||
return Map.isMap(raw) ? raw.toJS() : raw;
|
||||
}
|
||||
|
||||
.react-autosuggest__container--open .react-autosuggest__suggestions-container {
|
||||
${components.dropdownList}
|
||||
position: absolute;
|
||||
display: block;
|
||||
top: 51px;
|
||||
width: 100%;
|
||||
z-index: 2;
|
||||
}
|
||||
function getSelectedValue({ value, options, isMultiple }) {
|
||||
if (isMultiple) {
|
||||
const selectedOptions = List.isList(value) ? value.toJS() : value;
|
||||
|
||||
.react-autosuggest__suggestion {
|
||||
${components.dropdownItem}
|
||||
}
|
||||
if (!selectedOptions || !Array.isArray(selectedOptions)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
.react-autosuggest__suggestions-list {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
list-style-type: none;
|
||||
return selectedOptions
|
||||
.map(i => options.find(o => o.value === (i.value || i)))
|
||||
.filter(Boolean)
|
||||
.map(convertToOption);
|
||||
} else {
|
||||
return find(options, ['value', value]) || null;
|
||||
}
|
||||
|
||||
.react-autosuggest__suggestion {
|
||||
cursor: pointer;
|
||||
padding: 10px 20px;
|
||||
}
|
||||
|
||||
.react-autosuggest__suggestion--focused {
|
||||
background-color: #ddd;
|
||||
}
|
||||
`;
|
||||
}
|
||||
|
||||
export default class RelationControl extends React.Component {
|
||||
didInitialSearch = false;
|
||||
|
||||
static propTypes = {
|
||||
onChange: PropTypes.func.isRequired,
|
||||
forID: PropTypes.string.isRequired,
|
||||
value: PropTypes.node,
|
||||
field: PropTypes.node,
|
||||
isFetching: PropTypes.bool,
|
||||
field: ImmutablePropTypes.map,
|
||||
fetchID: PropTypes.string,
|
||||
query: PropTypes.func.isRequired,
|
||||
clearSearch: PropTypes.func.isRequired,
|
||||
queryHits: PropTypes.oneOfType([PropTypes.array, PropTypes.object]),
|
||||
classNameWrapper: PropTypes.string.isRequired,
|
||||
setActiveStyle: PropTypes.func.isRequired,
|
||||
setInactiveStyle: PropTypes.func.isRequired,
|
||||
};
|
||||
|
||||
static defaultProps = {
|
||||
value: '',
|
||||
};
|
||||
|
||||
constructor(props, ctx) {
|
||||
super(props, ctx);
|
||||
this.controlID = uuid();
|
||||
this.didInitialSearch = false;
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
const { value, field } = this.props;
|
||||
if (value) {
|
||||
const collection = field.get('collection');
|
||||
const searchFields = field.get('searchFields').toJS();
|
||||
this.props.query(this.controlID, collection, searchFields, value);
|
||||
}
|
||||
shouldComponentUpdate(nextProps) {
|
||||
return (
|
||||
this.props.value !== nextProps.value ||
|
||||
this.props.hasActiveStyle !== nextProps.hasActiveStyle ||
|
||||
this.props.queryHits !== nextProps.queryHits ||
|
||||
this.props.metadata !== nextProps.metadata
|
||||
);
|
||||
}
|
||||
|
||||
componentDidUpdate(prevProps) {
|
||||
@ -85,108 +64,126 @@ export default class RelationControl extends React.Component {
|
||||
* Load extra post data into the store after first query.
|
||||
*/
|
||||
if (this.didInitialSearch) return;
|
||||
if (
|
||||
this.props.queryHits !== prevProps.queryHits &&
|
||||
this.props.queryHits.get &&
|
||||
this.props.queryHits.get(this.controlID)
|
||||
) {
|
||||
const { value, field, forID, queryHits, onChange } = this.props;
|
||||
|
||||
if (queryHits !== prevProps.queryHits && queryHits.get(forID)) {
|
||||
this.didInitialSearch = true;
|
||||
const suggestion = this.props.queryHits.get(this.controlID);
|
||||
if (suggestion && suggestion.length === 1) {
|
||||
const val = this.getSuggestionValue(suggestion[0]);
|
||||
this.props.onChange(val, {
|
||||
[this.props.field.get('name')]: {
|
||||
[this.props.field.get('collection')]: { [val]: suggestion[0].data },
|
||||
},
|
||||
const valueField = field.get('valueField');
|
||||
const hits = queryHits.get(forID);
|
||||
if (value) {
|
||||
const listValue = List.isList(value) ? value : List([value]);
|
||||
listValue.forEach(val => {
|
||||
const hit = hits.find(i => i.data[valueField] === val);
|
||||
if (hit) {
|
||||
onChange(value, {
|
||||
[field.get('name')]: {
|
||||
[field.get('collection')]: { [val]: hit.data },
|
||||
},
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
onChange = (event, { newValue }) => {
|
||||
this.props.onChange(newValue);
|
||||
handleChange = selectedOption => {
|
||||
const { onChange, field } = this.props;
|
||||
let value;
|
||||
|
||||
if (Array.isArray(selectedOption)) {
|
||||
value = selectedOption.map(optionToString);
|
||||
onChange(fromJS(value), {
|
||||
[field.get('name')]: {
|
||||
[field.get('collection')]: {
|
||||
[last(value)]: !isEmpty(selectedOption) && last(selectedOption).data,
|
||||
},
|
||||
},
|
||||
});
|
||||
} else {
|
||||
value = optionToString(selectedOption);
|
||||
onChange(value, {
|
||||
[field.get('name')]: {
|
||||
[field.get('collection')]: { [value]: selectedOption.data },
|
||||
},
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
onSuggestionSelected = (event, { suggestion }) => {
|
||||
const value = this.getSuggestionValue(suggestion);
|
||||
this.props.onChange(value, {
|
||||
[this.props.field.get('name')]: {
|
||||
[this.props.field.get('collection')]: { [value]: suggestion.data },
|
||||
},
|
||||
parseHitOptions = hits => {
|
||||
const { field } = this.props;
|
||||
const valueField = field.get('valueField');
|
||||
const displayField = field.get('displayFields') || field.get('valueField');
|
||||
|
||||
return hits.map(hit => {
|
||||
return {
|
||||
data: hit.data,
|
||||
value: hit.data[valueField],
|
||||
label: List.isList(displayField)
|
||||
? displayField
|
||||
.toJS()
|
||||
.map(key => hit.data[key])
|
||||
.join(' ')
|
||||
: hit.data[displayField],
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
onSuggestionsFetchRequested = debounce(({ value }) => {
|
||||
if (value.length < 2) return;
|
||||
const { field } = this.props;
|
||||
loadOptions = debounce((term, callback) => {
|
||||
const { field, query, forID } = this.props;
|
||||
const collection = field.get('collection');
|
||||
const searchFields = field.get('searchFields').toJS();
|
||||
this.props.query(this.controlID, collection, searchFields, value);
|
||||
|
||||
query(forID, collection, searchFields, term).then(({ payload }) => {
|
||||
let options = this.parseHitOptions(payload.response.hits);
|
||||
|
||||
if (!this.allOptions && !term) {
|
||||
this.allOptions = options;
|
||||
}
|
||||
|
||||
if (!term) {
|
||||
options = options.slice(0, 20);
|
||||
}
|
||||
|
||||
callback(options);
|
||||
});
|
||||
}, 500);
|
||||
|
||||
onSuggestionsClearRequested = () => {
|
||||
this.props.clearSearch();
|
||||
};
|
||||
|
||||
getSuggestionValue = suggestion => {
|
||||
const { field } = this.props;
|
||||
const valueField = field.get('valueField');
|
||||
return suggestion.data[valueField];
|
||||
};
|
||||
|
||||
renderSuggestion = suggestion => {
|
||||
const { field } = this.props;
|
||||
const valueField = field.get('displayFields') || field.get('valueField');
|
||||
if (List.isList(valueField)) {
|
||||
return (
|
||||
<span>
|
||||
{valueField.toJS().map(key => (
|
||||
<span key={key}>{new String(suggestion.data[key])} </span>
|
||||
))}
|
||||
</span>
|
||||
);
|
||||
}
|
||||
return <span>{new String(suggestion.data[valueField])}</span>;
|
||||
};
|
||||
|
||||
render() {
|
||||
const {
|
||||
value,
|
||||
isFetching,
|
||||
fetchID,
|
||||
field,
|
||||
forID,
|
||||
queryHits,
|
||||
classNameWrapper,
|
||||
setActiveStyle,
|
||||
setInactiveStyle,
|
||||
queryHits,
|
||||
} = this.props;
|
||||
const isMultiple = field.get('multiple', false);
|
||||
const isClearable = !field.get('required', true) || isMultiple;
|
||||
|
||||
const inputProps = {
|
||||
placeholder: '',
|
||||
value: value || '',
|
||||
onChange: this.onChange,
|
||||
id: forID,
|
||||
className: classNameWrapper,
|
||||
onFocus: setActiveStyle,
|
||||
onBlur: setInactiveStyle,
|
||||
};
|
||||
|
||||
const suggestions = queryHits.get ? queryHits.get(this.controlID, []) : [];
|
||||
const hits = queryHits.get(forID, []);
|
||||
const options = this.allOptions || this.parseHitOptions(hits);
|
||||
const selectedValue = getSelectedValue({
|
||||
options,
|
||||
value,
|
||||
isMultiple,
|
||||
});
|
||||
|
||||
return (
|
||||
<div>
|
||||
<Autosuggest
|
||||
suggestions={suggestions}
|
||||
onSuggestionsFetchRequested={this.onSuggestionsFetchRequested}
|
||||
onSuggestionsClearRequested={this.onSuggestionsClearRequested}
|
||||
onSuggestionSelected={this.onSuggestionSelected}
|
||||
getSuggestionValue={this.getSuggestionValue}
|
||||
renderSuggestion={this.renderSuggestion}
|
||||
inputProps={inputProps}
|
||||
focusInputOnSuggestionClick={false}
|
||||
/>
|
||||
<Loader active={isFetching && this.controlID === fetchID} />
|
||||
</div>
|
||||
<AsyncSelect
|
||||
value={selectedValue}
|
||||
inputId={forID}
|
||||
defaultOptions
|
||||
loadOptions={this.loadOptions}
|
||||
onChange={this.handleChange}
|
||||
className={classNameWrapper}
|
||||
onFocus={setActiveStyle}
|
||||
onBlur={setInactiveStyle}
|
||||
styles={reactSelectStyles}
|
||||
isMulti={isMultiple}
|
||||
isClearable={isClearable}
|
||||
placeholder=""
|
||||
/>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,207 @@
|
||||
import React from 'react';
|
||||
import { fromJS, Map } from 'immutable';
|
||||
import { last } from 'lodash';
|
||||
import { render, fireEvent, wait } from 'react-testing-library';
|
||||
import 'react-testing-library/cleanup-after-each';
|
||||
import 'jest-dom/extend-expect';
|
||||
import { RelationControl } from '../';
|
||||
|
||||
const fieldConfig = {
|
||||
name: 'post',
|
||||
collection: 'posts',
|
||||
displayFields: ['title', 'slug'],
|
||||
searchFields: ['title', 'body'],
|
||||
valueField: 'title',
|
||||
};
|
||||
|
||||
const generateHits = length => {
|
||||
const hits = Array.from({ length }, (val, idx) => {
|
||||
const title = `Post # ${idx + 1}`;
|
||||
const slug = `post-number-${idx + 1}`;
|
||||
return { collection: 'posts', data: { title, slug } };
|
||||
});
|
||||
|
||||
return [
|
||||
...hits,
|
||||
{
|
||||
collection: 'posts',
|
||||
data: { title: 'YAML post', slug: 'post-yaml', body: 'Body yaml' },
|
||||
},
|
||||
];
|
||||
};
|
||||
|
||||
class RelationController extends React.Component {
|
||||
state = {
|
||||
value: this.props.value,
|
||||
queryHits: Map(),
|
||||
};
|
||||
|
||||
handleOnChange = jest.fn(value => {
|
||||
this.setState({ ...this.state, value });
|
||||
});
|
||||
|
||||
setQueryHits = jest.fn(hits => {
|
||||
const queryHits = Map().set('relation-field', hits);
|
||||
this.setState({ ...this.state, queryHits });
|
||||
});
|
||||
|
||||
query = jest.fn((...args) => {
|
||||
const queryHits = generateHits(25);
|
||||
if (last(args) === 'YAML') {
|
||||
return Promise.resolve({ payload: { response: { hits: [last(queryHits)] } } });
|
||||
}
|
||||
return Promise.resolve({ payload: { response: { hits: queryHits } } });
|
||||
});
|
||||
|
||||
render() {
|
||||
return this.props.children({
|
||||
value: this.state.value,
|
||||
handleOnChange: this.handleOnChange,
|
||||
query: this.query,
|
||||
queryHits: this.state.queryHits,
|
||||
setQueryHits: this.setQueryHits,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function setup({ field, value }) {
|
||||
let renderArgs;
|
||||
const setActiveSpy = jest.fn();
|
||||
const setInactiveSpy = jest.fn();
|
||||
|
||||
const helpers = render(
|
||||
<RelationController value={value}>
|
||||
{({ handleOnChange, value, query, queryHits, setQueryHits }) => {
|
||||
renderArgs = { value, onChangeSpy: handleOnChange, setQueryHitsSpy: setQueryHits };
|
||||
return (
|
||||
<RelationControl
|
||||
field={field}
|
||||
value={value}
|
||||
query={query}
|
||||
queryHits={queryHits}
|
||||
onChange={handleOnChange}
|
||||
forID="relation-field"
|
||||
classNameWrapper=""
|
||||
setActiveStyle={setActiveSpy}
|
||||
setInactiveStyle={setInactiveSpy}
|
||||
/>
|
||||
);
|
||||
}}
|
||||
</RelationController>,
|
||||
);
|
||||
|
||||
const input = helpers.container.querySelector('input');
|
||||
|
||||
return {
|
||||
...helpers,
|
||||
...renderArgs,
|
||||
setActiveSpy,
|
||||
setInactiveSpy,
|
||||
input,
|
||||
};
|
||||
}
|
||||
|
||||
describe('Relation widget', () => {
|
||||
it('should list the first 20 option hits on initial load', async () => {
|
||||
const field = fromJS(fieldConfig);
|
||||
const { getAllByRole, input } = setup({ field });
|
||||
|
||||
await wait(() => {
|
||||
fireEvent.keyDown(input, { key: 'ArrowDown' });
|
||||
expect(getAllByRole('option')).toHaveLength(20);
|
||||
});
|
||||
});
|
||||
|
||||
it('should update option list based on search term', async () => {
|
||||
const field = fromJS(fieldConfig);
|
||||
const { getAllByRole, getByText, input } = setup({ field });
|
||||
|
||||
await wait(() => {
|
||||
fireEvent.change(input, { target: { value: 'YAML' } });
|
||||
expect(getAllByRole('option')).toHaveLength(1);
|
||||
expect(getByText('YAML post post-yaml')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('should call onChange with correct selectedItem value and metadata', async () => {
|
||||
const field = fromJS(fieldConfig);
|
||||
const { getByText, input, onChangeSpy } = setup({ field });
|
||||
const value = 'Post # 1';
|
||||
const label = 'Post # 1 post-number-1';
|
||||
const metadata = {
|
||||
post: { posts: { 'Post # 1': { title: 'Post # 1', slug: 'post-number-1' } } },
|
||||
};
|
||||
|
||||
await wait(() => {
|
||||
fireEvent.keyDown(input, { key: 'ArrowDown' });
|
||||
fireEvent.click(getByText(label));
|
||||
expect(onChangeSpy).toHaveBeenCalledTimes(1);
|
||||
expect(onChangeSpy).toHaveBeenCalledWith(value, metadata);
|
||||
});
|
||||
});
|
||||
|
||||
it('should update metadata for initial preview', async () => {
|
||||
const field = fromJS(fieldConfig);
|
||||
const value = 'Post # 1';
|
||||
const { getByText, onChangeSpy, setQueryHitsSpy } = setup({ field, value });
|
||||
const label = 'Post # 1 post-number-1';
|
||||
const metadata = {
|
||||
post: { posts: { 'Post # 1': { title: 'Post # 1', slug: 'post-number-1' } } },
|
||||
};
|
||||
|
||||
setQueryHitsSpy(generateHits(1));
|
||||
|
||||
await wait(() => {
|
||||
expect(getByText(label)).toBeInTheDocument();
|
||||
expect(onChangeSpy).toHaveBeenCalledTimes(1);
|
||||
expect(onChangeSpy).toHaveBeenCalledWith(value, metadata);
|
||||
});
|
||||
});
|
||||
|
||||
describe('with multiple', () => {
|
||||
it('should call onChange with correct selectedItem value and metadata', async () => {
|
||||
const field = fromJS({ ...fieldConfig, multiple: true });
|
||||
const { getByText, input, onChangeSpy } = setup({ field });
|
||||
const metadata1 = {
|
||||
post: { posts: { 'Post # 1': { title: 'Post # 1', slug: 'post-number-1' } } },
|
||||
};
|
||||
const metadata2 = {
|
||||
post: { posts: { 'Post # 2': { title: 'Post # 2', slug: 'post-number-2' } } },
|
||||
};
|
||||
|
||||
await wait(() => {
|
||||
fireEvent.keyDown(input, { key: 'ArrowDown' });
|
||||
fireEvent.click(getByText('Post # 1 post-number-1'));
|
||||
fireEvent.keyDown(input, { key: 'ArrowDown' });
|
||||
fireEvent.click(getByText('Post # 2 post-number-2'));
|
||||
|
||||
expect(onChangeSpy).toHaveBeenCalledTimes(2);
|
||||
expect(onChangeSpy).toHaveBeenCalledWith(fromJS(['Post # 1']), metadata1);
|
||||
expect(onChangeSpy).toHaveBeenCalledWith(fromJS(['Post # 1', 'Post # 2']), metadata2);
|
||||
});
|
||||
});
|
||||
|
||||
it('should update metadata for initial preview', async () => {
|
||||
const field = fromJS({ ...fieldConfig, multiple: true });
|
||||
const value = fromJS(['Post # 1', 'Post # 2']);
|
||||
const { getByText, onChangeSpy, setQueryHitsSpy } = setup({ field, value });
|
||||
const metadata1 = {
|
||||
post: { posts: { 'Post # 1': { title: 'Post # 1', slug: 'post-number-1' } } },
|
||||
};
|
||||
const metadata2 = {
|
||||
post: { posts: { 'Post # 2': { title: 'Post # 2', slug: 'post-number-2' } } },
|
||||
};
|
||||
|
||||
setQueryHitsSpy(generateHits(2));
|
||||
|
||||
await wait(() => {
|
||||
expect(getByText('Post # 1 post-number-1')).toBeInTheDocument();
|
||||
expect(getByText('Post # 2 post-number-2')).toBeInTheDocument();
|
||||
|
||||
expect(onChangeSpy).toHaveBeenCalledTimes(2);
|
||||
expect(onChangeSpy).toHaveBeenCalledWith(value, metadata1);
|
||||
expect(onChangeSpy).toHaveBeenCalledWith(value, metadata2);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
@ -4,50 +4,7 @@ import ImmutablePropTypes from 'react-immutable-proptypes';
|
||||
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: styles => ({
|
||||
...styles,
|
||||
border: 0,
|
||||
boxShadow: 'none',
|
||||
padding: '9px 0 9px 12px',
|
||||
}),
|
||||
option: (styles, state) => ({
|
||||
...styles,
|
||||
backgroundColor: state.isSelected
|
||||
? `${colors.active}`
|
||||
: state.isFocused
|
||||
? `${colors.activeBackground}`
|
||||
: 'transparent',
|
||||
paddingLeft: '22px',
|
||||
}),
|
||||
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,
|
||||
},
|
||||
}),
|
||||
};
|
||||
import { reactSelectStyles } from 'netlify-cms-ui-default';
|
||||
|
||||
function optionToString(option) {
|
||||
return option && option.value ? option.value : null;
|
||||
@ -60,6 +17,23 @@ function convertToOption(raw) {
|
||||
return Map.isMap(raw) ? raw.toJS() : raw;
|
||||
}
|
||||
|
||||
function getSelectedValue({ value, options, isMultiple }) {
|
||||
if (isMultiple) {
|
||||
const selectedOptions = List.isList(value) ? value.toJS() : value;
|
||||
|
||||
if (!selectedOptions || !Array.isArray(selectedOptions)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return selectedOptions
|
||||
.map(i => options.find(o => o.value === (i.value || i)))
|
||||
.filter(Boolean)
|
||||
.map(convertToOption);
|
||||
} else {
|
||||
return find(options, ['value', value]) || null;
|
||||
}
|
||||
}
|
||||
|
||||
export default class SelectControl extends React.Component {
|
||||
static propTypes = {
|
||||
onChange: PropTypes.func.isRequired,
|
||||
@ -96,23 +70,6 @@ export default class SelectControl extends React.Component {
|
||||
}
|
||||
};
|
||||
|
||||
getSelectedValue = ({ value, options, isMultiple }) => {
|
||||
if (isMultiple) {
|
||||
const selectedOptions = List.isList(value) ? value.toJS() : value;
|
||||
|
||||
if (!selectedOptions || !Array.isArray(selectedOptions)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return selectedOptions
|
||||
.map(i => options.find(o => o.value === (i.value || i)))
|
||||
.filter(Boolean)
|
||||
.map(convertToOption);
|
||||
} else {
|
||||
return find(options, ['value', value]) || null;
|
||||
}
|
||||
};
|
||||
|
||||
render() {
|
||||
const { field, value, forID, classNameWrapper, setActiveStyle, setInactiveStyle } = this.props;
|
||||
const fieldOptions = field.get('options');
|
||||
@ -124,7 +81,7 @@ export default class SelectControl extends React.Component {
|
||||
}
|
||||
|
||||
const options = [...fieldOptions.map(convertToOption)];
|
||||
const selectedValue = this.getSelectedValue({
|
||||
const selectedValue = getSelectedValue({
|
||||
options,
|
||||
value,
|
||||
isMultiple,
|
||||
@ -139,7 +96,7 @@ export default class SelectControl extends React.Component {
|
||||
onFocus={setActiveStyle}
|
||||
onBlur={setInactiveStyle}
|
||||
options={options}
|
||||
styles={styles}
|
||||
styles={reactSelectStyles}
|
||||
isMulti={isMultiple}
|
||||
isClearable={isClearable}
|
||||
placeholder=""
|
||||
|
Reference in New Issue
Block a user