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] Enable using non-native Select in filter panel #4361

Merged
merged 6 commits into from
Apr 11, 2022

Conversation

kyeongsoosoo
Copy link
Contributor

@kyeongsoosoo kyeongsoosoo commented Apr 4, 2022

Preview: https://codesandbox.io/s/columntypesgrid-material-demo-forked-ll8bmp?file=/demo.tsx

Closes #4127

This PR resolves the issue that Select component in Filter Panel only uses native UI.

Following the suggestion, I made 3 changes.

  1. ClickAwayListener in Filter Panel uses onMouseUp mouseEvent.
<ClickAwayListener mouseEvent="onMouseUp" onClickAway={handleClickAway}>
  1. When rootProps.componentsProps?.baseSelect.native is set to false, baseSelect component uses MUI component and MenuItem component is used instead of option tag.

  2. rootProps.componentsProps?.baseSelect is passed to InputComponent when Select Component is used.

      SelectProps={{
        native: isSelectNative,
        ...rootProps.componentsProps?.baseSelect,
      }}

NOTE. To avoid breaking change, I set default native to true.

Before

2022-04-05.12.59.42.mov

After

2022-04-05.12.25.22.mov

@mui-bot
Copy link

mui-bot commented Apr 4, 2022

These are the results for the performance tests:

Test case Unit Min Max Median Mean σ
Filter 100k rows ms 230.9 435.5 404.9 346.54 94.194
Sort 100k rows ms 449.8 964.8 711.5 691.28 168.064
Select 100k rows ms 128.4 289.5 134.8 175.36 62.296
Deselect 100k rows ms 96.6 298.9 250.1 190.82 75.948

Generated by 🚫 dangerJS against 3be4afd

Copy link
Member

@alexfauquette alexfauquette left a comment

Choose a reason for hiding this comment

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

Supporting both native and non-native select seems to be a pain due to the switch between <option /> and <MenuItem />

What would you think about adding a helper component to switch between them more easily

const SelectOption = ({ isNative, ...props }) => isNative ? <option {...props} /> : <MenuItem {...props} />

@@ -137,6 +137,7 @@ function GridEditSingleSelectCell(props: GridRenderEditCellParams & Omit<SelectP
fullWidth
{...other}
{...rootProps.componentsProps?.baseSelect}
native={false}
Copy link
Member

Choose a reason for hiding this comment

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

That's the default value. I do not think users prop should be overridden

A solution could be to send a warning if other.native || rootProps.componentsProps?.baseSelect?.native explaining that EditSingleSelectCell does not support native select

Here is an example of a warning message:

if (process.env.NODE_ENV !== 'production') {
if (!columnTypeWarnedOnce && !columnTypes[type]) {
console.warn(
[
`MUI: The column type "${type}" you are using is not supported.`,
`Column type "string" is being used instead.`,
].join('\n'),
);
columnTypeWarnedOnce = true;
}
}

Another solution is to support native select by adding an argument isNative to renderSingleSelectOptions such that it can switch between <MenuItem/> and <option />

Copy link
Member

Choose a reason for hiding this comment

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

Another solution is to support native select by adding an argument isNative to renderSingleSelectOptions such that it can switch between and

+1 for this option. It will be odd to have all selects working with native=false and only this one that doesn't.

Copy link
Contributor Author

@kyeongsoosoo kyeongsoosoo Apr 5, 2022

Choose a reason for hiding this comment

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

I chose the latter solution(support native select).
I tested the change using Storybook, and I found that NativeSelect casts the value to a string.
To fix that issue, I copied the util getValueFromValueOptions from filterPanelUtils and formatted the value.

  const handleChange = async (event: React.KeyboardEvent<HTMLInputElement>) => {
    setOpen(false);
    const target = event.target as HTMLInputElement;
    // NativeSelect casts the value to a string.
    const formattedTargetValue = getValueFromValueOptions(target.value, valueOptionsFormatted);
    const isValid = await api.setEditCellValue({ id, field, value: formattedTargetValue }, event);
    ...

@alexfauquette alexfauquette requested a review from m4theushw April 5, 2022 09:09
Copy link
Member

@m4theushw m4theushw left a comment

Choose a reason for hiding this comment

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

Good job! I only think we could go one step further and reduce the amount of code duplicated. Sometimes only one or two props change.

))}
{linkOperators.map((linkOperator) =>
isBaseSelectNative ? (
<option key={linkOperator.toString()} value={linkOperator.toString()}>
Copy link
Member

Choose a reason for hiding this comment

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

The props are the same for MenuItem and option. You could store in an OptionComponent variable which option component to use, then do:

const OptionComponent = ...

linkOperators.map((linkOperator) => (
  <OptionComponent key={...} value={...} />
))

Same for the other components. This reduce code duplication.

@m4theushw m4theushw added component: data grid This is the name of the generic UI component, not the React module! new feature New feature or request labels Apr 5, 2022
@kyeongsoosoo
Copy link
Contributor Author

@alexfauquette @m4theushw
Thanks for the review. I refactored some code following the suggestions.

Copy link
Member

@alexfauquette alexfauquette left a comment

Choose a reason for hiding this comment

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

Changes look nice. Maybe putting the SelectOption in a dedicated file to avoid duplicated code, and for me it will be good.

Comment on lines 106 to 121
interface SelectOptionProps {
isNative: boolean;
children: React.ReactNode;
value: string;
}

const SelectOption = ({ isNative, children, value, ...props }: SelectOptionProps) =>
isNative ? (
<option value={value} {...props}>
{children}
</option>
) : (
<MenuItem value={value} {...props}>
{children}
</MenuItem>
);
Copy link
Member

Choose a reason for hiding this comment

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

Since this code is duplicated, maybe a dedicated file components/SelectOption.tsx could be more appropriate

@alexfauquette alexfauquette requested a review from m4theushw April 6, 2022 10:38
Copy link
Member

@m4theushw m4theushw left a comment

Choose a reason for hiding this comment

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

I pushed a4110eb simplifying the way we choose which component to use for the options. I think we were duplicating too much code. What do you think now?

@@ -12,17 +12,21 @@ import { useGridRootProps } from '../../hooks/utils/useGridRootProps';
import { GridEditModes } from '../../models/gridEditRowModel';
import { GridEvents } from '../../models/events/gridEvents';
import { GridColDef, ValueOptions } from '../../models/colDef/gridColDef';
import { getValueFromValueOptions } from './editCellUtils';
Copy link
Member

Choose a reason for hiding this comment

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

Could we reuse the functions from filterPanelUtils.ts?

@m4theushw m4theushw requested a review from alexfauquette April 7, 2022 16:13
@kyeongsoosoo
Copy link
Contributor Author

It looks simpler than my first version.
Thanks for a better solution.

Copy link
Member

@alexfauquette alexfauquette left a comment

Choose a reason for hiding this comment

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

I added a commit to change import helpers from the single select

Just spot a typo, otherwise it looks good

Co-authored-by: Alexandre Fauquette <45398769+alexfauquette@users.noreply.github.com>
@m4theushw m4theushw changed the title [DataGrid] Enable using MUI Select Component in Filter Panel [DataGrid] Enable using non-native Select in filter panel Apr 8, 2022
@m4theushw m4theushw merged commit a38c154 into mui:master Apr 11, 2022
alexfauquette pushed a commit to alexfauquette/mui-x that referenced this pull request Aug 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: data grid This is the name of the generic UI component, not the React module! new feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[DataGrid] Use non-native Select component to data grid filter panel
4 participants