Skip to content

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

Merged
flaviendelangle merged 17 commits intomui:masterfrom
flaviendelangle:clean-validation-api
Sep 11, 2024
Merged

[pickers] Allow custom fields to validate the value#14486
flaviendelangle merged 17 commits intomui:masterfrom
flaviendelangle:clean-validation-api

Conversation

@flaviendelangle
Copy link
Copy Markdown
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 scope: pickers Changes related to the date/time pickers. label Sep 4, 2024
@flaviendelangle flaviendelangle self-assigned this Sep 4, 2024
@mui-bot
Copy link
Copy Markdown

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
@github-actions
Copy link
Copy Markdown
Contributor

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
Copy Markdown
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
@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

scope: pickers Changes related to the date/time pickers.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[fields] Support validation on custom fields

3 participants