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

Multiselect table filter #1523

Merged
merged 6 commits into from
Aug 21, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 6 additions & 1 deletion src/components/Table/Table.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,12 @@ const propTypes = {
filters: PropTypes.arrayOf(
PropTypes.shape({
columnId: PropTypes.string.isRequired,
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.bool]).isRequired,
value: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number,
PropTypes.bool,
PropTypes.arrayOf(PropTypes.string),
]).isRequired,
})
),
toolbar: PropTypes.shape({
Expand Down
46 changes: 46 additions & 0 deletions src/components/Table/Table.story.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,52 @@ storiesOf('Watson IoT/Table', module)
},
}
)
.add(
'Stateful Example with multiselect filtering',
() => (
<FullWidthWrapper>
<StatefulTable
{...initialState}
columns={initialState.columns.map(column => {
if (column.filter) {
return {
...column,
filter: { ...column.filter, isMultiselect: !!column.filter?.options },
};
}
return column;
})}
view={{
...initialState.view,
pagination: {
...initialState.view.pagination,
maxPages: 5,
},
toolbar: {
activeBar: 'filter',
},
}}
secondaryTitle={text('Secondary Title', `Row count: ${initialState.data.length}`)}
actions={actions}
isSortable
lightweight={boolean('lightweight', false)}
options={{
...initialState.options,
hasFilter: select('hasFilter', ['onKeyPress', 'onEnterAndBlur'], 'onKeyPress'),
wrapCellText: select('wrapCellText', selectTextWrapping, 'always'),
hasSingleRowEdit: true,
}}
/>
</FullWidthWrapper>
),
{
info: {
text: `This table has a multiselect filter. To support multiselect filtering, make sure to pass isMultiselect: true to the filter prop on the table.`,
propTables: [Table],
propTablesExclude: [StatefulTable],
},
}
)
.add(
'Stateful Example with row nesting and fixed columns',
() => <StatefulTableWithNestedRowItems />,
Expand Down
104 changes: 73 additions & 31 deletions src/components/Table/TableHead/FilterHeaderRow/FilterHeaderRow.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { ComboBox, DataTable, FormItem, TextInput } from 'carbon-components-react';
import { ComboBox, DataTable, FormItem, TextInput, MultiSelect } from 'carbon-components-react';
import { Close16 } from '@carbon/icons-react';
import memoize from 'lodash/memoize';
import classnames from 'classnames';
Expand Down Expand Up @@ -30,6 +30,8 @@ class FilterHeaderRow extends Component {
text: PropTypes.string.isRequired,
})
),
/** if isMultiselect and isFilterable are true, the table is filtered based on a multiselect */
isMultiselect: PropTypes.bool,
})
).isRequired,
/** internationalized string */
Expand All @@ -48,7 +50,12 @@ class FilterHeaderRow extends Component {
filters: PropTypes.arrayOf(
PropTypes.shape({
columnId: PropTypes.string.isRequired,
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.bool]).isRequired,
value: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number,
PropTypes.bool,
PropTypes.arrayOf(PropTypes.string),
]).isRequired,
})
),
/** Callback when filter is applied sends object of keys and values with the filter values */
Expand Down Expand Up @@ -192,35 +199,70 @@ class FilterHeaderRow extends Component {
column.isFilterable !== undefined && !column.isFilterable ? (
<div />
) : column.options ? (
<ComboBox
key={columnStateValue}
className={`${iotPrefix}--filterheader-combo`}
id={`column-${i}`}
aria-label={filterText}
translateWithId={this.handleTranslation}
items={memoizeColumnOptions(column.options)}
itemToString={item => (item ? item.text : '')}
initialSelectedItem={{
id: columnStateValue,
text: (
column.options.find(option => option.id === columnStateValue) || { text: '' }
).text, // eslint-disable-line react/destructuring-assignment
}}
placeholder={column.placeholderText || 'Choose an option'}
onChange={evt => {
this.setState(
state => ({
filterValues: {
...state.filterValues,
[column.id]: evt.selectedItem === null ? '' : evt.selectedItem.id,
},
}),
this.handleApplyFilter
);
}}
light={lightweight}
disabled={isDisabled}
/>
column.isMultiselect ? (
<MultiSelect
key={columnStateValue}
className={`${iotPrefix}--filterheader-multiselect`}
id={`column-${i}`}
aria-label={filterText}
translateWithId={this.handleTranslation}
items={memoizeColumnOptions(column.options)}
label={column.placeholderText || 'Choose an option'}
itemToString={item => (item ? item.text : '')}
initialSelectedItems={
Array.isArray(columnStateValue)
? columnStateValue.map(value =>
typeof value !== 'object' ? { id: value, text: value } : value
)
: [{ id: columnStateValue, text: columnStateValue }]
}
onChange={evt => {
this.setState(
state => ({
filterValues: {
...state.filterValues,
[column.id]: evt.selectedItems.map(item => item.text),
},
}),
this.handleApplyFilter
);
}}
light
disabled={isDisabled}
/>
) : (
<ComboBox
key={columnStateValue}
className={`${iotPrefix}--filterheader-combo`}
id={`column-${i}`}
aria-label={filterText}
translateWithId={this.handleTranslation}
items={memoizeColumnOptions(column.options)}
itemToString={item => (item ? item.text : '')}
initialSelectedItem={{
id: columnStateValue,
text: (
column.options.find(option => option.id === columnStateValue) || {
text: '',
}
).text, // eslint-disable-line react/destructuring-assignment
}}
placeholder={column.placeholderText || 'Choose an option'}
onChange={evt => {
this.setState(
state => ({
filterValues: {
...state.filterValues,
[column.id]: evt.selectedItem === null ? '' : evt.selectedItem.id,
},
}),
this.handleApplyFilter
);
}}
light={lightweight}
disabled={isDisabled}
/>
)
) : (
<FormItem className={`${iotPrefix}--filter-header-row--form-item`}>
<TextInput
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@
padding-right: $spacing-03;
}

.#{$iot-prefix}--filterheader-multiselect {
JoelArmendariz marked this conversation as resolved.
Show resolved Hide resolved
.bx--list-box__selection--multi {
background-color: #393939;
}
.bx--list-box__selection--multi > svg {
fill: #fff;
}
}

.#{$prefix}--tag--filter {
background-color: transparent;

Expand Down
8 changes: 7 additions & 1 deletion src/components/Table/TableHead/TableHead.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,12 @@ const propTypes = {
filters: PropTypes.arrayOf(
PropTypes.shape({
columnId: PropTypes.string.isRequired,
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.bool]).isRequired,
value: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number,
PropTypes.bool,
PropTypes.arrayOf(PropTypes.string),
]).isRequired,
})
),
}).isRequired,
Expand Down Expand Up @@ -379,6 +384,7 @@ const TableHead = ({
...column.filter,
id: column.id,
isFilterable: !isNil(column.filter),
isMultiselect: column.filter?.isMultiselect,
width: column.width,
}))}
hasFastFilter={hasFastFilter}
Expand Down
1 change: 1 addition & 0 deletions src/components/Table/TablePropTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ export const TableColumnsPropTypes = PropTypes.arrayOf(
filter: PropTypes.shape({
/** I18N text for the filter */
placeholderText: PropTypes.string,
isMultiselect: PropTypes.bool,
JoelArmendariz marked this conversation as resolved.
Show resolved Hide resolved
options: PropTypes.arrayOf(
PropTypes.shape({
id: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.bool]).isRequired,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ exports[`Storybook Snapshot tests and console checks Storyshots Watson IoT/Table
>
<span
className="bx--list-box__label"
htmlFor="downshift-10-input"
JoelArmendariz marked this conversation as resolved.
Show resolved Hide resolved
htmlFor="downshift-11-input"
id="dropdown-field-label-iot--view-dropdown"
title={
<TableViewDropdownItem
Expand Down Expand Up @@ -145,7 +145,7 @@ exports[`Storybook Snapshot tests and console checks Storyshots Watson IoT/Table
>
<div
className="bx--list-box__menu-item bx--list-box__menu-item--active bx--list-box__menu-item--highlighted"
id="downshift-10-item-0"
id="downshift-11-item-0"
onClick={[Function]}
onMouseDown={[Function]}
onMouseMove={[Function]}
Expand Down Expand Up @@ -234,7 +234,7 @@ exports[`Storybook Snapshot tests and console checks Storyshots Watson IoT/Table
</div>
<div
className="bx--list-box__menu-item"
id="downshift-10-item-1"
id="downshift-11-item-1"
onClick={[Function]}
onMouseDown={[Function]}
onMouseMove={[Function]}
Expand Down Expand Up @@ -281,7 +281,7 @@ exports[`Storybook Snapshot tests and console checks Storyshots Watson IoT/Table
</div>
<div
className="bx--list-box__menu-item"
id="downshift-10-item-2"
id="downshift-11-item-2"
onClick={[Function]}
onMouseDown={[Function]}
onMouseMove={[Function]}
Expand Down Expand Up @@ -328,7 +328,7 @@ exports[`Storybook Snapshot tests and console checks Storyshots Watson IoT/Table
</div>
<div
className="bx--list-box__menu-item"
id="downshift-10-item-3"
id="downshift-11-item-3"
onClick={[Function]}
onMouseDown={[Function]}
onMouseMove={[Function]}
Expand Down Expand Up @@ -375,7 +375,7 @@ exports[`Storybook Snapshot tests and console checks Storyshots Watson IoT/Table
</div>
<div
className="bx--list-box__menu-item"
id="downshift-10-item-4"
id="downshift-11-item-4"
onClick={[Function]}
onMouseDown={[Function]}
onMouseMove={[Function]}
Expand Down Expand Up @@ -423,7 +423,7 @@ exports[`Storybook Snapshot tests and console checks Storyshots Watson IoT/Table
</div>
<div
className="bx--list-box__menu-item"
id="downshift-10-item-5"
id="downshift-11-item-5"
onClick={[Function]}
onMouseDown={[Function]}
onMouseMove={[Function]}
Expand Down Expand Up @@ -471,7 +471,7 @@ exports[`Storybook Snapshot tests and console checks Storyshots Watson IoT/Table
</div>
<div
className="bx--list-box__menu-item"
id="downshift-10-item-6"
id="downshift-11-item-6"
onClick={[Function]}
onMouseDown={[Function]}
onMouseMove={[Function]}
Expand Down
Loading