Skip to content

Commit

Permalink
Multiple table selection enabled after search. #396 (#491)
Browse files Browse the repository at this point in the history
* fix: multiple table selection enabled after search

* fix: removed console statements

* fix: added  select all functionality

* fix: select all issue fixed
  • Loading branch information
himanshu634 authored Jul 10, 2024
1 parent c23ae65 commit 3a4d429
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions wren-ui/src/components/table/MultiSelectBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ interface Props {

export default function MultiSelectBox(props: Props) {
const { columns, loading, items, onChange, value } = props;
const [selectedRowKeys, setSelectedRowKeys] = useState<React.Key[]>(
value || [],
const [selectedRowKeys, setSelectedRowKeys] = useState<Set<string>>(
new Set(value),
);
const [searchValue, setSearchValue] = useState<string>('');
const formItemContext =
Expand All @@ -59,9 +59,15 @@ export default function MultiSelectBox(props: Props) {
: items;
}, [items, searchValue]);

const onSelect = (rowKeys: React.Key[]) => {
setSelectedRowKeys(rowKeys);
onChange && onChange(rowKeys as string[]);
const onSelect = (rowKey: string) => {
const newSelectedRowKey = new Set(selectedRowKeys);
if (newSelectedRowKey.has(rowKey)) {
newSelectedRowKey.delete(rowKey);
} else {
newSelectedRowKey.add(rowKey);
}
setSelectedRowKeys(newSelectedRowKey);
onChange && onChange(Array.from(newSelectedRowKey));
};

const onSearchChange = (event) => {
Expand All @@ -71,9 +77,9 @@ export default function MultiSelectBox(props: Props) {
};

const total =
selectedRowKeys.length === 0
selectedRowKeys.size === 0
? items.length
: `${selectedRowKeys.length}/${items.length}`;
: `${selectedRowKeys.size}/${items.length}`;

return (
<StyledBox
Expand All @@ -91,8 +97,24 @@ export default function MultiSelectBox(props: Props) {
<Table
rowSelection={{
type: 'checkbox',
selectedRowKeys,
onChange: onSelect,
selectedRowKeys: Array.from(selectedRowKeys),
onSelect: (record) => onSelect(record['value']),
onChange(keys) {
// if more than 1 rows selected and there is only one possibility, if user selects all rows.
if (keys.length !== 1) {
if (keys.length === 0) {
setSelectedRowKeys(new Set());
onChange && onChange([]);
return;
}
const newSelectedRowKeys = [
...selectedRowKeys,
...(keys as string[]),
];
setSelectedRowKeys(new Set(newSelectedRowKeys));
onChange && onChange(newSelectedRowKeys);
}
},
}}
rowKey={(record) => record.value}
columns={columns}
Expand Down

0 comments on commit 3a4d429

Please sign in to comment.