Skip to content

Add multiselect functionality to select component #192

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

Merged
merged 2 commits into from
Jan 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
26 changes: 16 additions & 10 deletions packages/react-components/source/react/library/select/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,12 @@ class Select extends Component {
}

static getDerivedStateFromProps(props, state) {
let newProps;

if (isControlled(props) || !state.open) {
newProps = {
return {
listValue: props.value,
};
}

return newProps;
return null;
}

// If `open` prop is passed as default, let's trigger menu open
Expand Down Expand Up @@ -192,6 +189,10 @@ class Select extends Component {

this.setState({ focusedIndex: 0 });
}

if (type !== MULTISELECT) {
this.closeAndFocusButton();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm curious, what was the behavior you were trying to enable (or prevent) by adding this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I found that the select component with no type specified (if this is not in) it will not close the dropdown option list and therefore the user is able to select multiple options from the dropdown. This is acting as a catch-all to say if it isn't multi-select then close the drop down option list after one option from the list has been selected. Hope that makes sense!

Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting, thanks!

}

onActionClick() {
Expand Down Expand Up @@ -280,10 +281,17 @@ class Select extends Component {
}

getButtonLabel() {
const { type, options, value } = this.props;
const { type, options, value, placeholder } = this.props;
if (!value || value.length === 0) {
return placeholder;
}

if (type === MULTISELECT) {
const selectedOptions = options
.filter(option => value.includes(option.value))
.map(option => option.selectedLabel || option.label);

if (type === MULTISELECT || !value) {
return null;
return selectedOptions.join(', ');
}

const selectedOption = options.find(option => option.value === value);
Expand All @@ -293,7 +301,6 @@ class Select extends Component {

getOptions() {
const { options, value, type, onFilter } = this.props;

let filteredOptions = options;

// If the ingesting app uses the onFilter event handler, it should provide the filtered options
Expand Down Expand Up @@ -454,7 +461,6 @@ class Select extends Component {
onEscape={closeAndFocusButton}
onChange={onValueChange}
onFocusItem={onFocusItem}
onClickItem={closeAndFocusButton}
footer={footer}
style={menuStyle}
actionLabel={getActionLabel(this.props)}
Expand Down
42 changes: 42 additions & 0 deletions packages/react-components/source/react/library/select/Select.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,45 @@ const style = { margin: 10 };
/>
</div>;
```


### MultiSelect

With type `multiselect`, the Select input will allow multiple values to be selected. In this mode an Apply button will render below the options list. The newly selected values are not applied until the user activates this button unless the `applyImmediately` props has been passed a boolean value of true in which case it will apply immediately. If the options chosen exceed the side of the input the excess content will be replaced with an ellipsis. If they escape or click out of the open menu, their changes will be discarded.

```jsx
initialState = {
value: [],
};

const options = [
{ value: 'en', label: 'English' },
{ value: 'ru', label: 'русский' },
{ value: 'zh', label: '中文' },
{ value: 'sq', label: 'Albanian' },
{ value: 'ar', label: 'Arabic' },
{ value: 'eu', label: 'Basque' },
{ value: 'bn', label: 'Bengali' },
{ value: 'bs', label: 'Bosnian' },
{ value: 'bg', label: 'Bulgarian' },
{ value: 'ca', label: 'Catalan' },
];

const style = { margin: 10 };

<div>
<Select
id="button-select-one"
name="select-example-one"
options={options}
placeholder="Select your language"
style={style}
value={state.value}
onChange={value => {
console.log('New Value', value);
setState({ value });
}}
type="multiselect"
/>
</div>;
```
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@

.rc-select-target .rc-input {
cursor: pointer;
overflow: hidden;
padding-right: 30px;
text-align: left;
text-overflow: ellipsis;
white-space: nowrap;
}

.rc-menu-list {
Expand Down