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

[DataGrid] Grid depends on side effects #666

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
3 changes: 2 additions & 1 deletion docs/babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ module.exports = {
env: {
production: {
plugins: [
'@babel/plugin-transform-react-constant-elements',
// TODO fix useGridSelector side effect and enable back.
// '@babel/plugin-transform-react-constant-elements',
['babel-plugin-react-remove-properties', { properties: ['data-mui-test'] }],
['babel-plugin-transform-react-remove-prop-types', { mode: 'remove' }],
],
Expand Down
2 changes: 1 addition & 1 deletion packages/grid/_modules_/grid/components/column-headers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ColumnHeaderItem } from './column-header-item';
import { ApiContext } from './api-context';
import { LeftEmptyCell, RightEmptyCell } from './cell';
import { GridColumnHeaderMenu } from './menu/columnMenu/GridColumnHeaderMenu';
import { PreferencesPanel } from './tools/Preferences';
import { PreferencesPanel } from './tools/PreferencesPanel';
import { containerSizesSelector } from './viewport';
import { OptionsContext } from './options-context';
import { ScrollArea } from './ScrollArea';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { SortMenuItems } from './SortMenuItems';

const columnMenuStateSelector = (state: GridState) => state.columnMenu;

export const GridColumnHeaderMenu: React.FC<{}> = () => {
export function GridColumnHeaderMenu() {
const apiRef = React.useContext(ApiContext);
const columnMenuState = useGridSelector(apiRef!, columnMenuStateSelector);
const currentColumn = columnMenuState.field
Expand Down Expand Up @@ -74,4 +74,4 @@ export const GridColumnHeaderMenu: React.FC<{}> = () => {
<HideColMenuItem onClick={hideMenu} column={currentColumn!} />
</GridMenu>
);
};
}
40 changes: 21 additions & 19 deletions packages/grid/_modules_/grid/components/tools/ColumnsPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,29 @@ import { optionsSelector } from '../../hooks/utils/useOptionsProp';
import { ApiContext } from '../api-context';
import { DragIcon } from '../icons/index';

const useStyles = makeStyles(() => ({
columnsListContainer: {
paddingTop: 8,
paddingLeft: 12,
},
column: {
display: 'flex',
justifyContent: 'space-between',
padding: '2px 4px',
},
switch: {
marginRight: 4,
},
dragIconRoot: {
justifyContent: 'flex-end',
const useStyles = makeStyles(
{
columnsListContainer: {
paddingTop: 8,
paddingLeft: 12,
},
column: {
display: 'flex',
justifyContent: 'space-between',
padding: '2px 4px',
},
switch: {
marginRight: 4,
},
dragIconRoot: {
justifyContent: 'flex-end',
},
},
}));
{ name: 'MuiDataGridColumnsPanel' },
);

export const ColumnsPanel: React.FC<{}> = () => {
export function ColumnsPanel() {
const classes = useStyles();

const apiRef = React.useContext(ApiContext);
const searchInputRef = React.useRef<HTMLInputElement>(null);
const columns = useGridSelector(apiRef, allColumnsSelector);
Expand Down Expand Up @@ -139,4 +141,4 @@ export const ColumnsPanel: React.FC<{}> = () => {
</div>
</React.Fragment>
);
};
}
72 changes: 38 additions & 34 deletions packages/grid/_modules_/grid/components/tools/FilterForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,40 +23,44 @@ export interface FilterFormProps {
deleteFilter: (item: FilterItem) => void;
}

const useStyles = makeStyles(() => ({
root: {
display: 'flex',
justifyContent: 'space-around',
padding: 8,
},
linkOperatorSelect: {
width: 60,
},
columnSelect: {
width: 150,
},
operatorSelect: {
width: 120,
},
filterValueInput: {
width: 190,
},
closeIconRoot: {
flexShrink: 0,
justifyContent: 'flex-end',
},
}));
const useStyles = makeStyles(
() => ({
root: {
display: 'flex',
justifyContent: 'space-around',
padding: 8,
},
linkOperatorSelect: {
width: 60,
},
columnSelect: {
width: 150,
},
operatorSelect: {
width: 120,
},
filterValueInput: {
width: 190,
},
closeIconRoot: {
flexShrink: 0,
justifyContent: 'flex-end',
},
}),
{ name: 'MuiDataGridFilterForm' },
);

export const FilterForm: React.FC<FilterFormProps> = ({
item,
hasMultipleFilters,
deleteFilter,
applyFilterChanges,
multiFilterOperator,
showMultiFilterOperators,
disableMultiFilterOperator,
applyMultiFilterOperatorChanges,
}) => {
export function FilterForm(props: FilterFormProps) {
const {
item,
hasMultipleFilters,
deleteFilter,
applyFilterChanges,
multiFilterOperator,
showMultiFilterOperators,
disableMultiFilterOperator,
applyMultiFilterOperatorChanges,
} = props;
const classes = useStyles();
const apiRef = React.useContext(ApiContext);
const filterableColumns = useGridSelector(apiRef, filterableColumnsSelector);
Expand Down Expand Up @@ -199,4 +203,4 @@ export const FilterForm: React.FC<FilterFormProps> = ({
</FormControl>
</div>
);
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@ export interface TypeFilterInputValueProps extends FilterInputValueProps {
type?: 'text' | 'number' | 'date' | 'datetime-local';
}

export const FilterInputValue: React.FC<TypeFilterInputValueProps> = ({
item,
applyValue,
type,
}) => {
export function FilterInputValue(props: TypeFilterInputValueProps) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for a better typescript support, it would better to keep the type definition otherwise it might just see it as a function instead of a react functional component 🤔

Copy link
Member

@dtassone dtassone Dec 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    interface FunctionComponent<P = {}> {
        (props: PropsWithChildren<P>, context?: any): ReactElement<any, any> | null;
        propTypes?: WeakValidationMap<P>;
        contextTypes?: ValidationMap<any>;
        defaultProps?: Partial<P>;
        displayName?: string;
    }

Copy link
Member Author

@oliviertassinari oliviertassinari Dec 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe none of the added types of FunctionComponent are relevant:

I would be in favor of following @eps1lon's point in mui/material-ui#23634 (comment)

const { item, applyValue, type } = props;
const filterTimeout = React.useRef<any>();
const [filterValueState, setFilterValueState] = React.useState(item.value || '');
const [applying, setIsApplying] = React.useState(false);
Expand Down Expand Up @@ -57,4 +54,4 @@ export const FilterInputValue: React.FC<TypeFilterInputValueProps> = ({
}}
/>
);
};
}
4 changes: 2 additions & 2 deletions packages/grid/_modules_/grid/components/tools/FilterPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ApiContext } from '../api-context';
import { AddIcon } from '../icons/index';
import { FilterForm } from './FilterForm';

export const FilterPanel: React.FC<{}> = () => {
export function FilterPanel() {
const apiRef = React.useContext(ApiContext);
const [gridState] = useGridState(apiRef!);
const { disableMultipleColumnsFiltering } = useGridSelector(apiRef, optionsSelector);
Expand Down Expand Up @@ -74,4 +74,4 @@ export const FilterPanel: React.FC<{}> = () => {
)}
</React.Fragment>
);
};
}
6 changes: 4 additions & 2 deletions packages/grid/_modules_/grid/components/tools/Panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ const useStyles = makeStyles(
);

export interface PanelProps {
children?: React.ReactNode;
open: boolean;
}

export const Panel: React.FC<PanelProps> = ({ children, open }) => {
export function Panel(props: PanelProps) {
const { children, open } = props;
const classes = useStyles();
const apiRef = React.useContext(ApiContext);
const viewportSizes = useGridSelector(apiRef, viewportSizeStateSelector);
Expand Down Expand Up @@ -80,4 +82,4 @@ export const Panel: React.FC<PanelProps> = ({ children, open }) => {
</ClickAwayListener>
</Popper>
);
};
}
2 changes: 1 addition & 1 deletion packages/grid/_modules_/grid/components/tools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ export * from './FilterInputValue';
export * from './FilterInputValueProps';
export * from './FilterPanel';
export * from './Panel';
export * from './Preferences';
export * from './PreferencesPanel';