-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[docs] Apply the new DX to the Button Field demos (#14860)
Signed-off-by: Michel Engelen <[email protected]> Signed-off-by: Gene Arch <[email protected]> Signed-off-by: Flavien DELANGLE <[email protected]> Co-authored-by: Michel Engelen <[email protected]> Co-authored-by: Olivier Tassinari <[email protected]> Co-authored-by: Gene Arch <[email protected]> Co-authored-by: Rom Grk <[email protected]> Co-authored-by: Lukas Tyla <[email protected]> Co-authored-by: Sycamore <[email protected]>
- Loading branch information
1 parent
365da92
commit bde9466
Showing
31 changed files
with
420 additions
and
346 deletions.
There are no files selected for viewing
72 changes: 0 additions & 72 deletions
72
docs/data/date-pickers/custom-field/DateRangePickerWithButtonField.js
This file was deleted.
Oops, something went wrong.
91 changes: 0 additions & 91 deletions
91
docs/data/date-pickers/custom-field/DateRangePickerWithButtonField.tsx
This file was deleted.
Oops, something went wrong.
11 changes: 0 additions & 11 deletions
11
docs/data/date-pickers/custom-field/DateRangePickerWithButtonField.tsx.preview
This file was deleted.
Oops, something went wrong.
59 changes: 0 additions & 59 deletions
59
docs/data/date-pickers/custom-field/PickerWithButtonField.js
This file was deleted.
Oops, something went wrong.
79 changes: 0 additions & 79 deletions
79
docs/data/date-pickers/custom-field/PickerWithButtonField.tsx
This file was deleted.
Oops, something went wrong.
5 changes: 0 additions & 5 deletions
5
docs/data/date-pickers/custom-field/PickerWithButtonField.tsx.preview
This file was deleted.
Oops, something went wrong.
73 changes: 73 additions & 0 deletions
73
docs/data/date-pickers/custom-field/behavior-button/MaterialDatePicker.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import * as React from 'react'; | ||
|
||
import Button from '@mui/material/Button'; | ||
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 { useValidation, validateDate } from '@mui/x-date-pickers/validation'; | ||
import { | ||
useSplitFieldProps, | ||
useParsedFormat, | ||
usePickersContext, | ||
} from '@mui/x-date-pickers/hooks'; | ||
|
||
function ButtonDateField(props) { | ||
const { internalProps, forwardedProps } = useSplitFieldProps(props, 'date'); | ||
const { value, timezone, format } = internalProps; | ||
const { | ||
InputProps, | ||
slotProps, | ||
slots, | ||
ownerState, | ||
label, | ||
focused, | ||
name, | ||
...other | ||
} = forwardedProps; | ||
|
||
const pickersContext = usePickersContext(); | ||
|
||
const parsedFormat = useParsedFormat(internalProps); | ||
const { hasValidationError } = useValidation({ | ||
validator: validateDate, | ||
value, | ||
timezone, | ||
props: internalProps, | ||
}); | ||
|
||
const handleTogglePicker = (event) => { | ||
if (pickersContext.open) { | ||
pickersContext.onClose(event); | ||
} else { | ||
pickersContext.onOpen(event); | ||
} | ||
}; | ||
|
||
const valueStr = value == null ? parsedFormat : value.format(format); | ||
|
||
return ( | ||
<Button | ||
{...other} | ||
variant="outlined" | ||
color={hasValidationError ? 'error' : 'primary'} | ||
ref={InputProps?.ref} | ||
onClick={handleTogglePicker} | ||
> | ||
{label ? `${label}: ${valueStr}` : valueStr} | ||
</Button> | ||
); | ||
} | ||
|
||
function ButtonFieldDatePicker(props) { | ||
return ( | ||
<DatePicker {...props} slots={{ ...props.slots, field: ButtonDateField }} /> | ||
); | ||
} | ||
|
||
export default function MaterialDatePicker() { | ||
return ( | ||
<LocalizationProvider dateAdapter={AdapterDayjs}> | ||
<ButtonFieldDatePicker /> | ||
</LocalizationProvider> | ||
); | ||
} |
Oops, something went wrong.