Skip to content

Commit

Permalink
Fix ColumnsButton doesn't honor omit prop
Browse files Browse the repository at this point in the history
  • Loading branch information
fzaninotto committed Oct 18, 2022
1 parent e961cea commit 02640c8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 18 deletions.
28 changes: 27 additions & 1 deletion packages/ra-ui-materialui/src/list/datagrid/ColumnsButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,29 @@ import ViewWeekIcon from '@mui/icons-material/ViewWeek';

import { FieldEditor } from './FieldEditor';

/**
* Render s a button that lets users show / hide columns in a configurable datagrid
*
* @example
* import { ColumnsButton, DatagridConfigurable } from 'react-admin';
*
* const PostListActions = () => (
* <TopToolbar>
<ColumnsButton />
<FilterButton />
* </TopToolbar>
* );
*
* const PostList = () => (
* <List actions={<PostListActions />}>
* <DatagridConfigurable>
* <TextField source="title" />
* <TextField source="author" />
...
* </DatagridConfigurable>
* </List>
* );
*/
export const ColumnsButton = props => {
const resource = useResourceContext(props);
const preferenceKey =
Expand All @@ -14,9 +37,12 @@ export const ColumnsButton = props => {
`${preferenceKey}.availableColumns`,
[]
);
const [omit] = useStore<string[]>(`${preferenceKey}.omit`, []);
const [columns, setColumns] = useStore<string[]>(
`${preferenceKey}.columns`,
availableColumns.map(column => column.source)
availableColumns
.map(column => column.source)
.filter(source => !omit.includes(source))
);
const translate = useTranslate();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { DatagridEditor } from './DatagridEditor';

export const DatagridConfigurable = ({
preferenceKey,
omit: omitFromProps,
...props
}: DatagridConfigurableProps) => {
if (props.optimized) {
Expand All @@ -19,6 +20,11 @@ export const DatagridConfigurable = ({
const [availableColumns, setAvailableColumns] = useStore<
{ source: string; label?: string }[]
>(`preferences.${finalPreferenceKey}.availableColumns`, []);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [_, setOmit] = useStore<string[]>(
`preferences.${finalPreferenceKey}.omit`,
omitFromProps
);

React.useEffect(() => {
if (availableColumns.length === 0) {
Expand All @@ -29,16 +35,13 @@ export const DatagridConfigurable = ({
: null
).filter(column => column != null);
setAvailableColumns(columns);
setOmit(omitFromProps);
}
}, [availableColumns]); // eslint-disable-line react-hooks/exhaustive-deps

return (
<Configurable
editor={
<DatagridEditor omit={props.omit}>
{props.children}
</DatagridEditor>
}
editor={<DatagridEditor>{props.children}</DatagridEditor>}
preferenceKey={finalPreferenceKey}
sx={{
display: 'block',
Expand All @@ -57,12 +60,14 @@ export type DatagridConfigurableProps = DatagridProps & {
omit?: string[];
};

const DatagridWithPreferences = ({
children,
omit,
...props
}: DatagridProps & { omit?: string[] }) => {
DatagridConfigurable.propTypes = Datagrid.propTypes;

/**
* This Datagrid filters its children depending on preferences
*/
const DatagridWithPreferences = ({ children, ...props }: DatagridProps) => {
const [availableColumns] = usePreference('availableColumns', []);
const [omit] = usePreference('omit', []);
const [columns] = usePreference(
'columns',
availableColumns
Expand All @@ -87,5 +92,3 @@ const DatagridWithPreferences = ({
</Datagrid>
);
};

DatagridConfigurable.propTypes = Datagrid.propTypes;
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@ import { Box, Button } from '@mui/material';

import { FieldEditor } from './FieldEditor';

export const DatagridEditor = (props: {
children: React.ReactNode;
omit?: string[];
}) => {
export const DatagridEditor = (props: { children: React.ReactNode }) => {
const translate = useTranslate();
useSetInspectorTitle('ra.inspector.datagrid', { _: 'Datagrid' });

const [availableColumns] = usePreference('availableColumns', []);
const [omit] = usePreference('omit', []);

const [columns, setColumns] = usePreference(
'columns',
availableColumns
.map(column => column.source)
.filter(name => !props.omit?.includes(name))
.filter(name => !omit?.includes(name))
);

const handleToggle = event => {
Expand Down

0 comments on commit 02640c8

Please sign in to comment.