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

[docs] Apply the new DX to the Autocomplete Field demo #15165

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions docs/data/date-pickers/custom-field/PickerWithJoyField.tsx.preview

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,31 @@ import Stack from '@mui/material/Stack';
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 { useSplitFieldProps } from '@mui/x-date-pickers/hooks';
import { useValidation, validateDate } from '@mui/x-date-pickers/validation';

function AutocompleteField(props) {
const { internalProps, forwardedProps } = useSplitFieldProps(props, 'date');
const { value, timezone, onChange } = internalProps;
const {
InputProps,
slotProps,
slots,
ownerState,
label,
disabled,
readOnly,
id,
value,
onChange,
InputProps: { ref, startAdornment, endAdornment } = {},
inputProps,
focused,
name,
options = [],
} = props;
inputProps,
...other
} = forwardedProps;

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

const mergeAdornments = (...adornments) => {
const nonNullAdornments = adornments.filter((el) => el != null);
Expand All @@ -41,25 +53,24 @@ function AutocompleteField(props) {

return (
<Autocomplete
id={id}
{...other}
options={options}
disabled={disabled}
readOnly={readOnly}
ref={ref}
ref={InputProps?.ref}
sx={{ minWidth: 250 }}
renderInput={(params) => (
<TextField
{...params}
error={hasValidationError}
label={label}
inputProps={{ ...params.inputProps, ...inputProps }}
InputProps={{
...params.InputProps,
startAdornment: mergeAdornments(
startAdornment,
InputProps?.startAdornment,
params.InputProps.startAdornment,
),
endAdornment: mergeAdornments(
endAdornment,
InputProps?.endAdornment,
params.InputProps.endAdornment,
),
}}
Expand Down Expand Up @@ -107,7 +118,7 @@ function AutocompleteDatePicker(props) {

const today = dayjs().startOf('day');

export default function PickerWithAutocompleteField() {
export default function MaterialDatePicker() {
return (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<AutocompleteDatePicker
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,43 @@ import TextField from '@mui/material/TextField';
import Stack from '@mui/material/Stack';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { DatePicker, DatePickerProps } from '@mui/x-date-pickers/DatePicker';
import { UseDateFieldProps } from '@mui/x-date-pickers/DateField';
import {
BaseSingleInputFieldProps,
DateValidationError,
FieldSection,
} from '@mui/x-date-pickers/models';
DatePicker,
DatePickerFieldProps,
DatePickerProps,
} from '@mui/x-date-pickers/DatePicker';
import { useSplitFieldProps } from '@mui/x-date-pickers/hooks';
import { useValidation, validateDate } from '@mui/x-date-pickers/validation';

interface AutoCompleteFieldProps
extends UseDateFieldProps<Dayjs, true>,
BaseSingleInputFieldProps<
Dayjs | null,
Dayjs,
FieldSection,
true,
DateValidationError
> {
interface AutocompleteFieldProps extends DatePickerFieldProps<Dayjs> {
/**
* @typescript-to-proptypes-ignore
*/
options?: Dayjs[];
}

function AutocompleteField(props: AutoCompleteFieldProps) {
function AutocompleteField(props: AutocompleteFieldProps) {
const { internalProps, forwardedProps } = useSplitFieldProps(props, 'date');
const { value, timezone, onChange } = internalProps;
const {
InputProps,
slotProps,
slots,
ownerState,
label,
disabled,
readOnly,
id,
value,
onChange,
InputProps: { ref, startAdornment, endAdornment } = {},
inputProps,
focused,
name,
options = [],
} = props;
inputProps,
...other
} = forwardedProps;

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

const mergeAdornments = (...adornments: React.ReactNode[]) => {
const nonNullAdornments = adornments.filter((el) => el != null);
Expand All @@ -62,25 +64,24 @@ function AutocompleteField(props: AutoCompleteFieldProps) {

return (
<Autocomplete
id={id}
{...other}
options={options}
disabled={disabled}
readOnly={readOnly}
ref={ref}
ref={InputProps?.ref}
sx={{ minWidth: 250 }}
renderInput={(params) => (
<TextField
{...params}
error={hasValidationError}
label={label}
inputProps={{ ...params.inputProps, ...inputProps }}
InputProps={{
...params.InputProps,
startAdornment: mergeAdornments(
startAdornment,
InputProps?.startAdornment,
params.InputProps.startAdornment,
),
endAdornment: mergeAdornments(
endAdornment,
InputProps?.endAdornment,
params.InputProps.endAdornment,
),
}}
Expand Down Expand Up @@ -127,7 +128,7 @@ function AutocompleteDatePicker(props: AutocompleteDatePickerProps) {
);

return (
<DatePicker<Dayjs>
<DatePicker
slots={{ ...props.slots, field: AutocompleteField }}
slotProps={{ ...props.slotProps, field: { options } as any }}
shouldDisableDate={(date) => !optionsLookup[date.startOf('day').toISOString()]}
Expand All @@ -138,7 +139,7 @@ function AutocompleteDatePicker(props: AutocompleteDatePickerProps) {

const today = dayjs().startOf('day');

export default function PickerWithAutocompleteField() {
export default function MaterialDatePicker() {
return (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<AutocompleteDatePicker
Expand Down
2 changes: 1 addition & 1 deletion docs/data/date-pickers/custom-field/custom-field.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ The new accessible DOM structure will become compatible with Joy UI in the futur

If your user can only select a value in a small list of available dates, you can replace the field with the [Autocomplete](/material-ui/react-autocomplete/) component to list those dates:

{{"demo": "PickerWithAutocompleteField.js", "defaultCodeOpen": false}}
{{"demo": "behavior-autocomplete/MaterialDatePicker.js", "defaultCodeOpen": false}}

### Using a masked Text Field

Expand Down
Loading