Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select knob key/value ordering #1745

Merged
merged 19 commits into from
Jan 31, 2018
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
a12d5c5
key and value for select knob match key/value of options
psimyn Dec 29, 2017
4791306
Merge branch 'master' into select-knob-ordering
ndelangen Jan 1, 2018
c19a445
Update README.md
ndelangen Jan 1, 2018
68b543a
Merge branch 'master' into select-knob-ordering
ndelangen Jan 2, 2018
0605b99
Merge branch 'master' into select-knob-ordering
psimyn Jan 14, 2018
320a3d8
Merge branch 'master' of github.com:storybooks/storybook into select-…
psimyn Jan 15, 2018
92a68f0
Merge branch 'select-knob-ordering' of github.com:storybooks/storyboo…
psimyn Jan 15, 2018
81864f8
add deprecation messages for select and selectV2
psimyn Jan 15, 2018
571069b
don't deprecate selectV2 until v5
psimyn Jan 18, 2018
475dae7
Merge branch 'master' into select-knob-ordering
psimyn Jan 18, 2018
a188468
Merge branch 'master' of github.com:storybooks/storybook into select-…
psimyn Jan 24, 2018
880069d
Merge branch 'master' of github.com:storybooks/storybook into select-…
psimyn Jan 29, 2018
c0c8f3c
fix deprecate message, add example for select v2
psimyn Jan 30, 2018
b3e785e
Merge branch 'select-knob-ordering' of github.com:storybooks/storyboo…
psimyn Jan 30, 2018
1d1f0a7
fix example
psimyn Jan 30, 2018
1fa81fb
update snapshot with text from selectV2 example
psimyn Jan 30, 2018
b2f22ae
Merge branch 'master' into select-knob-ordering
ndelangen Jan 31, 2018
0a1140f
deprecate properly
psimyn Jan 31, 2018
5928842
Merge branch 'select-knob-ordering' of github.com:storybooks/storyboo…
psimyn Jan 31, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions addons/knobs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,25 @@ const value = select(label, options, defaultValue);

> You can also provide options as an array like this: `['red', 'blue', 'yellow']`

### selectV2

In v4 this will replace `select`. The value from the select now uses the values from the `options` object.

```js
import { selectV2 } from '@storybook/addon-knobs';

const label = 'Colors';
const options = {
Red: 'red',
Blue: 'blue',
Yellow: 'yellow',
Rainbow: ['red', 'orange', 'etc'],
None: null,
};
const defaultValue = 'Red';

const value = selectV2(label, options, defaultValue)

### date

Allow you to get date (and time) from the user.
Expand Down
3 changes: 2 additions & 1 deletion addons/knobs/src/angular/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import {
array,
date,
select,
selectV2,
button,
manager,
} from '../base';

export { knob, text, boolean, number, color, object, array, date, select, button };
export { knob, text, boolean, number, color, object, array, date, select, selectV2, button };

export const angularHandler = (channel, knobStore) => getStory => context =>
prepareComponent({ getStory, context, channel, knobStore });
Expand Down
10 changes: 9 additions & 1 deletion addons/knobs/src/base.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import deprecate from 'util-deprecate';
import KnobManager from './KnobManager';

export const manager = new KnobManager();
Expand Down Expand Up @@ -46,7 +47,14 @@ export function object(name, value) {
}

export function select(name, options, value) {
return manager.knob(name, { type: 'select', options, value });
return deprecate(
manager.knob(name, { type: 'select', options, value }),
'in v4 keys/values of the options argument are reversed'
);
}
Copy link
Member

Choose a reason for hiding this comment

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

For some reason, it throws in unit tests:

TypeError: Object prototype may only be an Object or null: apple
        at Function.setPrototypeOf (<anonymous>)

      74 | 
      75 | function select(name, options, value) {
    > 76 |   return (0, _utilDeprecate2.default)(manager.knob(name, { type: 'select', options: options, value: value }), 'in v4 keys/values of the options argument are reversed');
      77 | }
      78 | 
      79 | function selectV2(name, options, value) {
      
      at select (addons/knobs/dist/base.js:76:38)

https://circleci.com/gh/storybooks/storybook/33655?utm_campaign=vcs-integration-link&utm_medium=referral&utm_source=github-build-link

Copy link
Member Author

Choose a reason for hiding this comment

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

I was doing it wrong :)

fixed this up and added selectV2 example

Copy link
Member

Choose a reason for hiding this comment

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

This should probably be rewritten as follows:

export const select = deprecate(
  (name, options, value) => manager.knob(name, { type: 'select', options, value }),
  'in v4 keys/values of the options argument are reversed'
);

See #1745 (comment)

Copy link
Member Author

Choose a reason for hiding this comment

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

thanks! I was missing the arrow function before and passing the result of manager.knob 🤦


export function selectV2(name, options, value) {
return manager.knob(name, { type: 'select', selectV2: true, options, value });
}

export function array(name, value, separator = ',') {
Expand Down
40 changes: 40 additions & 0 deletions addons/knobs/src/components/__tests__/Select.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React from 'react';
import { shallow } from 'enzyme'; // eslint-disable-line
import SelectType from '../types/Select';

describe('Select', () => {
let knob;

beforeEach(() => {
knob = {
name: 'Colors',
value: '#00ff00',
options: {
Green: '#00ff00',
Red: '#ff0000',
},
};
});

it('displays value', () => {
const wrapper = shallow(<SelectType knob={knob} />);

const green = wrapper.find('option').first();
expect(green.text()).toEqual('#00ff00');
expect(green.prop('value')).toEqual('Green');
});

describe('selectV2', () => {
beforeEach(() => {
knob.selectV2 = true;
});

it('correctly maps option keys and values', () => {
const wrapper = shallow(<SelectType knob={knob} />);

const green = wrapper.find('option').first();
expect(green.text()).toEqual('Green');
expect(green.prop('value')).toEqual('#00ff00');
});
});
});
23 changes: 16 additions & 7 deletions addons/knobs/src/components/types/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,27 @@ const styles = {
};

class SelectType extends React.Component {
_makeOpt(key, val) {
_makeOpt(key, value, selectV2) {
const opts = {
key,
value: key,
};

return <option {...opts}>{val}</option>;
let display = value;

if (selectV2) {
opts.value = value;
display = key;
}

return <option {...opts}>{display}</option>;
}
_options(values) {
_options({ options, selectV2 }) {
let data = [];
if (Array.isArray(values)) {
data = values.map(val => this._makeOpt(val, val));
if (Array.isArray(options)) {
data = options.map(val => this._makeOpt(val, val));
} else {
data = Object.keys(values).map(key => this._makeOpt(key, values[key]));
data = Object.keys(options).map(key => this._makeOpt(key, options[key], selectV2));
}

return data;
Expand All @@ -49,7 +56,7 @@ class SelectType extends React.Component {
value={knob.value}
onChange={e => onChange(e.target.value)}
>
{this._options(knob.options)}
{this._options(knob)}
</select>
);
}
Expand All @@ -64,6 +71,8 @@ SelectType.propTypes = {
knob: PropTypes.shape({
name: PropTypes.string,
value: PropTypes.string,
options: PropTypes.oneOfType([PropTypes.array, PropTypes.object]),
selectV2: PropTypes.bool,
}),
onChange: PropTypes.func,
};
Expand Down
9 changes: 6 additions & 3 deletions addons/knobs/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ import {
number,
object,
select,
selectV2,
text,
} from './base';

export { knob, text, boolean, number, color, object, array, date, button, select };
export { knob, text, boolean, number, color, object, array, date, button, select, selectV2 };

deprecate(() => {},
'Using @storybook/addon-knobs directly is discouraged, please use @storybook/addon-knobs/{{framework}}');
deprecate(
() => {},
'Using @storybook/addon-knobs directly is discouraged, please use @storybook/addon-knobs/{{framework}}'
);

// generic higher-order component decorator for all platforms - usage is discouraged
// This file Should be removed with 4.0 release
Expand Down
3 changes: 2 additions & 1 deletion addons/knobs/src/react/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import {
array,
date,
select,
selectV2,
button,
manager,
} from '../base';

export { knob, text, boolean, number, color, object, array, date, select, button };
export { knob, text, boolean, number, color, object, array, date, select, selectV2, button };

export const reactHandler = (channel, knobStore) => getStory => context => {
const initialContent = getStory(context);
Expand Down
3 changes: 2 additions & 1 deletion addons/knobs/src/vue/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import {
array,
date,
select,
selectV2,
button,
manager,
} from '../base';

export { knob, text, boolean, number, color, object, array, date, select, button };
export { knob, text, boolean, number, color, object, array, date, select, selectV2, button };

export const vueHandler = (channel, knobStore) => getStory => context => ({
data() {
Expand Down