Skip to content

Commit

Permalink
fix(react): add prop adapter for downshift changes (#6427)
Browse files Browse the repository at this point in the history
* fix(react): add prop adapter for downshift changes

* fix(combobox): use initialSelectedItem instead of defaultSelectedItem

Co-authored-by: Andrea N. Cardona <andreancardona@gmail.com>
Co-authored-by: TJ Egan <tw15egan@gmail.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
4 people committed Jul 10, 2020
1 parent bb8d956 commit 0037736
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
5 changes: 3 additions & 2 deletions packages/react/src/components/ComboBox/ComboBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Checkmark16, WarningFilled16 } from '@carbon/icons-react';
import ListBox, { PropTypes as ListBoxPropTypes } from '../ListBox';
import { match, keys } from '../../internal/keyboard';
import setupGetInstanceId from '../../tools/setupGetInstanceId';
import { mapDownshiftProps } from '../../tools/createPropAdapter';

const { prefix } = settings;

Expand Down Expand Up @@ -333,13 +334,13 @@ export default class ComboBox extends React.Component {
const ItemToElement = itemToElement;
return (
<Downshift
{...downshiftProps}
{...mapDownshiftProps(downshiftProps)}
onChange={this.handleOnChange}
onInputValueChange={this.handleOnInputValueChange}
onStateChange={this.handleOnStateChange}
inputValue={this.state.inputValue || ''}
itemToString={itemToString}
defaultSelectedItem={initialSelectedItem}
initialSelectedItem={initialSelectedItem}
inputId={id}
selectedItem={selectedItem}>
{({
Expand Down
7 changes: 4 additions & 3 deletions packages/react/src/components/Dropdown/Dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import { useSelect } from 'downshift';
import { settings } from 'carbon-components';
import cx from 'classnames';
import PropTypes from 'prop-types';
import ListBox, { PropTypes as ListBoxPropTypes } from '../ListBox';
import { Checkmark16, WarningFilled16 } from '@carbon/icons-react';
import ListBox, { PropTypes as ListBoxPropTypes } from '../ListBox';
import { mapDownshiftProps } from '../../tools/createPropAdapter';

const { prefix } = settings;

Expand Down Expand Up @@ -46,13 +47,13 @@ function Dropdown({
selectedItem: controlledSelectedItem,
downshiftProps,
}) {
const selectProps = {
const selectProps = mapDownshiftProps({
...downshiftProps,
items,
itemToString,
initialSelectedItem,
onSelectedItemChange,
};
});

// only set selectedItem if the prop is defined. Setting if it is undefined
// will overwrite default selected items from useSelect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { defaultItemToString } from './tools/itemToString';
import { defaultSortItems, defaultCompareItems } from './tools/sorting';
import { defaultFilterItems } from '../ComboBox/tools/filter';
import setupGetInstanceId from '../../tools/setupGetInstanceId';
import { mapDownshiftProps } from '../../tools/createPropAdapter';

const { prefix } = settings;

Expand Down Expand Up @@ -321,7 +322,7 @@ export default class FilterableMultiSelect extends React.Component {
initialSelectedItems={initialSelectedItems}
render={({ selectedItems, onItemChange, clearSelection }) => (
<Downshift
{...downshiftProps}
{...mapDownshiftProps(downshiftProps)}
highlightedIndex={highlightedIndex}
id={id}
isOpen={isOpen}
Expand Down
21 changes: 12 additions & 9 deletions packages/react/src/components/MultiSelect/MultiSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { defaultItemToString } from './tools/itemToString';
import { defaultSortItems, defaultCompareItems } from './tools/sorting';
import { useSelection } from '../../internal/Selection';
import setupGetInstanceId from '../../tools/setupGetInstanceId';
import { mapDownshiftProps } from '../../tools/createPropAdapter';

const { prefix } = settings;
const noop = () => {};
Expand Down Expand Up @@ -79,15 +80,17 @@ function MultiSelect({
getMenuProps,
getItemProps,
selectedItem: selectedItems,
} = useSelect({
...downshiftProps,
highlightedIndex,
isOpen,
itemToString,
onStateChange,
selectedItem: controlledSelectedItems,
items,
});
} = useSelect(
mapDownshiftProps({
...downshiftProps,
highlightedIndex,
isOpen,
itemToString,
onStateChange,
selectedItem: controlledSelectedItems,
items,
})
);

/**
* programmatically control this `open` prop
Expand Down

0 comments on commit 0037736

Please sign in to comment.