@@ -4,6 +4,7 @@ import React, {
4
4
useEffect ,
5
5
forwardRef ,
6
6
useImperativeHandle ,
7
+ useMemo ,
7
8
} from 'react' ;
8
9
import {
9
10
View ,
@@ -232,37 +233,39 @@ const FloatingLabelInput: React.ForwardRefRenderFunction<InputRef, Props> = (
232
233
233
234
const [ fontColorAnimated , setFontColorAnimated ] = useState ( 0 ) ;
234
235
236
+ /**
237
+ * Set the initial values of the label;
238
+ * If there is already a value setted, it means that the label in case it is not a static one, should
239
+ * have the size and position as they were focused
240
+ */
235
241
const [ fontSizeAnimated , setFontSizeAnimated ] = useState (
236
242
isFocused
237
- ? customLabelStyles . fontSizeFocused
238
- ? customLabelStyles . fontSizeFocused
239
- : 10
240
- : customLabelStyles . fontSizeBlurred
241
- ? customLabelStyles . fontSizeBlurred
242
- : 14 ,
243
+ ? ( customLabelStyles ?. fontSizeFocused || 10 )
244
+ : ( value ? ( customLabelStyles ?. fontSizeFocused || 14 ) : ( customLabelStyles ?. fontSizeBlurred || 14 ) )
243
245
) ;
244
246
245
247
const [ leftAnimated , setLeftAnimated ] = useState (
246
248
staticLabel
247
- ? customLabelStyles ?. leftFocused !== undefined
248
- ? customLabelStyles . leftFocused
249
- : 15
250
- : customLabelStyles != undefined &&
251
- customLabelStyles . leftBlurred !== undefined
252
- ? customLabelStyles . leftBlurred
253
- : 6 ,
249
+ ? ( customLabelStyles ?. leftFocused || 15 )
250
+ : ( value ? ( customLabelStyles ?. leftFocused || 0 ) : ( customLabelStyles ?. leftBlurred || 6 ) )
254
251
) ;
255
252
256
253
const [ topAnimated , setTopAnimated ] = useState (
257
254
staticLabel
258
- ? customLabelStyles ?. topFocused !== undefined
259
- ? customLabelStyles . topFocused
260
- : 0
261
- : customLabelStyles . topBlurred
262
- ? customLabelStyles . topBlurred
263
- : 0 ,
255
+ ? ( customLabelStyles ?. topFocused || 0 )
256
+ : ( value ? ( customLabelStyles ?. topFocused || ( - halfTop + ( customLabelStyles ?. fontSizeFocused || 10 ) ) ) : ( customLabelStyles ?. topBlurred || 0 ) )
264
257
) ;
265
258
259
+ // ==== End initial values =====
260
+
261
+ useEffect ( ( ) => {
262
+ // if it's not a static label, as soon as the "halfTop" state has been setted to the onLayout event, get the correct ..
263
+ // .. top position of the label based if it has already a value setted or not
264
+ if ( halfTop > 0 && ! staticLabel ) {
265
+ setTopAnimated ( ( value ? ( customLabelStyles ?. topFocused || ( - halfTop + ( customLabelStyles ?. fontSizeFocused || 10 ) ) ) : ( customLabelStyles ?. topBlurred || 0 ) ) )
266
+ }
267
+ } , [ halfTop ] )
268
+
266
269
useEffect ( ( ) => {
267
270
if ( isFocused === undefined ) {
268
271
if ( value !== '' || isFocusedState ) {
@@ -291,7 +294,7 @@ const FloatingLabelInput: React.ForwardRefRenderFunction<InputRef, Props> = (
291
294
292
295
useEffect ( ( ) => {
293
296
if ( isFocusedState || value !== '' ) {
294
- if ( halfTop !== 0 ) {
297
+ if ( halfTop !== 0 && ! value ) {
295
298
animateFocus ( ) ;
296
299
}
297
300
} else {
@@ -527,6 +530,21 @@ const FloatingLabelInput: React.ForwardRefRenderFunction<InputRef, Props> = (
527
530
setHalfTop ( height / 2 ) ;
528
531
}
529
532
533
+ // Get the default non-animated position of the label and store it in a memo
534
+ const defaultPosition = useMemo ( ( ) => (
535
+ {
536
+ transform : [
537
+ {
538
+ translateX : leftAnimated
539
+ } ,
540
+ {
541
+ translateY : topAnimated
542
+ }
543
+ ] ,
544
+ fontSize : fontSizeAnimated
545
+ }
546
+ ) , [ leftAnimated , topAnimated , fontSizeAnimated ] ) ;
547
+
530
548
const positionAnimations = useAnimatedStyle ( ( ) => {
531
549
return {
532
550
transform : [
@@ -602,11 +620,11 @@ const FloatingLabelInput: React.ForwardRefRenderFunction<InputRef, Props> = (
602
620
< View style = { containerStyles } >
603
621
{ leftComponent && leftComponent }
604
622
< View style = { { flex : 1 , flexDirection : 'row' } } >
605
- { ! staticLabel && (
623
+ { ! staticLabel && halfTop > 0 && (
606
624
< AnimatedText
607
625
{ ...labelProps }
608
626
onPress = { setFocus }
609
- style = { [ style , positionAnimations , colorAnimation ] }
627
+ style = { [ style , ( ! value || value === '' ) ? positionAnimations : defaultPosition , colorAnimation ] }
610
628
>
611
629
{ label }
612
630
</ AnimatedText >
@@ -662,3 +680,4 @@ const FloatingLabelInput: React.ForwardRefRenderFunction<InputRef, Props> = (
662
680
} ;
663
681
export { setGlobalStyles } ;
664
682
export default forwardRef ( FloatingLabelInput ) ;
683
+
0 commit comments