Skip to content

Commit

Permalink
Return empty array when Array knob is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
gpittarelli committed Sep 7, 2017
1 parent e1fbd09 commit cba07ad
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
13 changes: 13 additions & 0 deletions addons/knobs/src/components/__tests__/Array.js
Original file line number Diff line number Diff line change
Expand Up @@ -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([]);
});
});
9 changes: 8 additions & 1 deletion addons/knobs/src/components/types/Array.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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))}
/>
);
}
Expand Down

0 comments on commit cba07ad

Please sign in to comment.