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] Required field does not work as expected for DatePicker #11509

Open
saewitz opened this issue Dec 24, 2023 · 1 comment
Open

[pickers] Required field does not work as expected for DatePicker #11509

saewitz opened this issue Dec 24, 2023 · 1 comment
Labels
component: pickers This is the name of the generic UI component, not the React module! enhancement This is not a bug, nor a new feature

Comments

@saewitz
Copy link

saewitz commented Dec 24, 2023

Steps to reproduce

Thanks for helping with this library!

https://stackblitz.com/edit/react-zy9jcg?file=Demo.tsx

Steps:

  1. Create a DatePicker and mark it as required
  2. Submit form without entering a value

The input should use the browser's built-in required field to note the field is required. Instead, it focuses the input and no note that the field is required.

This works as expected with an MUI TextField, and worked with DatePicker in mui-x v5.

Example code:

import * as React from 'react';
import { DemoContainer } from '@mui/x-date-pickers/internals/demo';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { DatePicker } from '@mui/x-date-pickers/DatePicker';
import TextField from '@mui/material/TextField/TextField';

export default function BasicDatePicker() {
  return (
    <LocalizationProvider dateAdapter={AdapterDayjs}>
      <DemoContainer components={['DatePicker']}>
        <form onSubmit={(event) => event.preventDefault()}>
          <TextField label="Text field" required />
          <DatePicker label="Date picker" slotProps={{ textField: { required: true } }}  />
          <button type="submit">Submit</button>
        </form>
      </DemoContainer>
    </LocalizationProvider>
  );
}

Search keywords: datepicker required

@saewitz saewitz added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Dec 24, 2023
@zannager zannager added the component: pickers This is the name of the generic UI component, not the React module! label Dec 25, 2023
@LukasTy
Copy link
Member

LukasTy commented Dec 27, 2023

Thank you for creating this issue @saewitz.
We already have an issue with adding the top-level required prop #7633 and it was discussed that some competitors (i.e. Kendo handles the required validation natively, even though their input has a present value).

I think that this topic is somewhat separate because it can be handled even now.
The only note is that it will probably not be possible to support this behavior with your example—the required prop would have to be defined on the field slot instead of textField to make this work. 😉

@LukasTy LukasTy added enhancement This is not a bug, nor a new feature and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Dec 27, 2023
@LukasTy LukasTy changed the title Required field does not work as expected for DatePicker [pickers] Required field does not work as expected for DatePicker Dec 27, 2023
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! enhancement This is not a bug, nor a new feature
Projects
None yet
Development

No branches or pull requests

3 participants