-
-
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.
Review Lukas + JSDoc for useParsedFormat
- Loading branch information
1 parent
2365f69
commit 1772912
Showing
18 changed files
with
94 additions
and
95 deletions.
There are no files selected for viewing
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
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
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 was deleted.
Oops, something went wrong.
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,61 @@ | ||
'use client'; | ||
import * as React from 'react'; | ||
import { useRtl } from '@mui/system/RtlProvider'; | ||
import { useUtils } from '../internals/hooks/useUtils'; | ||
import { buildSectionsFromFormat } from '../internals/hooks/useField/buildSectionsFromFormat'; | ||
import { getLocalizedDigits } from '../internals/hooks/useField/useField.utils'; | ||
import { PickerValidDate } from '../models'; | ||
import { usePickersTranslations } from './usePickersTranslations'; | ||
import type { UseFieldInternalProps } from '../internals/hooks/useField'; | ||
|
||
interface UseParsedFormatParameters | ||
extends Pick< | ||
UseFieldInternalProps<any, any, any, any, any>, | ||
'format' | 'formatDensity' | 'shouldRespectLeadingZeros' | ||
> {} | ||
|
||
/** | ||
* Returns the parsed format to be rendered in the field when their is no value or in other parts of the picker. | ||
* This format is localized (e.g: `AAAA` for the year with the French locale) and cannot be parsed by your date library. | ||
* @param {object} The parameters needed to build the placeholder. | ||
* @param {string} params.format Format of the date to use. | ||
* @param {'dense' | 'spacious'} params.formatDensity Density of the format (setting `formatDensity` to `"spacious"` will add a space before and after each `/`, `-` and `.` character). | ||
* @param {boolean} params.shouldRespectLeadingZeros If `true`, the format will respect the leading zeroes, if `false`, the format will always add leading zeroes. | ||
* @returns | ||
*/ | ||
export const useParsedFormat = <TDate extends PickerValidDate>( | ||
parameters: UseParsedFormatParameters, | ||
) => { | ||
const { format, formatDensity = 'dense', shouldRespectLeadingZeros = false } = parameters; | ||
const utils = useUtils<TDate>(); | ||
const isRtl = useRtl(); | ||
const translations = usePickersTranslations<TDate>(); | ||
const localizedDigits = React.useMemo(() => getLocalizedDigits(utils), [utils]); | ||
|
||
return React.useMemo(() => { | ||
const sections = buildSectionsFromFormat({ | ||
utils, | ||
format, | ||
formatDensity, | ||
isRtl, | ||
shouldRespectLeadingZeros, | ||
localeText: translations, | ||
localizedDigits, | ||
date: null, | ||
// TODO v9: Make sure we still don't reverse in `buildSectionsFromFormat` when using `useParsedFormat`. | ||
enableAccessibleFieldDOMStructure: false, | ||
}); | ||
|
||
return sections | ||
.map((section) => `${section.startSeparator}${section.placeholder}${section.endSeparator}`) | ||
.join(''); | ||
}, [ | ||
utils, | ||
isRtl, | ||
translations, | ||
localizedDigits, | ||
format, | ||
formatDensity, | ||
shouldRespectLeadingZeros, | ||
]); | ||
}; |
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
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
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
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
Oops, something went wrong.