-
-
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.
[fields] Improve
useSplitFieldProps
and make it public (#14514)
- Loading branch information
1 parent
a198b26
commit 309106d
Showing
15 changed files
with
106 additions
and
128 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
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,77 @@ | ||
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. | ||
* Note that some forwarded props might be used by the hooks as well. | ||
* For instance, hooks like `useDateField` need props like `autoFocus` to know how to behave. | ||
* @template TProps, TValueType | ||
* @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 useSplitFieldProps = < | ||
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.