-
-
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.
- Loading branch information
Showing
26 changed files
with
402 additions
and
97 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.
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> | ||
); | ||
} |
77 changes: 77 additions & 0 deletions
77
docs/data/date-pickers/custom-field/behavior-button/MaterialDatePicker.tsx
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,77 @@ | ||
import * as React from 'react'; | ||
import { Dayjs } from 'dayjs'; | ||
import Button from '@mui/material/Button'; | ||
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs'; | ||
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider'; | ||
import { | ||
DatePicker, | ||
DatePickerProps, | ||
DatePickerFieldProps, | ||
} 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: DatePickerFieldProps<Dayjs>) { | ||
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: React.UIEvent) => { | ||
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: DatePickerProps<Dayjs>) { | ||
return ( | ||
<DatePicker {...props} slots={{ ...props.slots, field: ButtonDateField }} /> | ||
); | ||
} | ||
|
||
export default function MaterialDatePicker() { | ||
return ( | ||
<LocalizationProvider dateAdapter={AdapterDayjs}> | ||
<ButtonFieldDatePicker /> | ||
</LocalizationProvider> | ||
); | ||
} |
1 change: 1 addition & 0 deletions
1
docs/data/date-pickers/custom-field/behavior-button/MaterialDatePicker.tsx.preview
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 @@ | ||
<ButtonFieldDatePicker /> |
82 changes: 82 additions & 0 deletions
82
docs/data/date-pickers/custom-field/behavior-button/MaterialDateRangePicker.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,82 @@ | ||
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 { DateRangePicker } from '@mui/x-date-pickers-pro/DateRangePicker'; | ||
import { useValidation } from '@mui/x-date-pickers/validation'; | ||
import { validateDateRange } from '@mui/x-date-pickers-pro/validation'; | ||
import { | ||
useSplitFieldProps, | ||
useParsedFormat, | ||
usePickersContext, | ||
} from '@mui/x-date-pickers/hooks'; | ||
|
||
function ButtonDateRangeField(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: validateDateRange, | ||
value, | ||
timezone, | ||
props: internalProps, | ||
}); | ||
|
||
const handleTogglePicker = (event) => { | ||
if (pickersContext.open) { | ||
pickersContext.onClose(event); | ||
} else { | ||
pickersContext.onOpen(event); | ||
} | ||
}; | ||
|
||
const formattedValue = (value ?? [null, null]) | ||
.map((date) => (date == null ? parsedFormat : date.format(format))) | ||
.join(' – '); | ||
|
||
return ( | ||
<Button | ||
{...other} | ||
variant="outlined" | ||
color={hasValidationError ? 'error' : 'primary'} | ||
ref={InputProps?.ref} | ||
onClick={handleTogglePicker} | ||
> | ||
{label ? `${label}: ${formattedValue}` : formattedValue} | ||
</Button> | ||
); | ||
} | ||
|
||
// TODO v8: Will be removed before the end of the alpha since single input will become the default field. | ||
ButtonDateRangeField.fieldType = 'single-input'; | ||
|
||
function ButtonFieldDateRangePicker(props) { | ||
return ( | ||
<DateRangePicker | ||
{...props} | ||
slots={{ ...props.slots, field: ButtonDateRangeField }} | ||
/> | ||
); | ||
} | ||
|
||
export default function MaterialDateRangePicker() { | ||
return ( | ||
<LocalizationProvider dateAdapter={AdapterDayjs}> | ||
<ButtonFieldDateRangePicker /> | ||
</LocalizationProvider> | ||
); | ||
} |
86 changes: 86 additions & 0 deletions
86
docs/data/date-pickers/custom-field/behavior-button/MaterialDateRangePicker.tsx
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,86 @@ | ||
import * as React from 'react'; | ||
import { Dayjs } from 'dayjs'; | ||
import Button from '@mui/material/Button'; | ||
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs'; | ||
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider'; | ||
import { | ||
DateRangePicker, | ||
DateRangePickerProps, | ||
DateRangePickerFieldProps, | ||
} from '@mui/x-date-pickers-pro/DateRangePicker'; | ||
import { useValidation } from '@mui/x-date-pickers/validation'; | ||
import { validateDateRange } from '@mui/x-date-pickers-pro/validation'; | ||
import { | ||
useSplitFieldProps, | ||
useParsedFormat, | ||
usePickersContext, | ||
} from '@mui/x-date-pickers/hooks'; | ||
|
||
function ButtonDateRangeField(props: DateRangePickerFieldProps<Dayjs>) { | ||
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: validateDateRange, | ||
value, | ||
timezone, | ||
props: internalProps, | ||
}); | ||
|
||
const handleTogglePicker = (event: React.UIEvent) => { | ||
if (pickersContext.open) { | ||
pickersContext.onClose(event); | ||
} else { | ||
pickersContext.onOpen(event); | ||
} | ||
}; | ||
|
||
const formattedValue = (value ?? [null, null]) | ||
.map((date) => (date == null ? parsedFormat : date.format(format))) | ||
.join(' – '); | ||
|
||
return ( | ||
<Button | ||
{...other} | ||
variant="outlined" | ||
color={hasValidationError ? 'error' : 'primary'} | ||
ref={InputProps?.ref} | ||
onClick={handleTogglePicker} | ||
> | ||
{label ? `${label}: ${formattedValue}` : formattedValue} | ||
</Button> | ||
); | ||
} | ||
|
||
// TODO v8: Will be removed before the end of the alpha since single input will become the default field. | ||
ButtonDateRangeField.fieldType = 'single-input'; | ||
|
||
function ButtonFieldDateRangePicker(props: DateRangePickerProps<Dayjs>) { | ||
return ( | ||
<DateRangePicker | ||
{...props} | ||
slots={{ ...props.slots, field: ButtonDateRangeField }} | ||
/> | ||
); | ||
} | ||
|
||
export default function MaterialDateRangePicker() { | ||
return ( | ||
<LocalizationProvider dateAdapter={AdapterDayjs}> | ||
<ButtonFieldDateRangePicker /> | ||
</LocalizationProvider> | ||
); | ||
} |
Oops, something went wrong.