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

1617 add select all checker to filter multi select widget #1619

Merged
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
2 changes: 1 addition & 1 deletion api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@devtable/api",
"version": "14.10.0",
"version": "14.11.0",
"description": "",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion dashboard/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@devtable/dashboard",
"version": "14.10.0",
"version": "14.11.0",
"license": "Apache-2.0",
"publishConfig": {
"access": "public",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ export default createStyles((theme, { radius = 4, name, ...rest }: MultiSelectWi
overflowWrap: 'break-word',
},
},
'.rc-select-item-option-state-icon': {
lineHeight: '20px',
},
},
'.rc-virtual-list-holder-inner': {
alignItems: 'flex-start',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
import { Badge, CloseButton, Group, MantineRadius, Stack, Text, Tooltip } from '@mantine/core';
import { Badge, Checkbox, CloseButton, Divider, Group, MantineRadius, Stack, Text, Tooltip } from '@mantine/core';
import { EmotionStyles } from '@mantine/emotion';
import _ from 'lodash';
import Select, { Option } from 'rc-select';
import { useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { ErrorMessageOrNotFound } from '~/components/filter/error-message-or-not-found';
import useStyles from './widget.styles';

const DropdownHeaderStyles: EmotionStyles = {
root: {
cursor: 'pointer',
'&:hover': {
backgroundColor: 'var(--mantine-color-default-hover)',
},
'input, .mantine-Checkbox-labelWrapper': {
cursor: 'pointer',
pointerEvents: 'none',
},
},
};

export type TSelectOption = {
label: string;
value: string;
Expand Down Expand Up @@ -55,6 +70,30 @@ export const MultiSelectWidget = ({
return options.filter(match);
}, [keyword, options]);

const allValueSet = useMemo(() => {
return new Set(options.map((o) => o.value));
}, [options]);

const selectedValueSet = useMemo(() => {
return new Set(value);
}, [value]);

const allSelected = useMemo(() => {
if (selectedValueSet.size !== allValueSet.size) {
return false;
}
return Array.from(allValueSet).every((v) => selectedValueSet.has(v));
}, [selectedValueSet, allValueSet]);

const toggleSelectAll = () => {
if (allSelected) {
onChange([]);
return;
}
const newValue = Array.from(allValueSet);
onChange(newValue);
};

return (
<Stack gap={3}>
<Group justify="space-between">
Expand Down Expand Up @@ -93,6 +132,17 @@ export const MultiSelectWidget = ({
searchValue={keyword}
onSearch={setKeyword}
filterOption={false}
dropdownRender={(menu) => (
<>
{allValueSet.size > 0 && !keyword && (
<Group px="xs" py="xs" onClick={toggleSelectAll} styles={DropdownHeaderStyles}>
<Checkbox size="xs" checked={allSelected} onChange={_.noop} label={t('common.actions.select_all')} />
</Group>
)}
<Divider />
{menu}
</>
)}
>
{filteredOptions.map((o) => (
<Option key={o.value} title={o.label}>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@devtable/root",
"version": "14.10.0",
"version": "14.11.0",
"private": true,
"workspaces": [
"api",
Expand Down
2 changes: 1 addition & 1 deletion settings-form/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@devtable/settings-form",
"version": "14.10.0",
"version": "14.11.0",
"license": "Apache-2.0",
"publishConfig": {
"access": "public",
Expand Down
2 changes: 1 addition & 1 deletion website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@devtable/website",
"private": true,
"license": "Apache-2.0",
"version": "14.10.0",
"version": "14.11.0",
"scripts": {
"dev": "vite",
"preview": "vite preview"
Expand Down