11import {
2- ComponentPropsWithRef ,
2+ ComponentPropsWithoutRef ,
33 ComponentType ,
44 ElementType ,
5- ForwardedRef ,
5+ FC ,
6+ JSX ,
67 ReactNode ,
8+ Ref ,
79} from 'react' ;
8- import { forwardRef } from 'react' ;
9- import type { StyleProp , TextStyle , ViewProps } from 'react-native' ;
10+ import type {
11+ StyleProp ,
12+ TextInputProps ,
13+ TextStyle ,
14+ ViewProps ,
15+ } from 'react-native' ;
1016import { Platform , TextInput , View } from 'react-native' ;
1117
1218import { styles } from './CodeField.styles' ;
@@ -19,15 +25,26 @@ export interface RenderCellOptions {
1925 isFocused : boolean ;
2026}
2127
22- type OmitStyle < T extends { style ?: any } > = Omit < T , 'style' > ;
23-
24- interface BaseProps {
28+ export interface Props extends Omit < TextInputProps , 'style' > {
2529 renderCell : ( options : RenderCellOptions ) => ReactNode ;
2630 RootProps ?: ViewProps ;
2731 RootComponent ?: ComponentType < ViewProps > ;
2832 rootStyle ?: ViewProps [ 'style' ] ;
2933 textInputStyle ?: StyleProp < TextStyle > ;
3034 cellCount ?: number ;
35+ ref ?: Ref < TextInput > ;
36+ }
37+
38+ // Based on: https://github.com/mui/material-ui/blob/c3d02722da19e3bcb9b97eb640c21475fecd501c/packages/mui-material/src/OverridableComponent/index.ts#L10
39+ export interface CodeFieldOverridableComponent extends FC < Props > {
40+ // Overload for custom InputComponent to consume correct props
41+ < TInput extends ElementType > (
42+ props : { InputComponent : TInput } & Omit <
43+ ComponentPropsWithoutRef < TInput > ,
44+ 'style'
45+ > &
46+ Props ,
47+ ) : JSX . Element | null ;
3148}
3249
3350const DEFAULT_CELL_COUNT = 4 ;
@@ -36,20 +53,18 @@ const autoComplete = Platform.select({
3653 default : 'one-time-code' ,
3754} ) ;
3855
39- function CodeFieldComponent (
40- {
41- rootStyle,
42- textInputStyle,
43- value,
44- renderCell,
45- cellCount = DEFAULT_CELL_COUNT ,
46- RootProps,
47- RootComponent = View ,
48- InputComponent = TextInput ,
49- ...rest
50- } : Props & { InputComponent ?: ComponentType < any > } ,
51- ref : ForwardedRef < TextInput > ,
52- ) {
56+ export const CodeField : CodeFieldOverridableComponent = ( {
57+ rootStyle,
58+ textInputStyle,
59+ value,
60+ renderCell,
61+ cellCount = DEFAULT_CELL_COUNT ,
62+ RootProps,
63+ RootComponent = View ,
64+ InputComponent = TextInput ,
65+ ref,
66+ ...rest
67+ } : Props & { InputComponent ?: ComponentType < any > } ) => {
5368 'use memo' ;
5469 const [ isFocused , onFocus , onBlur ] = useFocusState ( rest . onBlur , rest . onFocus ) ;
5570
@@ -93,27 +108,6 @@ function CodeFieldComponent(
93108 />
94109 </ RootComponent >
95110 ) ;
96- }
97-
98- CodeFieldComponent . displayName = 'CodeField' ;
99-
100- export interface Props
101- extends BaseProps , OmitStyle < ComponentPropsWithRef < typeof TextInput > > {
102- //
103- }
104-
105- // Based on: https://github.com/mui/material-ui/blob/c3d02722da19e3bcb9b97eb640c21475fecd501c/packages/mui-material/src/OverridableComponent/index.ts#L10
106- export interface CodeFieldOverridableComponent {
107- // Overload for custom InputComponent to consume correct props
108- < TInput extends ElementType > (
109- props : { InputComponent : TInput } & OmitStyle < ComponentPropsWithRef < TInput > > &
110- BaseProps ,
111- ) : React . JSX . Element | null ;
112-
113- // Default overload with TextInput
114- ( props : Props ) : React . JSX . Element | null ;
115- }
111+ } ;
116112
117- export const CodeField = forwardRef (
118- CodeFieldComponent ,
119- ) as CodeFieldOverridableComponent ;
113+ CodeField . displayName = 'CodeField' ;
0 commit comments