Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fields] Allow to override the separator between the start and the end date in all range fields #12174

Merged
merged 9 commits into from
Mar 20, 2024
Merged
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' } } }} />
LukasTy marked this conversation as resolved.
Show resolved Hide resolved
<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,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
@@ -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')
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved this one below the introduction of the single input range fields since it uses them


You can use the `dateSeparator` prop to change the separator rendered between the two `TextField`:
flaviendelangle marked this conversation as resolved.
Show resolved Hide resolved

{{"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 @@ -3,7 +3,7 @@ import PropTypes from 'prop-types';
import clsx from 'clsx';
import Stack, { StackProps } from '@mui/material/Stack';
import MuiTextField from '@mui/material/TextField';
import Typography, { TypographyProps } from '@mui/material/Typography';
import Typography from '@mui/material/Typography';
import { styled, useThemeProps } from '@mui/material/styles';
import { useSlotProps } from '@mui/base/utils';
import {
Expand Down 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, {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One less component in the tree 👌

name: 'MuiMultiInputDateRangeField',
slot: 'Separator',
overridesResolver: (props, styles) => styles.separator,
})({
lineHeight: '1.4375em', // 23px
});

Expand Down Expand Up @@ -156,6 +153,9 @@ const MultiInputDateRangeField = React.forwardRef(function MultiInputDateRangeFi
const separatorProps = useSlotProps({
elementType: Separator,
externalSlotProps: slotProps?.separator,
additionalProps: {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also applied the new prop to the multi input range fields because IMHO it makes the DX more consistent (even though people could use slotProps.fieldSeparator.children to achieve the same behavior.
Note that the old DX still works.

children: ` ${internalProps.dateSeparator ?? '–'} `,
},
ownerState,
className: classes.separator,
});
Expand Down Expand Up @@ -199,6 +199,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 @@ -3,7 +3,7 @@ import PropTypes from 'prop-types';
import clsx from 'clsx';
import Stack, { StackProps } from '@mui/material/Stack';
import MuiTextField from '@mui/material/TextField';
import Typography, { TypographyProps } from '@mui/material/Typography';
import Typography from '@mui/material/Typography';
import { styled, useThemeProps } from '@mui/material/styles';
import { useSlotProps } from '@mui/base/utils';
import {
Expand Down 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,
})({
lineHeight: '1.4375em', // 23px
});

Expand Down Expand Up @@ -154,6 +151,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 @@ -202,6 +202,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 @@ -3,7 +3,7 @@ import PropTypes from 'prop-types';
import { clsx } from 'clsx';
import Stack, { StackProps } from '@mui/material/Stack';
import MuiTextField from '@mui/material/TextField';
import Typography, { TypographyProps } from '@mui/material/Typography';
import Typography from '@mui/material/Typography';
import { styled, useThemeProps } from '@mui/material/styles';
import { useSlotProps } from '@mui/base/utils';
import {
Expand Down 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,
})({
lineHeight: '1.4375em', // 23px
});

Expand Down Expand Up @@ -157,6 +154,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 @@ -205,6 +205,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
Loading
Loading