Skip to content

Commit

Permalink
[fields] New prop dateSeparator on range fields
Browse files Browse the repository at this point in the history
  • Loading branch information
flaviendelangle committed Feb 22, 2024
1 parent 58f1859 commit 653cb7a
Show file tree
Hide file tree
Showing 36 changed files with 196 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default function MultiInputFieldSeparatorSlotProps() {
return (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<DemoContainer components={['DateRangePicker', 'DateRangePicker']}>
<DateRangePicker slotProps={{ fieldSeparator: { children: 'to' } }} />
<DateRangePicker slotProps={{ fieldSeparator: { sx: { color: 'red' } } }} />
<DateRangePicker slotProps={{ fieldSeparator: { sx: { opacity: 0.5 } } }} />
</DemoContainer>
</LocalizationProvider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ export default function MultiInputFieldSeparatorSlotProps() {
return (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<DemoContainer components={['DateRangePicker', 'DateRangePicker']}>
<DateRangePicker slotProps={{ fieldSeparator: { children: 'to' } }} />
<DateRangePicker
slotProps={{ fieldSeparator: { sx: { children: ' TOTO ' } } }}
/>
<DateRangePicker slotProps={{ fieldSeparator: { sx: { opacity: 0.5 } } }} />
</DemoContainer>
</LocalizationProvider>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<DateRangePicker slotProps={{ fieldSeparator: { children: 'to' } }} />
<DateRangePicker slotProps={{ fieldSeparator: { sx: { color: 'red' } } }} />
<DateRangePicker slotProps={{ fieldSeparator: { sx: { opacity: 0.5 } } }} />
20 changes: 20 additions & 0 deletions docs/data/date-pickers/custom-field/RangeFieldDateSeparator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as React from 'react';
import { DemoContainer } from '@mui/x-date-pickers/internals/demo';
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 { SingleInputDateRangeField } from '@mui/x-date-pickers-pro/SingleInputDateRangeField';

export default function RangeFieldDateSeparator() {
return (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<DemoContainer components={['DateRangePicker', 'DateRangePicker']}>
<DateRangePicker slotProps={{ field: { dateSeparator: ' to ' } }} />
<DateRangePicker
slotProps={{ field: { dateSeparator: ' to ' } }}
slots={{ field: SingleInputDateRangeField }}
/>
</DemoContainer>
</LocalizationProvider>
);
}
20 changes: 20 additions & 0 deletions docs/data/date-pickers/custom-field/RangeFieldDateSeparator.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as React from 'react';
import { DemoContainer } from '@mui/x-date-pickers/internals/demo';
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 { SingleInputDateRangeField } from '@mui/x-date-pickers-pro/SingleInputDateRangeField';

export default function RangeFieldDateSeparator() {
return (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<DemoContainer components={['DateRangePicker', 'DateRangePicker']}>
<DateRangePicker slotProps={{ field: { dateSeparator: 'to' } }} />
<DateRangePicker
slotProps={{ field: { dateSeparator: 'to' } }}
slots={{ field: SingleInputDateRangeField }}
/>
</DemoContainer>
</LocalizationProvider>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<DateRangePicker slotProps={{ field: { dateSeparator: ' to ' } }} />
<DateRangePicker
slotProps={{ field: { dateSeparator: ' to ' } }}
slots={{ field: SingleInputDateRangeField }}
/>
8 changes: 7 additions & 1 deletion docs/data/date-pickers/custom-field/custom-field.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ You can use the `textField` slot to pass custom props to the `TextField`:

{{"demo": "TextFieldSlotProps.js"}}

### Customize the separator of multi input fields [<span class="plan-pro"></span>](/x/introduction/licensing/#pro-plan 'Pro plan')
### Customize the separator of multi input range fields [<span class="plan-pro"></span>](/x/introduction/licensing/#pro-plan 'Pro plan')

You can use the `fieldSeparator` slot to pass custom props to the `Typography` rendered between the two `TextField`:

Expand Down Expand Up @@ -49,6 +49,12 @@ You can manually add an `endAdornment` if you want your range picker to look exa
This adornment is purely decorative, the focus remains on the field when the picker is opened.
:::

### Change the separator of range fields [<span class="plan-pro"></span>](/x/introduction/licensing/#pro-plan 'Pro plan')

You can use the `dateSeparator` prop to change the separator rendered between the two `TextField`:

{{"demo": "RangeFieldDateSeparator.js"}}

### Change the format density

You can control the field format spacing using the `formatDensity` prop.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"props": {
"autoFocus": { "type": { "name": "bool" } },
"classes": { "type": { "name": "object" }, "additionalInfo": { "cssApi": true } },
"dateSeparator": { "type": { "name": "string" }, "default": "\"\"" },
"defaultValue": { "type": { "name": "arrayOf", "description": "Array&lt;object&gt;" } },
"direction": {
"type": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"ampm": { "type": { "name": "bool" }, "default": "`utils.is12HourCycleInCurrentLocale()`" },
"autoFocus": { "type": { "name": "bool" } },
"classes": { "type": { "name": "object" }, "additionalInfo": { "cssApi": true } },
"dateSeparator": { "type": { "name": "string" }, "default": "\"\"" },
"defaultValue": { "type": { "name": "arrayOf", "description": "Array&lt;object&gt;" } },
"direction": {
"type": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"ampm": { "type": { "name": "bool" }, "default": "`utils.is12HourCycleInCurrentLocale()`" },
"autoFocus": { "type": { "name": "bool" } },
"classes": { "type": { "name": "object" }, "additionalInfo": { "cssApi": true } },
"dateSeparator": { "type": { "name": "string" }, "default": "\"\"" },
"defaultValue": { "type": { "name": "arrayOf", "description": "Array&lt;object&gt;" } },
"direction": {
"type": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
},
"default": "'primary'"
},
"dateSeparator": { "type": { "name": "string" }, "default": "\"\"" },
"defaultValue": { "type": { "name": "arrayOf", "description": "Array&lt;object&gt;" } },
"disabled": { "type": { "name": "bool" }, "default": "false" },
"disableFuture": { "type": { "name": "bool" }, "default": "false" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
},
"default": "'primary'"
},
"dateSeparator": { "type": { "name": "string" }, "default": "\"\"" },
"defaultValue": { "type": { "name": "arrayOf", "description": "Array&lt;object&gt;" } },
"disabled": { "type": { "name": "bool" }, "default": "false" },
"disableFuture": { "type": { "name": "bool" }, "default": "false" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
},
"default": "'primary'"
},
"dateSeparator": { "type": { "name": "string" }, "default": "\"\"" },
"defaultValue": { "type": { "name": "arrayOf", "description": "Array&lt;object&gt;" } },
"disabled": { "type": { "name": "bool" }, "default": "false" },
"disableFuture": { "type": { "name": "bool" }, "default": "false" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"description": "If <code>true</code>, the <code>input</code> element is focused during the first mount."
},
"classes": { "description": "Override or extend the styles applied to the component." },
"dateSeparator": { "description": "String displayed between the start and the end date." },
"defaultValue": {
"description": "The default value. Use when the component is not controlled."
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"description": "If <code>true</code>, the <code>input</code> element is focused during the first mount."
},
"classes": { "description": "Override or extend the styles applied to the component." },
"dateSeparator": { "description": "String displayed between the start and the end date." },
"defaultValue": {
"description": "The default value. Use when the component is not controlled."
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"description": "If <code>true</code>, the <code>input</code> element is focused during the first mount."
},
"classes": { "description": "Override or extend the styles applied to the component." },
"dateSeparator": { "description": "String displayed between the start and the end date." },
"defaultValue": {
"description": "The default value. Use when the component is not controlled."
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"color": {
"description": "The color of the component. It supports both default and custom theme colors, which can be added as shown in the <a href=\"https://mui.com/material-ui/customization/palette/#custom-colors\">palette customization guide</a>."
},
"dateSeparator": { "description": "String displayed between the start and the end date." },
"defaultValue": {
"description": "The default value. Use when the component is not controlled."
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"color": {
"description": "The color of the component. It supports both default and custom theme colors, which can be added as shown in the <a href=\"https://mui.com/material-ui/customization/palette/#custom-colors\">palette customization guide</a>."
},
"dateSeparator": { "description": "String displayed between the start and the end date." },
"defaultValue": {
"description": "The default value. Use when the component is not controlled."
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"color": {
"description": "The color of the component. It supports both default and custom theme colors, which can be added as shown in the <a href=\"https://mui.com/material-ui/customization/palette/#custom-colors\">palette customization guide</a>."
},
"dateSeparator": { "description": "String displayed between the start and the end date." },
"defaultValue": {
"description": "The default value. Use when the component is not controlled."
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,11 @@ const MultiInputDateRangeFieldRoot = styled(
},
)({});

const MultiInputDateRangeFieldSeparator = styled(
(props: TypographyProps) => <Typography {...props}>{props.children ?? ' – '}</Typography>,
{
name: 'MuiMultiInputDateRangeField',
slot: 'Separator',
overridesResolver: (props, styles) => styles.separator,
},
)({});
const MultiInputDateRangeFieldSeparator = styled(Typography, {
name: 'MuiMultiInputDateRangeField',
slot: 'Separator',
overridesResolver: (props, styles) => styles.separator,
})({});

type MultiInputDateRangeFieldComponent = (<
TDate extends PickerValidDate,
Expand Down Expand Up @@ -154,6 +151,9 @@ const MultiInputDateRangeField = React.forwardRef(function MultiInputDateRangeFi
const separatorProps = useSlotProps({
elementType: Separator,
externalSlotProps: slotProps?.separator,
additionalProps: {
children: ` ${internalProps.dateSeparator ?? '–'} `,
},
ownerState,
className: classes.separator,
});
Expand Down Expand Up @@ -197,6 +197,11 @@ MultiInputDateRangeField.propTypes = {
classes: PropTypes.object,
className: PropTypes.string,
component: PropTypes.elementType,
/**
* String displayed between the start and the end date.
* @default "–"
*/
dateSeparator: PropTypes.string,
/**
* The default value. Use when the component is not controlled.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,11 @@ const MultiInputDateTimeRangeFieldRoot = styled(
},
)({});

const MultiInputDateTimeRangeFieldSeparator = styled(
(props: TypographyProps) => <Typography {...props}>{props.children ?? ' – '}</Typography>,
{
name: 'MuiMultiInputDateTimeRangeField',
slot: 'Separator',
overridesResolver: (props, styles) => styles.separator,
},
)({});
const MultiInputDateTimeRangeFieldSeparator = styled(Typography, {
name: 'MuiMultiInputDateTimeRangeField',
slot: 'Separator',
overridesResolver: (props, styles) => styles.separator,
})({});

type MultiInputDateTimeRangeFieldComponent = (<
TDate extends PickerValidDate,
Expand Down Expand Up @@ -152,6 +149,9 @@ const MultiInputDateTimeRangeField = React.forwardRef(function MultiInputDateTim
const separatorProps = useSlotProps({
elementType: Separator,
externalSlotProps: slotProps?.separator,
additionalProps: {
children: ` ${internalProps.dateSeparator ?? '–'} `,
},
ownerState,
className: classes.separator,
});
Expand Down Expand Up @@ -200,6 +200,11 @@ MultiInputDateTimeRangeField.propTypes = {
classes: PropTypes.object,
className: PropTypes.string,
component: PropTypes.elementType,
/**
* String displayed between the start and the end date.
* @default "–"
*/
dateSeparator: PropTypes.string,
/**
* The default value. Use when the component is not controlled.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,11 @@ const MultiInputTimeRangeFieldRoot = styled(
},
)({});

const MultiInputTimeRangeFieldSeparator = styled(
(props: TypographyProps) => <Typography {...props}>{props.children ?? ' – '}</Typography>,
{
name: 'MuiMultiInputTimeRangeField',
slot: 'Separator',
overridesResolver: (props, styles) => styles.separator,
},
)({});
const MultiInputTimeRangeFieldSeparator = styled(Typography, {
name: 'MuiMultiInputTimeRangeField',
slot: 'Separator',
overridesResolver: (props, styles) => styles.separator,
})({});

type MultiInputTimeRangeFieldComponent = (<
TDate extends PickerValidDate,
Expand Down Expand Up @@ -155,6 +152,9 @@ const MultiInputTimeRangeField = React.forwardRef(function MultiInputTimeRangeFi
const separatorProps = useSlotProps({
elementType: Separator,
externalSlotProps: slotProps?.separator,
additionalProps: {
children: ` ${internalProps.dateSeparator ?? '–'} `,
},
ownerState,
className: classes.separator,
});
Expand Down Expand Up @@ -203,6 +203,11 @@ MultiInputTimeRangeField.propTypes = {
classes: PropTypes.object,
className: PropTypes.string,
component: PropTypes.elementType,
/**
* String displayed between the start and the end date.
* @default "–"
*/
dateSeparator: PropTypes.string,
/**
* The default value. Use when the component is not controlled.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ SingleInputDateRangeField.propTypes = {
*/
color: PropTypes.oneOf(['error', 'info', 'primary', 'secondary', 'success', 'warning']),
component: PropTypes.elementType,
/**
* String displayed between the start and the end date.
* @default "–"
*/
dateSeparator: PropTypes.string,
/**
* The default value. Use when the component is not controlled.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
UseClearableFieldSlotProps,
} from '@mui/x-date-pickers/hooks';
import { UseDateRangeFieldProps } from '../internals/models';
import type { DateRange, RangeFieldSection, DateRangeValidationError } from '../models';
import { DateRange, RangeFieldSection, DateRangeValidationError } from '../models';

export interface UseSingleInputDateRangeFieldProps<
TDate extends PickerValidDate,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import * as React from 'react';
import {
useField,
splitFieldInternalAndForwardedProps,
useDefaultizedDateField,
} from '@mui/x-date-pickers/internals';
import { PickerValidDate } from '@mui/x-date-pickers/models';
import { UseSingleInputDateRangeFieldProps } from './SingleInputDateRangeField.types';
import { rangeValueManager, rangeFieldValueManager } from '../internals/utils/valueManagers';
import { rangeValueManager, getRangeFieldValueManager } from '../internals/utils/valueManagers';
import { validateDateRange } from '../internals/utils/validation/validateDateRange';
import { RangeFieldSection, DateRange } from '../models';

Expand All @@ -27,6 +28,11 @@ export const useSingleInputDateRangeField = <
keyof UseSingleInputDateRangeFieldProps<TDate, TEnableAccessibleFieldDOMStructure>
>(props, 'date');

const fieldValueManager = React.useMemo(
() => getRangeFieldValueManager<TDate>({ dateSeparator: internalProps.dateSeparator }),
[internalProps.dateSeparator],
);

return useField<
DateRange<TDate>,
TDate,
Expand All @@ -38,7 +44,7 @@ export const useSingleInputDateRangeField = <
forwardedProps,
internalProps,
valueManager: rangeValueManager,
fieldValueManager: rangeFieldValueManager,
fieldValueManager,
validator: validateDateRange,
valueType: 'date',
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ SingleInputDateTimeRangeField.propTypes = {
*/
color: PropTypes.oneOf(['error', 'info', 'primary', 'secondary', 'success', 'warning']),
component: PropTypes.elementType,
/**
* String displayed between the start and the end date.
* @default "–"
*/
dateSeparator: PropTypes.string,
/**
* The default value. Use when the component is not controlled.
*/
Expand Down

0 comments on commit 653cb7a

Please sign in to comment.