6
6
TextInput as NativeTextInput ,
7
7
TextStyle ,
8
8
ViewStyle ,
9
+ NativeSyntheticEvent ,
10
+ TextLayoutEventData ,
9
11
} from 'react-native' ;
10
12
11
13
import TextInputAffix , {
@@ -248,6 +250,10 @@ const TextInput = forwardRef<TextInputHandles, Props>(
248
250
// Use value from props instead of local state when input is controlled
249
251
const value = isControlled ? rest . value : uncontrolledValue ;
250
252
253
+ const [ labelTextLayout , setLabelTextLayout ] = React . useState ( {
254
+ width : 33 ,
255
+ } ) ;
256
+
251
257
const [ labelLayout , setLabelLayout ] = React . useState < {
252
258
measured : boolean ;
253
259
width : number ;
@@ -447,6 +453,18 @@ const TextInput = forwardRef<TextInputHandles, Props>(
447
453
[ labelLayout . height , labelLayout . width ]
448
454
) ;
449
455
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
+
450
468
const forceFocus = React . useCallback ( ( ) => root . current ?. focus ( ) , [ ] ) ;
451
469
452
470
const { maxFontSizeMultiplier = 1.5 } = rest ;
@@ -469,6 +487,7 @@ const TextInput = forwardRef<TextInputHandles, Props>(
469
487
focused,
470
488
placeholder,
471
489
value,
490
+ labelTextLayout,
472
491
labelLayout,
473
492
leftLayout,
474
493
rightLayout,
@@ -481,6 +500,7 @@ const TextInput = forwardRef<TextInputHandles, Props>(
481
500
onBlur = { handleBlur }
482
501
onChangeText = { handleChangeText }
483
502
onLayoutAnimatedText = { handleLayoutAnimatedText }
503
+ onLabelTextLayout = { handleLabelTextLayout }
484
504
onLeftAffixLayoutChange = { onLeftAffixLayoutChange }
485
505
onRightAffixLayoutChange = { onRightAffixLayoutChange }
486
506
maxFontSizeMultiplier = { maxFontSizeMultiplier }
@@ -506,6 +526,7 @@ const TextInput = forwardRef<TextInputHandles, Props>(
506
526
focused,
507
527
placeholder,
508
528
value,
529
+ labelTextLayout,
509
530
labelLayout,
510
531
leftLayout,
511
532
rightLayout,
@@ -518,6 +539,7 @@ const TextInput = forwardRef<TextInputHandles, Props>(
518
539
onBlur = { handleBlur }
519
540
onChangeText = { handleChangeText }
520
541
onLayoutAnimatedText = { handleLayoutAnimatedText }
542
+ onLabelTextLayout = { handleLabelTextLayout }
521
543
onLeftAffixLayoutChange = { onLeftAffixLayoutChange }
522
544
onRightAffixLayoutChange = { onRightAffixLayoutChange }
523
545
maxFontSizeMultiplier = { maxFontSizeMultiplier }
0 commit comments