Skip to content

Commit de1451d

Browse files
[pickers] Make the usePickersTranslations hook public (#13657)
1 parent 2b16a5e commit de1451d

File tree

36 files changed

+197
-140
lines changed

36 files changed

+197
-140
lines changed

docs/data/date-pickers/custom-components/ActionBarComponent.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
99
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
1010
import { StaticDatePicker } from '@mui/x-date-pickers/StaticDatePicker';
1111

12-
import { useLocaleText } from '@mui/x-date-pickers/internals';
12+
import { usePickersTranslations } from '@mui/x-date-pickers/hooks';
1313

1414
function CustomActionBar(props) {
1515
const { onAccept, onClear, onCancel, onSetToday, actions, className } = props;
16-
const localeText = useLocaleText();
16+
const translations = usePickersTranslations();
1717
const [anchorEl, setAnchorEl] = React.useState(null);
1818
const open = Boolean(anchorEl);
1919
const id = useId();
@@ -34,7 +34,7 @@ function CustomActionBar(props) {
3434
}}
3535
key={actionType}
3636
>
37-
{localeText.clearButtonLabel}
37+
{translations.clearButtonLabel}
3838
</MenuItem>
3939
);
4040

@@ -47,7 +47,7 @@ function CustomActionBar(props) {
4747
}}
4848
key={actionType}
4949
>
50-
{localeText.cancelButtonLabel}
50+
{translations.cancelButtonLabel}
5151
</MenuItem>
5252
);
5353

@@ -60,7 +60,7 @@ function CustomActionBar(props) {
6060
}}
6161
key={actionType}
6262
>
63-
{localeText.okButtonLabel}
63+
{translations.okButtonLabel}
6464
</MenuItem>
6565
);
6666

@@ -74,7 +74,7 @@ function CustomActionBar(props) {
7474
}}
7575
key={actionType}
7676
>
77-
{localeText.todayButtonLabel}
77+
{translations.todayButtonLabel}
7878
</MenuItem>
7979
);
8080

docs/data/date-pickers/custom-components/ActionBarComponent.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
99
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
1010
import { StaticDatePicker } from '@mui/x-date-pickers/StaticDatePicker';
1111
import { PickersActionBarProps } from '@mui/x-date-pickers/PickersActionBar';
12-
import { useLocaleText } from '@mui/x-date-pickers/internals';
12+
import { usePickersTranslations } from '@mui/x-date-pickers/hooks';
1313

1414
function CustomActionBar(props: PickersActionBarProps) {
1515
const { onAccept, onClear, onCancel, onSetToday, actions, className } = props;
16-
const localeText = useLocaleText();
16+
const translations = usePickersTranslations();
1717
const [anchorEl, setAnchorEl] = React.useState<HTMLButtonElement | null>(null);
1818
const open = Boolean(anchorEl);
1919
const id = useId();
@@ -34,7 +34,7 @@ function CustomActionBar(props: PickersActionBarProps) {
3434
}}
3535
key={actionType}
3636
>
37-
{localeText.clearButtonLabel}
37+
{translations.clearButtonLabel}
3838
</MenuItem>
3939
);
4040
case 'cancel':
@@ -46,7 +46,7 @@ function CustomActionBar(props: PickersActionBarProps) {
4646
}}
4747
key={actionType}
4848
>
49-
{localeText.cancelButtonLabel}
49+
{translations.cancelButtonLabel}
5050
</MenuItem>
5151
);
5252
case 'accept':
@@ -58,7 +58,7 @@ function CustomActionBar(props: PickersActionBarProps) {
5858
}}
5959
key={actionType}
6060
>
61-
{localeText.okButtonLabel}
61+
{translations.okButtonLabel}
6262
</MenuItem>
6363
);
6464
case 'today':
@@ -71,7 +71,7 @@ function CustomActionBar(props: PickersActionBarProps) {
7171
}}
7272
key={actionType}
7373
>
74-
{localeText.todayButtonLabel}
74+
{translations.todayButtonLabel}
7575
</MenuItem>
7676
);
7777
default:
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<StaticDatePicker
2+
defaultValue={dayjs('2022-04-17')}
3+
slots={{
4+
actionBar: CustomActionBar,
5+
}}
6+
slotProps={{
7+
actionBar: {
8+
actions: ['accept'],
9+
},
10+
}}
11+
/>

docs/data/date-pickers/localization/localization.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,17 @@ You can [find the source](https://github.com/mui/mui-x/tree/HEAD/packages/x-date
131131

132132
To create your own translation or to customize the English text, copy this file to your project, make any changes needed and import the locale from there.
133133
Note that these translations of the date and time picker components depend on the [Localization strategy](/material-ui/guides/localization/) of the whole library.
134+
135+
## Access the translations in slots and subcomponents
136+
137+
You can use the `usePickersTranslations` hook to access the translations in your custom components.
138+
139+
```tsx
140+
import { usePickersTranslations } from '@mui/x-date-pickers/hooks';
141+
142+
const translations = usePickersTranslations();
143+
```
144+
145+
:::info
146+
See [Custom slots and subcomponents—Action bar](/x/react-date-pickers/custom-components/#component) for more details.
147+
:::

packages/x-date-pickers-pro/src/DateRangePicker/DateRangePickerToolbar.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import {
99
PickersToolbarButton,
1010
useUtils,
1111
BaseToolbarProps,
12-
useLocaleText,
1312
ExportedBaseToolbarProps,
1413
} from '@mui/x-date-pickers/internals';
14+
import { usePickersTranslations } from '@mui/x-date-pickers/hooks';
1515
import { PickerValidDate } from '@mui/x-date-pickers/models';
1616
import { DateRange } from '../models';
1717
import { UseRangePositionResponse } from '../internals/hooks/useRangePosition';
@@ -86,23 +86,23 @@ const DateRangePickerToolbar = React.forwardRef(function DateRangePickerToolbar<
8686
...other
8787
} = props;
8888

89-
const localeText = useLocaleText<TDate>();
89+
const translations = usePickersTranslations<TDate>();
9090

9191
const startDateValue = start
9292
? utils.formatByString(start, toolbarFormat || utils.formats.shortDate)
93-
: localeText.start;
93+
: translations.start;
9494

9595
const endDateValue = end
9696
? utils.formatByString(end, toolbarFormat || utils.formats.shortDate)
97-
: localeText.end;
97+
: translations.end;
9898

9999
const ownerState = props;
100100
const classes = useUtilityClasses(ownerState);
101101

102102
return (
103103
<DateRangePickerToolbarRoot
104104
{...other}
105-
toolbarTitle={localeText.dateRangePickerToolbarTitle}
105+
toolbarTitle={translations.dateRangePickerToolbarTitle}
106106
isLandscape={false}
107107
className={clsx(className, classes.root)}
108108
ownerState={ownerState}

packages/x-date-pickers-pro/src/DateTimeRangePicker/DateTimeRangePickerTabs.tsx

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ import { styled, useThemeProps } from '@mui/material/styles';
55
import composeClasses from '@mui/utils/composeClasses';
66
import useEventCallback from '@mui/utils/useEventCallback';
77
import { TimeIcon, DateRangeIcon, ArrowLeftIcon, ArrowRightIcon } from '@mui/x-date-pickers/icons';
8+
import { PickerValidDate } from '@mui/x-date-pickers/models';
89
import {
910
DateOrTimeViewWithMeridiem,
10-
useLocaleText,
1111
BaseTabsProps,
1212
ExportedBaseTabsProps,
1313
isDatePickerView,
1414
} from '@mui/x-date-pickers/internals';
15+
import { usePickersTranslations } from '@mui/x-date-pickers/hooks';
1516
import IconButton from '@mui/material/IconButton';
1617
import Button from '@mui/material/Button';
1718
import {
@@ -106,7 +107,7 @@ const DateTimeRangePickerTabFiller = styled('div', {
106107

107108
const tabOptions: TabValue[] = ['start-date', 'start-time', 'end-date', 'end-time'];
108109

109-
const DateTimeRangePickerTabs = function DateTimeRangePickerTabs(
110+
const DateTimeRangePickerTabs = function DateTimeRangePickerTabs<TDate extends PickerValidDate>(
110111
inProps: DateTimeRangePickerTabsProps,
111112
) {
112113
const props = useThemeProps({ props: inProps, name: 'MuiDateTimeRangePickerTabs' });
@@ -122,25 +123,31 @@ const DateTimeRangePickerTabs = function DateTimeRangePickerTabs(
122123
sx,
123124
} = props;
124125

125-
const localeText = useLocaleText();
126+
const translations = usePickersTranslations<TDate>();
126127
const classes = useUtilityClasses(props);
127128
const value = React.useMemo(() => viewToTab(view, rangePosition), [view, rangePosition]);
128129
const isPreviousHidden = value === 'start-date';
129130
const isNextHidden = value === 'end-time';
130131
const tabLabel = React.useMemo(() => {
131132
switch (value) {
132133
case 'start-date':
133-
return localeText.startDate;
134+
return translations.startDate;
134135
case 'start-time':
135-
return localeText.startTime;
136+
return translations.startTime;
136137
case 'end-date':
137-
return localeText.endDate;
138+
return translations.endDate;
138139
case 'end-time':
139-
return localeText.endTime;
140+
return translations.endTime;
140141
default:
141142
return '';
142143
}
143-
}, [localeText.endDate, localeText.endTime, localeText.startDate, localeText.startTime, value]);
144+
}, [
145+
translations.endDate,
146+
translations.endTime,
147+
translations.startDate,
148+
translations.startTime,
149+
value,
150+
]);
144151

145152
const handleRangePositionChange = useEventCallback((newTab: TabValue) => {
146153
if (newTab.includes('start')) {
@@ -176,7 +183,7 @@ const DateTimeRangePickerTabs = function DateTimeRangePickerTabs(
176183
<IconButton
177184
onClick={changeToPreviousTab}
178185
className={classes.navigationButton}
179-
title={localeText.openPreviousView}
186+
title={translations.openPreviousView}
180187
>
181188
<ArrowLeftIcon />
182189
</IconButton>
@@ -195,7 +202,7 @@ const DateTimeRangePickerTabs = function DateTimeRangePickerTabs(
195202
<IconButton
196203
onClick={changeToNextTab}
197204
className={classes.navigationButton}
198-
title={localeText.openNextView}
205+
title={translations.openNextView}
199206
>
200207
<ArrowRightIcon />
201208
</IconButton>

packages/x-date-pickers-pro/src/DateTimeRangePicker/DateTimeRangePickerToolbar.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import { styled, useThemeProps } from '@mui/material/styles';
55
import { unstable_composeClasses as composeClasses } from '@mui/utils';
66
import {
77
BaseToolbarProps,
8-
useLocaleText,
98
ExportedBaseToolbarProps,
109
useUtils,
1110
DateOrTimeViewWithMeridiem,
1211
WrapperVariant,
1312
} from '@mui/x-date-pickers/internals';
13+
import { usePickersTranslations } from '@mui/x-date-pickers/hooks';
1414
import { PickerValidDate } from '@mui/x-date-pickers/models';
1515
import {
1616
DateTimePickerToolbarProps,
@@ -151,7 +151,7 @@ const DateTimeRangePickerToolbar = React.forwardRef(function DateTimeRangePicker
151151
toolbarPlaceholder,
152152
};
153153

154-
const localeText = useLocaleText<TDate>();
154+
const translations = usePickersTranslations<TDate>();
155155

156156
const ownerState = props;
157157
const classes = useUtilityClasses(ownerState);
@@ -212,7 +212,7 @@ const DateTimeRangePickerToolbar = React.forwardRef(function DateTimeRangePicker
212212
<DateTimeRangePickerToolbarStart<TDate>
213213
value={start}
214214
onViewChange={handleStartRangeViewChange}
215-
toolbarTitle={localeText.start}
215+
toolbarTitle={translations.start}
216216
ownerState={ownerState}
217217
toolbarVariant="desktop"
218218
view={rangePosition === 'start' ? view : undefined}
@@ -224,7 +224,7 @@ const DateTimeRangePickerToolbar = React.forwardRef(function DateTimeRangePicker
224224
<DateTimeRangePickerToolbarEnd<TDate>
225225
value={end}
226226
onViewChange={handleEndRangeViewChange}
227-
toolbarTitle={localeText.end}
227+
toolbarTitle={translations.end}
228228
ownerState={ownerState}
229229
toolbarVariant="desktop"
230230
view={rangePosition === 'end' ? view : undefined}

packages/x-date-pickers-pro/src/PickersRangeCalendarHeader/PickersRangeCalendarHeader.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import { PickersCalendarHeader } from '@mui/x-date-pickers/PickersCalendarHeader
55
import { PickerValidDate } from '@mui/x-date-pickers/models';
66
import {
77
PickersArrowSwitcher,
8-
useLocaleText,
98
useNextMonthDisabled,
109
usePreviousMonthDisabled,
1110
useUtils,
1211
} from '@mui/x-date-pickers/internals';
12+
import { usePickersTranslations } from '@mui/x-date-pickers/hooks';
1313
import { PickersRangeCalendarHeaderProps } from './PickersRangeCalendarHeader.types';
1414

1515
type PickersRangeCalendarHeaderComponent = (<TDate extends PickerValidDate>(
@@ -27,7 +27,7 @@ const PickersRangeCalendarHeader = React.forwardRef(function PickersRangeCalenda
2727
TDate extends PickerValidDate,
2828
>(props: PickersRangeCalendarHeaderProps<TDate>, ref: React.Ref<HTMLDivElement>) {
2929
const utils = useUtils<TDate>();
30-
const localeText = useLocaleText<TDate>();
30+
const translations = usePickersTranslations<TDate>();
3131

3232
const { calendars, month, monthIndex, labelId, ...other } = props;
3333
const {
@@ -70,10 +70,10 @@ const PickersRangeCalendarHeader = React.forwardRef(function PickersRangeCalenda
7070
onGoToNext={selectNextMonth}
7171
isPreviousHidden={monthIndex !== 0}
7272
isPreviousDisabled={isPreviousMonthDisabled}
73-
previousLabel={localeText.previousMonth}
73+
previousLabel={translations.previousMonth}
7474
isNextHidden={monthIndex !== calendars - 1}
7575
isNextDisabled={isNextMonthDisabled}
76-
nextLabel={localeText.nextMonth}
76+
nextLabel={translations.nextMonth}
7777
slots={slots}
7878
slotProps={slotProps}
7979
labelId={labelId}

packages/x-date-pickers-pro/src/internals/hooks/useEnrichedRangePickerFieldProps.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@ import {
1111
FieldRef,
1212
PickerValidDate,
1313
} from '@mui/x-date-pickers/models';
14-
import { UseClearableFieldSlots, UseClearableFieldSlotProps } from '@mui/x-date-pickers/hooks';
14+
import {
15+
UseClearableFieldSlots,
16+
UseClearableFieldSlotProps,
17+
usePickersTranslations,
18+
} from '@mui/x-date-pickers/hooks';
1519
import { PickersInputLocaleText } from '@mui/x-date-pickers/locales';
1620
import {
1721
BaseFieldProps,
1822
onSpaceOrEnter,
19-
useLocaleText,
2023
UsePickerResponse,
2124
WrapperVariant,
2225
UsePickerProps,
@@ -167,7 +170,7 @@ const useMultiInputFieldSlotProps = <
167170
TError
168171
>;
169172

170-
const localeText = useLocaleText<TDate>();
173+
const translations = usePickersTranslations<TDate>();
171174
const handleStartFieldRef = useForkRef(fieldProps.unstableStartFieldRef, startFieldRef);
172175
const handleEndFieldRef = useForkRef(fieldProps.unstableEndFieldRef, endFieldRef);
173176

@@ -245,7 +248,7 @@ const useMultiInputFieldSlotProps = <
245248
let InputProps: MultiInputFieldSlotTextFieldProps['InputProps'];
246249
if (ownerState.position === 'start') {
247250
textFieldProps = {
248-
label: inLocaleText?.start ?? localeText.start,
251+
label: inLocaleText?.start ?? translations.start,
249252
onKeyDown: onSpaceOrEnter(openRangeStartSelection),
250253
onFocus: handleFocusStart,
251254
focused: open ? rangePosition === 'start' : undefined,
@@ -262,7 +265,7 @@ const useMultiInputFieldSlotProps = <
262265
}
263266
} else {
264267
textFieldProps = {
265-
label: inLocaleText?.end ?? localeText.end,
268+
label: inLocaleText?.end ?? translations.end,
266269
onKeyDown: onSpaceOrEnter(openRangeEndSelection),
267270
onFocus: handleFocusEnd,
268271
focused: open ? rangePosition === 'end' : undefined,

0 commit comments

Comments
 (0)