Skip to content

Commit 1772912

Browse files
Review Lukas + JSDoc for useParsedFormat
1 parent 2365f69 commit 1772912

File tree

18 files changed

+94
-95
lines changed

18 files changed

+94
-95
lines changed

docs/data/date-pickers/custom-field/custom-behavior/ReadOnlyMaterialTextField.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { DatePicker } from '@mui/x-date-pickers/DatePicker';
77
import { useValidation, validateDate } from '@mui/x-date-pickers/validation';
88
import {
99
useSplitFieldProps,
10-
useFieldPlaceholder,
10+
useParsedFormat,
1111
usePickersContext,
1212
} from '@mui/x-date-pickers/hooks';
1313

@@ -18,7 +18,7 @@ function ReadOnlyDateField(props) {
1818

1919
const pickersContext = usePickersContext();
2020

21-
const placeholder = useFieldPlaceholder(internalProps);
21+
const parsedFormat = useParsedFormat(internalProps);
2222
const { hasValidationError } = useValidation({
2323
validator: validateDate,
2424
value,
@@ -30,7 +30,7 @@ function ReadOnlyDateField(props) {
3030
<TextField
3131
{...other}
3232
value={value == null ? '' : value.format(format)}
33-
placeholder={placeholder}
33+
placeholder={parsedFormat}
3434
InputProps={{ ...InputProps, readOnly: true }}
3535
error={hasValidationError}
3636
onClick={pickersContext.onToggleView}

docs/data/date-pickers/custom-field/custom-behavior/ReadOnlyMaterialTextField.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
import { useValidation, validateDate } from '@mui/x-date-pickers/validation';
1212
import {
1313
useSplitFieldProps,
14-
useFieldPlaceholder,
14+
useParsedFormat,
1515
usePickersContext,
1616
} from '@mui/x-date-pickers/hooks';
1717

@@ -22,7 +22,7 @@ function ReadOnlyDateField(props: DatePickerFieldProps<Dayjs, false>) {
2222

2323
const pickersContext = usePickersContext();
2424

25-
const placeholder = useFieldPlaceholder(internalProps);
25+
const parsedFormat = useParsedFormat(internalProps);
2626
const { hasValidationError } = useValidation({
2727
validator: validateDate,
2828
value,
@@ -34,7 +34,7 @@ function ReadOnlyDateField(props: DatePickerFieldProps<Dayjs, false>) {
3434
<TextField
3535
{...other}
3636
value={value == null ? '' : value.format(format)}
37-
placeholder={placeholder}
37+
placeholder={parsedFormat}
3838
InputProps={{ ...InputProps, readOnly: true }}
3939
error={hasValidationError}
4040
onClick={pickersContext.onToggleView}

packages/x-date-pickers-pro/src/internals/hooks/useDesktopRangePicker/useDesktopRangePicker.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export const useDesktopRangePicker = <
9393
renderCurrentView,
9494
shouldRestoreFocus,
9595
fieldProps: pickerFieldProps,
96-
fieldContextValue,
96+
contextValue,
9797
} = usePicker<
9898
DateRange<TDate>,
9999
TDate,
@@ -210,7 +210,7 @@ export const useDesktopRangePicker = <
210210
const Layout = slots?.layout ?? PickersLayout;
211211

212212
const renderPicker = () => (
213-
<PickersProvider fieldContextValue={fieldContextValue} localeText={localeText}>
213+
<PickersProvider contextValue={contextValue} localeText={localeText}>
214214
<Field {...enrichedFieldProps} />
215215
<PickersPopper
216216
role="tooltip"

packages/x-date-pickers-pro/src/internals/hooks/useMobileRangePicker/useMobileRangePicker.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export const useMobileRangePicker = <
8888
layoutProps,
8989
renderCurrentView,
9090
fieldProps: pickerFieldProps,
91-
fieldContextValue,
91+
contextValue,
9292
} = usePicker<
9393
DateRange<TDate>,
9494
TDate,
@@ -215,7 +215,7 @@ export const useMobileRangePicker = <
215215
};
216216

217217
const renderPicker = () => (
218-
<PickersProvider fieldContextValue={fieldContextValue} localeText={localeText}>
218+
<PickersProvider contextValue={contextValue} localeText={localeText}>
219219
<Field {...enrichedFieldProps} />
220220
<PickersModalDialog {...actions} open={open} slots={slots} slotProps={slotProps}>
221221
<Layout

packages/x-date-pickers/src/hooks/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ export { usePickersTranslations } from './usePickersTranslations';
1010

1111
export { useSplitFieldProps } from './useSplitFieldProps';
1212

13-
export { useFieldPlaceholder } from './useFieldPlaceholder';
13+
export { useParsedFormat } from './useParsedFormat';
1414

1515
export { usePickersContext } from './usePickersContext';

packages/x-date-pickers/src/hooks/useFieldPlaceholder.ts

Lines changed: 0 additions & 56 deletions
This file was deleted.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
'use client';
2+
import * as React from 'react';
3+
import { useRtl } from '@mui/system/RtlProvider';
4+
import { useUtils } from '../internals/hooks/useUtils';
5+
import { buildSectionsFromFormat } from '../internals/hooks/useField/buildSectionsFromFormat';
6+
import { getLocalizedDigits } from '../internals/hooks/useField/useField.utils';
7+
import { PickerValidDate } from '../models';
8+
import { usePickersTranslations } from './usePickersTranslations';
9+
import type { UseFieldInternalProps } from '../internals/hooks/useField';
10+
11+
interface UseParsedFormatParameters
12+
extends Pick<
13+
UseFieldInternalProps<any, any, any, any, any>,
14+
'format' | 'formatDensity' | 'shouldRespectLeadingZeros'
15+
> {}
16+
17+
/**
18+
* Returns the parsed format to be rendered in the field when their is no value or in other parts of the picker.
19+
* This format is localized (e.g: `AAAA` for the year with the French locale) and cannot be parsed by your date library.
20+
* @param {object} The parameters needed to build the placeholder.
21+
* @param {string} params.format Format of the date to use.
22+
* @param {'dense' | 'spacious'} params.formatDensity Density of the format (setting `formatDensity` to `"spacious"` will add a space before and after each `/`, `-` and `.` character).
23+
* @param {boolean} params.shouldRespectLeadingZeros If `true`, the format will respect the leading zeroes, if `false`, the format will always add leading zeroes.
24+
* @returns
25+
*/
26+
export const useParsedFormat = <TDate extends PickerValidDate>(
27+
parameters: UseParsedFormatParameters,
28+
) => {
29+
const { format, formatDensity = 'dense', shouldRespectLeadingZeros = false } = parameters;
30+
const utils = useUtils<TDate>();
31+
const isRtl = useRtl();
32+
const translations = usePickersTranslations<TDate>();
33+
const localizedDigits = React.useMemo(() => getLocalizedDigits(utils), [utils]);
34+
35+
return React.useMemo(() => {
36+
const sections = buildSectionsFromFormat({
37+
utils,
38+
format,
39+
formatDensity,
40+
isRtl,
41+
shouldRespectLeadingZeros,
42+
localeText: translations,
43+
localizedDigits,
44+
date: null,
45+
// TODO v9: Make sure we still don't reverse in `buildSectionsFromFormat` when using `useParsedFormat`.
46+
enableAccessibleFieldDOMStructure: false,
47+
});
48+
49+
return sections
50+
.map((section) => `${section.startSeparator}${section.placeholder}${section.endSeparator}`)
51+
.join('');
52+
}, [
53+
utils,
54+
isRtl,
55+
translations,
56+
localizedDigits,
57+
format,
58+
formatDensity,
59+
shouldRespectLeadingZeros,
60+
]);
61+
};

packages/x-date-pickers/src/internals/components/PickersProvider.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,25 @@ export const PickersContext = React.createContext<PickersContextValue | null>(nu
77

88
/**
99
* Provides the context for the various parts of a picker component:
10-
* - fieldContextValue: the context for the field
10+
* - contextValue: the context for the picker sub-components.
1111
* - localizationProvider: the translations passed through the props and through a parent LocalizationProvider.
1212
*
1313
* @ignore - do not document.
1414
*/
1515
export function PickersProvider<TDate extends PickerValidDate>(
1616
props: PickersFieldProviderProps<TDate>,
1717
) {
18-
const { fieldContextValue, localeText, children } = props;
18+
const { contextValue, localeText, children } = props;
1919

2020
return (
21-
<PickersContext.Provider value={fieldContextValue}>
21+
<PickersContext.Provider value={contextValue}>
2222
<LocalizationProvider localeText={localeText}>{children}</LocalizationProvider>
2323
</PickersContext.Provider>
2424
);
2525
}
2626

2727
interface PickersFieldProviderProps<TDate extends PickerValidDate> {
28-
fieldContextValue: PickersContextValue;
28+
contextValue: PickersContextValue;
2929
localeText: PickersInputLocaleText<TDate> | undefined;
3030
children: React.ReactNode;
3131
}

packages/x-date-pickers/src/internals/hooks/useDesktopPicker/useDesktopPicker.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export const useDesktopPicker = <
8080
renderCurrentView,
8181
shouldRestoreFocus,
8282
fieldProps: pickerFieldProps,
83-
fieldContextValue,
83+
contextValue,
8484
} = usePicker<TDate | null, TDate, TView, FieldSection, TExternalProps, {}>({
8585
...pickerParams,
8686
props,
@@ -213,7 +213,7 @@ export const useDesktopPicker = <
213213
const handleFieldRef = useForkRef(fieldRef, fieldProps.unstableFieldRef);
214214

215215
const renderPicker = () => (
216-
<PickersProvider fieldContextValue={fieldContextValue} localeText={localeText}>
216+
<PickersProvider contextValue={contextValue} localeText={localeText}>
217217
<Field
218218
{...fieldProps}
219219
slots={slotsForField}

packages/x-date-pickers/src/internals/hooks/useField/buildSectionsFromFormat.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FieldSection, MuiPickersAdapter, PickersTimezone, PickerValidDate } from '../../../models';
1+
import { FieldSection, MuiPickersAdapter, PickerValidDate } from '../../../models';
22
import { PickersLocaleText } from '../../../locales';
33
import {
44
applyLocalizedDigits,
@@ -13,7 +13,6 @@ interface BuildSectionsFromFormatParams<TDate extends PickerValidDate> {
1313
format: string;
1414
formatDensity: 'dense' | 'spacious';
1515
isRtl: boolean;
16-
timezone: PickersTimezone;
1716
shouldRespectLeadingZeros: boolean;
1817
localeText: PickersLocaleText<TDate>;
1918
localizedDigits: string[];
@@ -64,15 +63,14 @@ const getEscapedPartsFromFormat = <TDate extends PickerValidDate>({
6463

6564
const getSectionPlaceholder = <TDate extends PickerValidDate>(
6665
utils: MuiPickersAdapter<TDate>,
67-
timezone: PickersTimezone,
6866
localeText: PickersLocaleText<TDate>,
6967
sectionConfig: Pick<FieldSection, 'type' | 'contentType'>,
7068
sectionFormat: string,
7169
) => {
7270
switch (sectionConfig.type) {
7371
case 'year': {
7472
return localeText.fieldYearPlaceholder({
75-
digitAmount: utils.formatByString(utils.date(undefined, timezone), sectionFormat).length,
73+
digitAmount: utils.formatByString(utils.date(undefined, 'default'), sectionFormat).length,
7674
format: sectionFormat,
7775
});
7876
}
@@ -119,7 +117,6 @@ const getSectionPlaceholder = <TDate extends PickerValidDate>(
119117

120118
const createSection = <TDate extends PickerValidDate>({
121119
utils,
122-
timezone,
123120
date,
124121
shouldRespectLeadingZeros,
125122
localeText,
@@ -140,7 +137,6 @@ const createSection = <TDate extends PickerValidDate>({
140137

141138
const hasLeadingZerosInFormat = doesSectionFormatHaveLeadingZeros(
142139
utils,
143-
timezone,
144140
sectionConfig.contentType,
145141
sectionConfig.type,
146142
token,
@@ -181,7 +177,7 @@ const createSection = <TDate extends PickerValidDate>({
181177
format: token,
182178
maxLength,
183179
value: sectionValue,
184-
placeholder: getSectionPlaceholder(utils, timezone, localeText, sectionConfig, token),
180+
placeholder: getSectionPlaceholder(utils, localeText, sectionConfig, token),
185181
hasLeadingZerosInFormat,
186182
hasLeadingZerosInInput,
187183
startSeparator,

0 commit comments

Comments
 (0)