Merge pull request #1811 from gpittarelli/1404-fix-empty-array-knob

Return empty array when Array knob is empty
This commit is contained in:
Filipp Riabchun 2017-09-07 14:12:28 +03:00 committed by GitHub
commit ed705f3b89
2 changed files with 21 additions and 1 deletions

View File

@ -15,4 +15,17 @@ describe('Array', () => {
wrapper.simulate('change', { target: { value: 'Fhishing,Skiing,Dancing' } });
expect(onChange).toHaveBeenCalledWith(['Fhishing', 'Skiing', 'Dancing']);
});
it('should change to an empty array when emptied', () => {
const onChange = jest.fn();
const wrapper = shallow(
<Array
onChange={onChange}
knob={{ name: 'passions', value: ['Fishing', 'Skiing'], separator: ',' }}
/>
);
wrapper.simulate('change', { target: { value: '' } });
expect(onChange).toHaveBeenCalledWith([]);
});
});

View File

@ -16,6 +16,13 @@ const styles = {
color: '#555',
};
function formatArray(value, separator) {
if (value === '') {
return [];
}
return value.split(separator);
}
class ArrayType extends React.Component {
render() {
const { knob, onChange } = this.props;
@ -27,7 +34,7 @@ class ArrayType extends React.Component {
}}
style={styles}
value={knob.value.join(knob.separator)}
onChange={e => onChange(e.target.value.split(knob.separator))}
onChange={e => onChange(formatArray(e.target.value, knob.separator))}
/>
);
}