-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
[test] Test the validation before saving a value #2087
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should GRID_CELL_EDIT_PROPS_CHANGE_COMMITTED
fire if there is an error or not? The name of the event is weird. It says committed even if it wasn't yet or might not be.
70cadf6
to
ed7d333
Compare
@oliviertassinari True, In #1955 I changed all edit components to call the API directly, but by doing that the events would not be published and developers couldn't stop the propagation. It also broke https://master--material-ui-x.netlify.app/storybook/?path=/story/x-grid-tests-rows--validate-edit-value-server-side&globals=measureEnabled:false. To fix this, I updated
We can open another PR to rename |
572dd69
to
b356a75
Compare
@@ -137,14 +130,15 @@ export function useGridEditRows(apiRef: GridApiRef) { | |||
[options.isCellEditable], | |||
); | |||
|
|||
const setEditCellProps = React.useCallback( | |||
const changeCellEditProps = React.useCallback( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
setEditCellProps
, changeCellEditProps
=> pretty confusing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, the names are a bit weird, we can discuss. Before #1824, some components were directly publishing GRID_CELL_EDIT_PROPS_CHANGE
, but that is not good because TypeScript can't suggested the arguments. This new method is just a helper to publish the event. The actual logic of updating the state is in setEditCellProps
. I can't just call setEditCellProps
because the prop won't be called.
packages/grid/_modules_/grid/hooks/features/rows/useGridEditRows.ts
Outdated
Show resolved
Hide resolved
@@ -25,7 +25,7 @@ export function GridEditInputCell(props: GridCellParams & InputBaseProps) { | |||
const newValue = event.target.value; | |||
const editProps = { value: newValue }; | |||
setValueState(newValue); | |||
api.setEditCellProps({ id, field, props: editProps }); | |||
api.changeCellEditProps({ id, field, props: editProps }, event); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't get the purpose of this new method
|
||
const cellCommitParams = apiRef.current.getEditCellPropsParams(params.id, params.field); | ||
if (!cellCommitParams.props.error) { | ||
// We commit the change when there is no error |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how are you handling this now? error was a sort of predefined prop
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
commitCellChange
now checks if there's an error. If there's no error, it publishes GRID_CELL_EDIT_PROPS_CHANGE_COMMITTED
. The handler of this event is who updates the row.
@@ -168,6 +163,16 @@ export function useGridEditRows(apiRef: GridApiRef) { | |||
[apiRef, forceUpdate, logger, setGridState], | |||
); | |||
|
|||
const handleCellEditPropsChange = React.useCallback( | |||
(params: GridEditCellPropsParams, event?: React.SyntheticEvent) => { | |||
if (event?.isPropagationStopped()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is that not handled in the publishEvent?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, publishEvent
prevents from publishing an event if the given event object has the propagation stopped. Once the event is published, it will call all the listeners. If the user wants to stop the propagation of an event in onEditCellChange
, we need to check by hand if the propagation was stopped.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes so one way to do it, is to override the emit behavior of the event emitter so we avoid this check everywhere 🤔
@@ -333,10 +355,11 @@ export function useGridEditRows(apiRef: GridApiRef) { | |||
getCellMode, | |||
isCellEditable, | |||
commitCellChange, | |||
setEditCellProps, | |||
setEditCellProps, // TODO don't expose, update the editRowsModel prop directly |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why this comment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To reduce confusion between setEditCellValue
. In edit components, the current value is only thing that needs to be updated in the majority of the cases. Outside an edit components, developers can control the state with the editRowsModel
prop to set the error or other prop.
@dtassone We discussed to create another GRID_CELL_EDIT_VALUE_CHANGE event that would be fired by setEditCellValue. I chose to not do it here because GRID_CELL_EDIT_PROPS_CHANGE has only one listener, so it's already this new event. Also, this PR was created just to improve the error validation and to fix a regression. We can go back to it in the useGridControlState's PR. About the TODO to not expose |
@m4theushw To me it wasn't obvious. In the documentation we use the event to listen to the action that signals that the end-user is done (enter, click away, etc.), we even suggest that validation should be done after. From what I understand we have no documentation for using |
@oliviertassinari That's wrong. Although we say to do the validation after, the demo is doing the validation before, while the user is changing the value. Note that we use the
Now we have documentation about
Based on the new docs what do you think we can remove? The |
@m4theushw I didn't saw #2143 when I commented. We are good |
I just noticed that when calling
commitCellChange
we're not checking if there's an error. This could cause the invalid value being commited when it shouldn't. This situation may occur with custom edit components.