Skip to content

Commit 1504b50

Browse files
committed
fix(input): label position
the label position is correct when the input already has a value
1 parent 50c53f4 commit 1504b50

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/components/Input/index.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, InputHTMLAttributes } from 'react';
1+
import React, { useState, InputHTMLAttributes, useEffect } from 'react';
22
import uniqid from 'uniqid';
33

44
import * as S from './styles';
@@ -30,6 +30,7 @@ export const Input = ({
3030
}: InputProps) => {
3131
const identifier = id || uniqid('appetizer-input__');
3232
const [active, setActive] = useState(!!props.placeholder);
33+
const hasValue = !!props.value || !!props.defaultValue;
3334

3435
const handleOnFocus = (e: React.FocusEvent<HTMLInputElement>) => {
3536
if (onFocus) onFocus(e);
@@ -41,6 +42,10 @@ export const Input = ({
4142
setActive(e.target?.value.length !== 0 || !!props.placeholder);
4243
};
4344

45+
useEffect(() => {
46+
if (hasValue) setActive(true);
47+
}, [hasValue]);
48+
4449
return (
4550
<S.Container>
4651
<S.InputContainer>

0 commit comments

Comments
 (0)