Skip to content

Commit

Permalink
Add onOpen prop
Browse files Browse the repository at this point in the history
  • Loading branch information
flaviendelangle committed Sep 13, 2024
1 parent 92da0c7 commit b79b5f4
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import TextField from '@mui/material/TextField';

function ReadonlyDateField(props) {
const { internalProps, forwardedProps } = useSplitFieldProps(props, 'date');
const { value, timezone, format } = internalProps;
const { value, timezone, format, onOpen } = internalProps;
const { InputProps, slotProps, slots, ...other } = forwardedProps;

const placeholder = useFieldPlaceholder(internalProps);
Expand All @@ -28,6 +28,7 @@ function ReadonlyDateField(props) {
placeholder={placeholder}
InputProps={{ ...InputProps, readOnly: true }}
error={hasValidationError}
onClick={onOpen}
/>
);
}
Expand All @@ -38,7 +39,7 @@ function ReadonlyFieldDatePicker(props) {
);
}

export default function CustomReadonlyBehaviorMaterialTextField() {
export default function ReadonlyMaterialTextField() {
return (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<ReadonlyFieldDatePicker />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import TextField from '@mui/material/TextField';

function ReadonlyDateField(props: DateFieldInPickerProps<Dayjs, false>) {
const { internalProps, forwardedProps } = useSplitFieldProps(props, 'date');
const { value, timezone, format } = internalProps;
const { value, timezone, format, onOpen } = internalProps;
const { InputProps, slotProps, slots, ...other } = forwardedProps;

const placeholder = useFieldPlaceholder(internalProps);
Expand All @@ -28,6 +28,7 @@ function ReadonlyDateField(props: DateFieldInPickerProps<Dayjs, false>) {
placeholder={placeholder}
InputProps={{ ...InputProps, readOnly: true }}
error={hasValidationError}
onClick={onOpen}
/>
);
}
Expand All @@ -38,7 +39,7 @@ function ReadonlyFieldDatePicker(props: DatePickerProps<Dayjs>) {
);
}

export default function CustomReadonlyBehaviorMaterialTextField() {
export default function ReadonlyMaterialTextField() {
return (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<ReadonlyFieldDatePicker />
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 @@ -153,7 +153,7 @@ Learn more about the accessible DOM structure and its difference compared to the

### Using a read-only `TextField`

{{"demo": "CustomReadonlyBehaviorMaterialTextField.js", "defaultCodeOpen": false}}
{{"demo": "custom-behavior/ReadonlyMaterialTextField.js", "defaultCodeOpen": false}}

### Using an `Autocomplete`

Expand Down
1 change: 1 addition & 0 deletions packages/x-date-pickers/src/hooks/useSplitFieldProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const SHARED_FIELD_INTERNAL_PROP_NAMES = [
'disabled',
'readOnly',
'dateSeparator',
'onOpen',
] as const;

type InternalPropNames<TValueType extends FieldValueType> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ export const useDesktopPicker = <
timezone,
label,
name,
onOpen: actions.onOpen,
autoFocus: autoFocus && !props.open,
focused: open ? true : undefined,
...(inputRef ? { inputRef } : {}),
Expand Down
1 change: 1 addition & 0 deletions packages/x-date-pickers/src/internals/models/fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ export interface BaseFieldProps<
format?: string;
disabled?: boolean;
ref?: React.Ref<HTMLDivElement>;
onOpen: (event: React.UIEvent) => void;
}

0 comments on commit b79b5f4

Please sign in to comment.