Skip to content

Commit 804b3aa

Browse files
fix(select-widget): select widget not able to select number value 0 (#7254)
Fixes #6515 Co-authored-by: Martin Jagodic <[email protected]>
1 parent 9881de0 commit 804b3aa

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

packages/decap-cms-widget-select/src/SelectControl.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import { reactSelectStyles } from 'decap-cms-ui-default';
88
import { validations } from 'decap-cms-lib-widgets';
99

1010
function optionToString(option) {
11-
return option && option.value ? option.value : null;
11+
return option && (typeof option.value === 'number' || typeof option.value === 'string')
12+
? option.value
13+
: null;
1214
}
1315

1416
function convertToOption(raw) {
@@ -47,9 +49,10 @@ export default class SelectControl extends React.Component {
4749
options: ImmutablePropTypes.listOf(
4850
PropTypes.oneOfType([
4951
PropTypes.string,
52+
PropTypes.number,
5053
ImmutablePropTypes.contains({
5154
label: PropTypes.string.isRequired,
52-
value: PropTypes.string.isRequired,
55+
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
5356
}),
5457
]),
5558
).isRequired,

packages/decap-cms-widget-select/src/__tests__/select.spec.js

+33
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ const options = [
1212
{ value: 'baz', label: 'Baz' },
1313
];
1414
const stringOptions = ['foo', 'bar', 'baz'];
15+
const numberOptions = [
16+
{ value: 0, label: 'Foo' },
17+
{ value: 1, label: 'Bar' },
18+
{ value: 2, label: 'Baz' },
19+
];
1520

1621
class SelectController extends React.Component {
1722
state = {
@@ -134,6 +139,18 @@ describe('Select widget', () => {
134139
expect(getByText('baz')).toBeInTheDocument();
135140
});
136141

142+
it('should call onChange with correct selectedItem when value is number 0', () => {
143+
const field = fromJS({ options: numberOptions });
144+
const { getByText, input, onChangeSpy } = setup({ field });
145+
146+
fireEvent.focus(input);
147+
fireEvent.keyDown(input, { key: 'ArrowDown' });
148+
fireEvent.click(getByText('Foo'));
149+
150+
expect(onChangeSpy).toHaveBeenCalledTimes(1);
151+
expect(onChangeSpy).toHaveBeenCalledWith(numberOptions[0].value);
152+
});
153+
137154
describe('with multiple', () => {
138155
it('should call onChange with correct selectedItem', () => {
139156
const field = fromJS({ options, multiple: true });
@@ -248,6 +265,22 @@ describe('Select widget', () => {
248265
expect(getByText('bar')).toBeInTheDocument();
249266
expect(getByText('baz')).toBeInTheDocument();
250267
});
268+
269+
it('should call onChange with correct selectedItem when values are numbers including 0', () => {
270+
const field = fromJS({ options: numberOptions, multiple: true });
271+
const { getByText, input, onChangeSpy } = setup({ field });
272+
273+
fireEvent.keyDown(input, { key: 'ArrowDown' });
274+
fireEvent.click(getByText('Foo'));
275+
fireEvent.keyDown(input, { key: 'ArrowDown' });
276+
fireEvent.click(getByText('Baz'));
277+
278+
expect(onChangeSpy).toHaveBeenCalledTimes(2);
279+
expect(onChangeSpy).toHaveBeenCalledWith(fromJS([numberOptions[0].value]));
280+
expect(onChangeSpy).toHaveBeenCalledWith(
281+
fromJS([numberOptions[0].value, numberOptions[2].value]),
282+
);
283+
});
251284
});
252285
describe('validation', () => {
253286
function validate(setupOpts) {

0 commit comments

Comments
 (0)