Skip to content

Commit

Permalink
feat: added select/deselect all button to column selection
Browse files Browse the repository at this point in the history
  • Loading branch information
schummar committed Jun 1, 2023
1 parent 51ddeb6 commit 481f34d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
4 changes: 2 additions & 2 deletions docs/stories/aIntro.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default {
table: { disable: true },
},
fullWidth: {
defaultValue: true,
defaultValue: 'left',
options: [true, false, 'left', 'right'],
control: { type: 'inline-radio' },
},
Expand All @@ -31,7 +31,7 @@ export const Primary = {
virtual: true,
enableExport: true,
stickyHeader: true,
fullWidth: true,
fullWidth: 'left',
},
};

Expand Down
30 changes: 23 additions & 7 deletions src/components/columnSelection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export function ColumnSelection<T>(): JSX.Element {
const Settings = useTheme((t) => t.icons.Settings);
const selectColumns = useTheme((t) => t.text.selectColumns);
const resetAll = useTheme((t) => t.text.resetAll);
const showAll = useTheme((t) => t.text.showAllColumns);
const hideAll = useTheme((t) => t.text.hideAllColumns);
const classes = useTheme((t) => t.classes);
const cssVariables = useCssVariables();

Expand All @@ -23,14 +25,20 @@ export function ColumnSelection<T>(): JSX.Element {

const [anchorElement, setAnchorElement] = useState<Element | null>(null);

const toggle = (column: InternalColumn<T, unknown>) => {
const toggle = (column?: InternalColumn<T, unknown>, state?: boolean) => {
const {
props: { hiddenColumns: controlledHiddenColumns, onHiddenColumnsChange },
} = table.getState();

const newValue = new Set(hiddenColumns);
if (hiddenColumns.has(column.id)) newValue.delete(column.id);
else newValue.add(column.id);

for (const columnId of column ? [column.id] : columns.map((column) => column.id)) {
if (state ?? hiddenColumns.has(columnId)) {
newValue.delete(columnId);
} else {
newValue.add(columnId);
}
}

if (!controlledHiddenColumns) {
table.update((state) => {
Expand All @@ -41,6 +49,8 @@ export function ColumnSelection<T>(): JSX.Element {
onHiddenColumnsChange?.(newValue);
};

const allVisible = columns.every((column) => !hiddenColumns.has(column.id));

return (
<>
<IconButton onClick={(event) => setAnchorElement(anchorElement ? null : event.currentTarget)}>
Expand All @@ -58,6 +68,16 @@ export function ColumnSelection<T>(): JSX.Element {
<div css={{ padding: `calc(var(--spacing) * 2)`, display: 'grid' }}>
<div css={{ marginBottom: 'var(--spacing)' }}>{selectColumns}</div>

<div css={{ margin: '0.5em 0', display: 'flex', gap: '0.5em' }}>
<Button variant="outlined" onClick={() => toggle(undefined, !allVisible)}>
{allVisible ? hideAll : showAll}
</Button>

<Button variant="outlined" onClick={reset}>
{resetAll}
</Button>
</div>

{columns.map((column) => (
<FormControlLabel
key={column.id}
Expand All @@ -71,10 +91,6 @@ export function ColumnSelection<T>(): JSX.Element {
label={column.header}
></FormControlLabel>
))}

<Button variant="outlined" onClick={reset}>
{resetAll}
</Button>
</div>
</Popover>
</>
Expand Down
2 changes: 2 additions & 0 deletions src/theme/defaultTheme/defaultTexts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type { TableTheme } from '../../types';

export const defaultTexts: TableTheme['text'] = {
selectColumns: 'Select visible columns',
showAllColumns: 'Show all',
hideAllColumns: 'Hide all',
noResults: 'No results',
exportTitle: 'Export',
exportCopy: 'To clipboard',
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export interface TableTheme<T = unknown> {
/** Define display texts. */
text: {
selectColumns: ReactNode;
showAllColumns: ReactNode;
hideAllColumns: ReactNode;
noResults: ReactNode;
exportTitle: ReactNode;
exportCopy: ReactNode;
Expand Down

0 comments on commit 481f34d

Please sign in to comment.