mirror of
https://github.com/storybookjs/storybook.git
synced 2025-04-08 11:11:53 +08:00
Merge pull request #1811 from gpittarelli/1404-fix-empty-array-knob
Return empty array when Array knob is empty
This commit is contained in:
commit
ed705f3b89
@ -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([]);
|
||||
});
|
||||
});
|
||||
|
@ -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))}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user