Skip to content

Commit

Permalink
Merge pull request #7568 from elliotlarson/next
Browse files Browse the repository at this point in the history
Fix select knob default selection when using array-values [key] => val[]
  • Loading branch information
kroeder authored Jul 26, 2019
2 parents 097e559 + 8ff1ef3 commit 2f5bb39
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
15 changes: 15 additions & 0 deletions addons/knobs/src/components/__tests__/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ describe('Select', () => {
expect(green.text()).toEqual('Green');
expect(green.prop('value')).toEqual('Green');
});

it('should set the default value for array-values correctly', () => {
knob = {
name: 'Array values',
options: {
'100 x 100': [100, 100],
'200 x 200': [200, 200],
},
value: [200, 200],
};

const wrapper = shallow(<SelectType knob={knob} />);
const value = wrapper.prop('value');
expect(value).toEqual('200 x 200');
});
});

describe('Array values', () => {
Expand Down
7 changes: 6 additions & 1 deletion addons/knobs/src/components/types/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ const SelectType: FunctionComponent<SelectTypeProps> & {
? options.reduce<Record<string, SelectTypeKnobValue>>((acc, k) => ({ ...acc, [k]: k }), {})
: (options as Record<string, SelectTypeKnobValue>);

const selectedKey = Object.keys(entries).find(k => entries[k] === knob.value);
const selectedKey = Object.keys(entries).find(k => {
if (Array.isArray(knob.value)) {
return JSON.stringify(entries[k]) === JSON.stringify(knob.value);
}
return entries[k] === knob.value;
});

return (
<Form.Select
Expand Down

1 comment on commit 2f5bb39

@vercel
Copy link

@vercel vercel bot commented on 2f5bb39 Jul 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.