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

[pickers] Allow custom fields to validate the value #14486

Merged
merged 17 commits into from
Sep 11, 2024

Conversation

flaviendelangle
Copy link
Member

@flaviendelangle flaviendelangle commented Sep 4, 2024

Closes #14495

Changes

useValidation

  • No longer require to pass isSameError and defaultErrorState to useValidation, instead bind the valueManager direction to the validator (when using validateTime, we always need the isSameError and defaultErrorState of the singleItemValueManager
  • Move the timezone, the value and the onChange outside of the props object passed to the useValidation hook. We often override these properties (because we don't want the one in the prop, we want the one with a default value, the one of the internal state, etc...), we were often doing props: { ...props, timezone, value: state.value } which was very error prone. Now props only contains the validation props (minDate, ...), we could even consider renaming it.
  • Return additional information from useValidation (hasValidationError and getValidationErrorForNewValue) to make sure that people never need utils or valueManager to manage their error, useValidation should be sufficient.

Field components

  • Create new DateFieldInPickerProps, TimeFieldInPickerProps, etc... interfaces that simplifies dramatically how to type custom fields. They contain the props the field actually receives from the picker.

New DX

import { Dayjs } from 'dayjs';
import { useValidation, validateDate } from '@mui/x-date-pickers/validation';
import { DateFieldInPickerProps } from '@mui/x-date-pickers/DateField';
// Should be moved to a public endpoint
import { splitFieldInternalAndForwardedProps } from '@mui/x-date-pickers/internals';

function CustomField(props: DateFieldInPickerProps<Dayjs, false>) {
  const { internalProps, forwardedProps } = splitFieldInternalAndForwardedProps(
    props,
    'date',
  );

  const { value, timezone, onError } = internalProps;

  const { hasValidationError } = useValidation({
    validator: validateDate,
    value,
    timezone,
    onError,
    props: internalProps,
  });

  // Return the desired UI
  return <SomeComponent {...forwardedProps} />
}

And if you want to

Demo

Extracted PRs

Follow up

  • Build a public demo of a read-only field
  • Build a public demo of a masked field ([docs] Create Pickers masked field recipe #13515)
  • Apply the improvements to all the custom field demos (add support for validation to autocomplete demo, use the simplified typing everywhere, ...)

@flaviendelangle flaviendelangle added the component: pickers This is the name of the generic UI component, not the React module! label Sep 4, 2024
@flaviendelangle flaviendelangle self-assigned this Sep 4, 2024
@mui-bot
Copy link

mui-bot commented Sep 4, 2024

@flaviendelangle flaviendelangle changed the title [pickers] Clean the validation internals DX to prepare for publication [pickers] Clean and publish the validation internals Sep 4, 2024
@flaviendelangle flaviendelangle force-pushed the clean-validation-api branch 6 times, most recently from 18b2e88 to 2765a2e Compare September 4, 2024 11:46
@flaviendelangle flaviendelangle changed the title [pickers] Clean and publish the validation internals [pickers] Allow custom fields to validate the value Sep 4, 2024
@flaviendelangle flaviendelangle force-pushed the clean-validation-api branch 2 times, most recently from aed4748 to 0653dc3 Compare September 4, 2024 14:26
@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Sep 9, 2024
Copy link

github-actions bot commented Sep 9, 2024

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Sep 9, 2024
Copy link
Member

@LukasTy LukasTy left a comment

Choose a reason for hiding this comment

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

Great work! 👍
This is a masive improvement! 💙 💯
The new custom field DX looks awesome. 🙌

@flaviendelangle flaviendelangle merged commit c4004c2 into mui:master Sep 11, 2024
18 checks passed
@flaviendelangle flaviendelangle deleted the clean-validation-api branch September 11, 2024 11:36
@LukasTy LukasTy mentioned this pull request Sep 12, 2024
arthurbalduini pushed a commit to arthurbalduini/mui-x that referenced this pull request Sep 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: pickers This is the name of the generic UI component, not the React module!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[fields] Support validation on custom fields
3 participants