-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pickers] Improve useSplitFieldInternalAndForwardedProps and make it …
…public
- Loading branch information
1 parent
cc2a9a3
commit 40bb070
Showing
15 changed files
with
118 additions
and
127 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 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
74 changes: 74 additions & 0 deletions
74
packages/x-date-pickers/src/hooks/useSplitFieldInternalAndForwardedProps.ts
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,74 @@ | ||
import * as React from 'react'; | ||
import { FieldValueType } from '../models'; | ||
import { | ||
DATE_TIME_VALIDATION_PROP_NAMES, | ||
DATE_VALIDATION_PROP_NAMES, | ||
TIME_VALIDATION_PROP_NAMES, | ||
} from '../internals/utils/validation/extractValidationProps'; | ||
|
||
const SHARED_FIELD_INTERNAL_PROP_NAMES = [ | ||
'value', | ||
'defaultValue', | ||
'referenceDate', | ||
'format', | ||
'formatDensity', | ||
'onChange', | ||
'timezone', | ||
'onError', | ||
'shouldRespectLeadingZeros', | ||
'selectedSections', | ||
'onSelectedSectionsChange', | ||
'unstableFieldRef', | ||
'enableAccessibleFieldDOMStructure', | ||
'disabled', | ||
'readOnly', | ||
'dateSeparator', | ||
] as const; | ||
|
||
type InternalPropNames<TValueType extends FieldValueType> = | ||
| (typeof SHARED_FIELD_INTERNAL_PROP_NAMES)[number] | ||
| (TValueType extends 'date' | 'date-time' ? (typeof DATE_VALIDATION_PROP_NAMES)[number] : never) | ||
| (TValueType extends 'time' | 'date-time' ? (typeof TIME_VALIDATION_PROP_NAMES)[number] : never) | ||
| (TValueType extends 'date-time' ? (typeof DATE_TIME_VALIDATION_PROP_NAMES)[number] : never); | ||
|
||
/** | ||
* Split the props received by the field component into: | ||
* - `internalProps` which are used by the various hooks called by the field component. | ||
* - `forwardedProps` which are passed to the underlying component. | ||
* @param {TProps} props The props received by the field component. | ||
* @param {TValueType} valueType The type of the field value ('date', 'time', or 'date-time'). | ||
*/ | ||
export const useSplitFieldInternalAndForwardedProps = < | ||
TValueType extends FieldValueType, | ||
TProps extends { [key in InternalPropNames<TValueType>]?: any }, | ||
>( | ||
props: TProps, | ||
valueType: TValueType, | ||
) => { | ||
return React.useMemo(() => { | ||
const forwardedProps = { ...props } as Omit<TProps, InternalPropNames<TValueType>>; | ||
const internalProps = {} as Pick<TProps, InternalPropNames<TValueType>>; | ||
|
||
const extractProp = (propName: string) => { | ||
if (forwardedProps.hasOwnProperty(propName)) { | ||
// @ts-ignore | ||
internalProps[propName] = forwardedProps[propName]; | ||
delete forwardedProps[propName as keyof typeof forwardedProps]; | ||
} | ||
}; | ||
|
||
SHARED_FIELD_INTERNAL_PROP_NAMES.forEach(extractProp); | ||
|
||
if (valueType === 'date') { | ||
DATE_VALIDATION_PROP_NAMES.forEach(extractProp); | ||
} else if (valueType === 'time') { | ||
TIME_VALIDATION_PROP_NAMES.forEach(extractProp); | ||
} else if (valueType === 'date-time') { | ||
DATE_VALIDATION_PROP_NAMES.forEach(extractProp); | ||
TIME_VALIDATION_PROP_NAMES.forEach(extractProp); | ||
DATE_TIME_VALIDATION_PROP_NAMES.forEach(extractProp); | ||
} | ||
|
||
return { forwardedProps, internalProps }; | ||
}, [props, valueType]); | ||
}; |
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
Oops, something went wrong.