Skip to content

Commit

Permalink
feat(dropdownv2): Support uncontrolled props (carbon-design-system#729)
Browse files Browse the repository at this point in the history
In the case that the selected item is driven by an external component.
  • Loading branch information
Paul Sachs authored and tw15egan committed Mar 22, 2018
1 parent 38b8f56 commit 64f4698
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
25 changes: 25 additions & 0 deletions src/components/DropdownV2/DropdownV2-story.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import { storiesOf, action } from '@storybook/react';
import DropdownV2 from '../DropdownV2';
import WithState from '../../tools/withState';

const items = [
{
Expand Down Expand Up @@ -89,4 +90,28 @@ storiesOf('DropdownV2', module)
/>
</div>
)
)
.addWithInfo(
'fully controlled',
`
Sometimes you want to control everything.
`,
() => (
<WithState initialState={{ selectedItem: items[0] }}>
{({ state, setState }) => (
<div style={{ width: 300 }}>
<DropdownV2
type="inline"
label="Label"
items={items}
itemToString={item => (item ? item.text : '')}
onChange={({ selectedItem }) =>
setTimeout(() => setState({ selectedItem }), 1000)
}
selectedItem={state.selectedItem}
/>
</div>
)}
</WithState>
)
);
9 changes: 8 additions & 1 deletion src/components/DropdownV2/DropdownV2.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ export default class DropdownV2 extends React.Component {
label: PropTypes.node.isRequired,

type: ListBoxPropTypes.ListBoxType,

/**
* In the case you want to control the dropdown selection entirely.
*/
selectedItem: PropTypes.object,
};

static defaultProps = {
Expand All @@ -66,13 +71,15 @@ export default class DropdownV2 extends React.Component {
itemToString,
type,
initialSelectedItem,
selectedItem,
} = this.props;
const className = cx('bx--dropdown-v2', containerClassName);
return (
<Downshift
onChange={this.handleOnChange}
itemToString={itemToString}
defaultSelectedItem={initialSelectedItem}>
defaultSelectedItem={initialSelectedItem}
selectedItem={selectedItem}>
{({
isOpen,
itemToString,
Expand Down

0 comments on commit 64f4698

Please sign in to comment.