-
-
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.
Signed-off-by: Lukas <[email protected]> Co-authored-by: Nora <[email protected]>
- Loading branch information
1 parent
e9f90e2
commit 0ead122
Showing
7 changed files
with
191 additions
and
25 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
docs/data/date-pickers/custom-field/DateRangePickerWithButtonField.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,72 @@ | ||
import * as React from 'react'; | ||
|
||
import Button from '@mui/material/Button'; | ||
import useForkRef from '@mui/utils/useForkRef'; | ||
|
||
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'; | ||
|
||
const DateRangeButtonField = React.forwardRef((props, ref) => { | ||
const { | ||
setOpen, | ||
label, | ||
id, | ||
disabled, | ||
InputProps: { ref: containerRef } = {}, | ||
inputProps: { 'aria-label': ariaLabel } = {}, | ||
} = props; | ||
|
||
const handleRef = useForkRef(ref, containerRef); | ||
|
||
return ( | ||
<Button | ||
variant="outlined" | ||
id={id} | ||
disabled={disabled} | ||
ref={handleRef} | ||
aria-label={ariaLabel} | ||
onClick={() => setOpen?.((prev) => !prev)} | ||
> | ||
{label ? `Current date range: ${label}` : 'Pick a date range'} | ||
</Button> | ||
); | ||
}); | ||
|
||
DateRangeButtonField.fieldType = 'single-input'; | ||
|
||
const ButtonDateRangePicker = React.forwardRef((props, ref) => { | ||
const [open, setOpen] = React.useState(false); | ||
|
||
return ( | ||
<DateRangePicker | ||
slots={{ field: DateRangeButtonField, ...props.slots }} | ||
slotProps={{ field: { setOpen } }} | ||
ref={ref} | ||
{...props} | ||
open={open} | ||
onClose={() => setOpen(false)} | ||
onOpen={() => setOpen(true)} | ||
/> | ||
); | ||
}); | ||
|
||
export default function DateRangePickerWithButtonField() { | ||
const [value, setValue] = React.useState([null, null]); | ||
|
||
return ( | ||
<LocalizationProvider dateAdapter={AdapterDayjs}> | ||
<ButtonDateRangePicker | ||
label={ | ||
value[0] === null && value[1] === null | ||
? null | ||
: value | ||
.map((date) => (date ? date.format('MM/DD/YYYY') : 'null')) | ||
.join(' - ') | ||
} | ||
value={value} | ||
onChange={(newValue) => setValue(newValue)} | ||
/> | ||
</LocalizationProvider> | ||
); | ||
} |
91 changes: 91 additions & 0 deletions
91
docs/data/date-pickers/custom-field/DateRangePickerWithButtonField.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,91 @@ | ||
import * as React from 'react'; | ||
import { Dayjs } from 'dayjs'; | ||
import Button from '@mui/material/Button'; | ||
import useForkRef from '@mui/utils/useForkRef'; | ||
import { DateRange } from '@mui/x-date-pickers-pro'; | ||
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs'; | ||
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider'; | ||
import { | ||
DateRangePicker, | ||
DateRangePickerProps, | ||
} from '@mui/x-date-pickers-pro/DateRangePicker'; | ||
import { SingleInputDateRangeFieldProps } from '@mui/x-date-pickers-pro/SingleInputDateRangeField'; | ||
|
||
interface DateRangeButtonFieldProps extends SingleInputDateRangeFieldProps<Dayjs> { | ||
setOpen?: React.Dispatch<React.SetStateAction<boolean>>; | ||
} | ||
|
||
type DateRangeButtonFieldComponent = (( | ||
props: DateRangeButtonFieldProps & React.RefAttributes<HTMLDivElement>, | ||
) => React.JSX.Element) & { fieldType?: string }; | ||
|
||
const DateRangeButtonField = React.forwardRef( | ||
(props: DateRangeButtonFieldProps, ref: React.Ref<HTMLDivElement>) => { | ||
const { | ||
setOpen, | ||
label, | ||
id, | ||
disabled, | ||
InputProps: { ref: containerRef } = {}, | ||
inputProps: { 'aria-label': ariaLabel } = {}, | ||
} = props; | ||
|
||
const handleRef = useForkRef(ref, containerRef); | ||
|
||
return ( | ||
<Button | ||
variant="outlined" | ||
id={id} | ||
disabled={disabled} | ||
ref={handleRef} | ||
aria-label={ariaLabel} | ||
onClick={() => setOpen?.((prev) => !prev)} | ||
> | ||
{label ? `Current date range: ${label}` : 'Pick a date range'} | ||
</Button> | ||
); | ||
}, | ||
) as DateRangeButtonFieldComponent; | ||
|
||
DateRangeButtonField.fieldType = 'single-input'; | ||
|
||
const ButtonDateRangePicker = React.forwardRef( | ||
( | ||
props: Omit<DateRangePickerProps<Dayjs>, 'open' | 'onOpen' | 'onClose'>, | ||
ref: React.Ref<HTMLDivElement>, | ||
) => { | ||
const [open, setOpen] = React.useState(false); | ||
|
||
return ( | ||
<DateRangePicker | ||
slots={{ field: DateRangeButtonField, ...props.slots }} | ||
slotProps={{ field: { setOpen } as any }} | ||
ref={ref} | ||
{...props} | ||
open={open} | ||
onClose={() => setOpen(false)} | ||
onOpen={() => setOpen(true)} | ||
/> | ||
); | ||
}, | ||
); | ||
|
||
export default function DateRangePickerWithButtonField() { | ||
const [value, setValue] = React.useState<DateRange<Dayjs>>([null, null]); | ||
|
||
return ( | ||
<LocalizationProvider dateAdapter={AdapterDayjs}> | ||
<ButtonDateRangePicker | ||
label={ | ||
value[0] === null && value[1] === null | ||
? null | ||
: value | ||
.map((date) => (date ? date.format('MM/DD/YYYY') : 'null')) | ||
.join(' - ') | ||
} | ||
value={value} | ||
onChange={(newValue) => setValue(newValue)} | ||
/> | ||
</LocalizationProvider> | ||
); | ||
} |
11 changes: 11 additions & 0 deletions
11
docs/data/date-pickers/custom-field/DateRangePickerWithButtonField.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,11 @@ | ||
<ButtonDateRangePicker | ||
label={ | ||
value[0] === null && value[1] === null | ||
? null | ||
: value | ||
.map((date) => (date ? date.format('MM/DD/YYYY') : 'null')) | ||
.join(' - ') | ||
} | ||
value={value} | ||
onChange={(newValue) => setValue(newValue)} | ||
/> |
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
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
4 changes: 1 addition & 3 deletions
4
docs/data/date-pickers/custom-field/PickerWithButtonField.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 |
---|---|---|
@@ -1,7 +1,5 @@ | ||
<ButtonDatePicker | ||
label={`Current date: ${ | ||
value == null ? 'null' : value.format('MM/DD/YYYY') | ||
}`} | ||
label={value == null ? null : value.format('MM/DD/YYYY')} | ||
value={value} | ||
onChange={(newValue) => setValue(newValue)} | ||
/> |
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