Skip to content

Commit 323a008

Browse files
committed
Fixed floating animation when there is already a default value setted (ISSUE Cnilton#133)
1 parent ea3d0a2 commit 323a008

File tree

1 file changed

+41
-22
lines changed

1 file changed

+41
-22
lines changed

src/index.tsx

+41-22
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import React, {
44
useEffect,
55
forwardRef,
66
useImperativeHandle,
7+
useMemo,
78
} from 'react';
89
import {
910
View,
@@ -232,37 +233,39 @@ const FloatingLabelInput: React.ForwardRefRenderFunction<InputRef, Props> = (
232233

233234
const [fontColorAnimated, setFontColorAnimated] = useState(0);
234235

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+
*/
235241
const [fontSizeAnimated, setFontSizeAnimated] = useState(
236242
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))
243245
);
244246

245247
const [leftAnimated, setLeftAnimated] = useState(
246248
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))
254251
);
255252

256253
const [topAnimated, setTopAnimated] = useState(
257254
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))
264257
);
265258

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+
266269
useEffect(() => {
267270
if (isFocused === undefined) {
268271
if (value !== '' || isFocusedState) {
@@ -291,7 +294,7 @@ const FloatingLabelInput: React.ForwardRefRenderFunction<InputRef, Props> = (
291294

292295
useEffect(() => {
293296
if (isFocusedState || value !== '') {
294-
if (halfTop !== 0) {
297+
if (halfTop !== 0 && !value) {
295298
animateFocus();
296299
}
297300
} else {
@@ -527,6 +530,21 @@ const FloatingLabelInput: React.ForwardRefRenderFunction<InputRef, Props> = (
527530
setHalfTop(height / 2);
528531
}
529532

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+
530548
const positionAnimations = useAnimatedStyle(() => {
531549
return {
532550
transform: [
@@ -602,11 +620,11 @@ const FloatingLabelInput: React.ForwardRefRenderFunction<InputRef, Props> = (
602620
<View style={containerStyles}>
603621
{leftComponent && leftComponent}
604622
<View style={{ flex: 1, flexDirection: 'row' }}>
605-
{!staticLabel && (
623+
{!staticLabel && halfTop > 0 && (
606624
<AnimatedText
607625
{...labelProps}
608626
onPress={setFocus}
609-
style={[style, positionAnimations, colorAnimation]}
627+
style={[style, (!value || value === '') ? positionAnimations : defaultPosition, colorAnimation]}
610628
>
611629
{label}
612630
</AnimatedText>
@@ -662,3 +680,4 @@ const FloatingLabelInput: React.ForwardRefRenderFunction<InputRef, Props> = (
662680
};
663681
export { setGlobalStyles };
664682
export default forwardRef(FloatingLabelInput);
683+

0 commit comments

Comments
 (0)