66 TextInput as NativeTextInput ,
77 TextStyle ,
88 ViewStyle ,
9+ NativeSyntheticEvent ,
10+ TextLayoutEventData ,
911} from 'react-native' ;
1012
1113import TextInputAffix , {
@@ -248,6 +250,10 @@ const TextInput = forwardRef<TextInputHandles, Props>(
248250 // Use value from props instead of local state when input is controlled
249251 const value = isControlled ? rest . value : uncontrolledValue ;
250252
253+ const [ labelTextLayout , setLabelTextLayout ] = React . useState ( {
254+ width : 33 ,
255+ } ) ;
256+
251257 const [ labelLayout , setLabelLayout ] = React . useState < {
252258 measured : boolean ;
253259 width : number ;
@@ -447,6 +453,18 @@ const TextInput = forwardRef<TextInputHandles, Props>(
447453 [ labelLayout . height , labelLayout . width ]
448454 ) ;
449455
456+ const handleLabelTextLayout = React . useCallback (
457+ ( { nativeEvent } : NativeSyntheticEvent < TextLayoutEventData > ) => {
458+ setLabelTextLayout ( {
459+ width : nativeEvent . lines . reduce (
460+ ( acc , line ) => acc + Math . ceil ( line . width ) ,
461+ 0
462+ ) ,
463+ } ) ;
464+ } ,
465+ [ ]
466+ ) ;
467+
450468 const forceFocus = React . useCallback ( ( ) => root . current ?. focus ( ) , [ ] ) ;
451469
452470 const { maxFontSizeMultiplier = 1.5 } = rest ;
@@ -469,6 +487,7 @@ const TextInput = forwardRef<TextInputHandles, Props>(
469487 focused,
470488 placeholder,
471489 value,
490+ labelTextLayout,
472491 labelLayout,
473492 leftLayout,
474493 rightLayout,
@@ -481,6 +500,7 @@ const TextInput = forwardRef<TextInputHandles, Props>(
481500 onBlur = { handleBlur }
482501 onChangeText = { handleChangeText }
483502 onLayoutAnimatedText = { handleLayoutAnimatedText }
503+ onLabelTextLayout = { handleLabelTextLayout }
484504 onLeftAffixLayoutChange = { onLeftAffixLayoutChange }
485505 onRightAffixLayoutChange = { onRightAffixLayoutChange }
486506 maxFontSizeMultiplier = { maxFontSizeMultiplier }
@@ -506,6 +526,7 @@ const TextInput = forwardRef<TextInputHandles, Props>(
506526 focused,
507527 placeholder,
508528 value,
529+ labelTextLayout,
509530 labelLayout,
510531 leftLayout,
511532 rightLayout,
@@ -518,6 +539,7 @@ const TextInput = forwardRef<TextInputHandles, Props>(
518539 onBlur = { handleBlur }
519540 onChangeText = { handleChangeText }
520541 onLayoutAnimatedText = { handleLayoutAnimatedText }
542+ onLabelTextLayout = { handleLabelTextLayout }
521543 onLeftAffixLayoutChange = { onLeftAffixLayoutChange }
522544 onRightAffixLayoutChange = { onRightAffixLayoutChange }
523545 maxFontSizeMultiplier = { maxFontSizeMultiplier }
0 commit comments