Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correção na estilização do input "outlined" quando desabilitado #183

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
363 changes: 190 additions & 173 deletions src/components/TextInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
FocusEvent,
InputHTMLAttributes,
MouseEvent,
forwardRef,
useRef,
useState,
} from 'react';
Expand Down Expand Up @@ -56,194 +57,210 @@ export type TextInputType = InputHTMLAttributes<HTMLInputElement> &
| undefined;
};

const TextInput: FC<TextInputType> = ({
message,
error,
onChange,
onBlur,
onFocus,
onClickIconLeft,
onClickIconRight,
style,
label,
textInputStyle,
value,
id,
name,
maxLength,
autoFocus,
maskOptions,
iconRight,
iconLeft,
variant = 'default',
...rest
}) => {
const [isFocused, setIsFocused] = useState(false);

const hasValue = value?.length > 0;
const hasError = error ? error.length > 0 : false;
const hasFocus = isFocused || hasValue;

const RightIconComponent: any = iconRight && Icons[iconRight];
const LeftIconComponent: any = iconLeft && Icons[iconLeft];
const ErrorIconComponent: any = Icons['ExclamationTriangleIcon'];

const onAccept = (value: any) => {
if (onChange) {
onChange({
target: {
name,
value,
},
} as any);
}
};
const TextInput: FC<TextInputType> = forwardRef<
HTMLInputElement,
TextInputType
>(
(
{
message,
error,
onChange,
onBlur,
onFocus,
onClickIconLeft,
onClickIconRight,
style,
label,
textInputStyle,
value,
id,
name,
maxLength,
autoFocus,
maskOptions,
iconRight,
iconLeft,
variant = 'default',
...rest
},
ref,
) => {
const [isFocused, setIsFocused] = useState(false);

const handleFocus = () => {
setIsFocused(true);
};
const hasValue = value?.length > 0;
const hasError = error ? error.length > 0 : false;
const hasFocus = isFocused || hasValue;

const handleBlur = (event: FocusEvent<HTMLInputElement>) => {
if (event.target.value === '') {
setIsFocused(false);
}
};
const RightIconComponent: any = iconRight && Icons[iconRight];
const LeftIconComponent: any = iconLeft && Icons[iconLeft];
const ErrorIconComponent: any = Icons['ExclamationTriangleIcon'];

const onAccept = (value: any) => {
if (onChange) {
onChange({
target: {
name,
value,
},
} as any);
}
};

const handleFocus = () => {
setIsFocused(true);
};

const ref = useRef(null);
const inputRef = useRef(null);
const handleBlur = (event: FocusEvent<HTMLInputElement>) => {
if (event.target.value === '') {
setIsFocused(false);
}
};

const inputRef = useRef<HTMLInputElement>(null);

return (
<Wrapper style={style} $variant={variant}>
{variant === 'outlined' && (
<Label
htmlFor={id}
$hasFocus={hasFocus}
$hasError={hasError}
$hasIconLeft={!!iconLeft}
$hasIconRight={!!iconRight}
$isDisabled={rest.disabled}
>
{label}
</Label>
)}

return (
<Wrapper style={style}>
{variant === 'outlined' && (
<Label
htmlFor={id}
<InputWrapper
$hasFocus={hasFocus}
$hasError={hasError}
$hasIconLeft={!!iconLeft}
$hasIconRight={!!iconRight}
$variant={variant}
$isDisabled={rest.disabled}
>
{label}
</Label>
)}

<InputWrapper
$hasFocus={hasFocus}
$hasError={hasError}
$variant={variant}
>
{iconLeft && (
<IconWrapperLeft
$clickable={!!onClickIconLeft}
$hasError={hasError}
onClick={onClickIconLeft}
>
<LeftIconComponent
id={`${id}-left-icon`}
accessibility="ícone do botão"
{iconLeft && (
<IconWrapperLeft
$clickable={!!onClickIconLeft}
$hasError={hasError}
onClick={onClickIconLeft}
>
<LeftIconComponent
id={`${id}-left-icon`}
accessibility="ícone do botão"
/>
</IconWrapperLeft>
)}

{maskOptions ? (
<StyledIMaskInput
{...maskOptions}
id={id}
name={name}
data-testid={id}
style={textInputStyle}
ref={ref}
inputRef={inputRef}
onAccept={onAccept}
autoFocus={autoFocus}
hasError={hasError}
onFocus={(event) => {
handleFocus();
onFocus?.(event);
}}
onBlur={(event) => {
handleBlur(event);
onBlur?.(event);
}}
mask={hasFocus ? maskOptions?.mask : ''}
defaultValue={value}
$hasIconLeft={!!iconLeft}
$hasIconRight={!!iconRight}
$hasError={hasError}
$variant={variant}
$isDisabled={rest.disabled}
{...rest}
/>
</IconWrapperLeft>
)}
) : (
<Input
id={id}
data-testid={id}
name={name}
value={value}
style={textInputStyle}
ref={ref}
onFocus={(event) => {
handleFocus();
onFocus?.(event);
}}
onBlur={(event) => {
handleBlur(event);
onBlur?.(event);
}}
onChange={onChange}
maxLength={maxLength}
autoFocus={autoFocus}
$hasError={hasError}
$hasIconLeft={!!iconLeft}
$hasIconRight={!!iconRight}
$variant={variant}
$isDisabled={rest.disabled}
{...rest}
/>
)}

{iconRight && (
<IconWrapperRight
$clickable={!!onClickIconRight}
$hasError={hasError}
onClick={onClickIconRight}
>
<RightIconComponent
id={`${id}-right-icon`}
accessibility="ícone do botão"
/>
</IconWrapperRight>
)}

{maskOptions ? (
<StyledIMaskInput
{...maskOptions}
id={id}
name={name}
data-testid={id}
style={textInputStyle}
ref={ref}
inputRef={inputRef}
onAccept={onAccept}
autoFocus={autoFocus}
hasError={hasError}
onFocus={(event) => {
handleFocus();
onFocus?.(event);
}}
onBlur={(event) => {
handleBlur(event);
onBlur?.(event);
}}
mask={hasFocus ? maskOptions?.mask : ''}
defaultValue={value}
{variant === 'outlined' && (
<Fieldset
$hasFocus={hasFocus}
$hasError={hasError}
$isDisabled={rest.disabled}
>
<legend>
<span>{label}</span>
</legend>
</Fieldset>
)}
</InputWrapper>

{variant !== 'outlined' && (
<PlaceholderLabel
className="text-input-label"
$hasIconLeft={!!iconLeft}
$hasIconRight={!!iconRight}
$hasError={hasError}
$variant={variant}
{...rest}
/>
) : (
<Input
id={id}
data-testid={id}
name={name}
value={value}
style={textInputStyle}
onFocus={(event) => {
handleFocus();
onFocus?.(event);
}}
onBlur={(event) => {
handleBlur(event);
onBlur?.(event);
}}
onChange={onChange}
maxLength={maxLength}
autoFocus={autoFocus}
$hasError={hasError}
$hasIconLeft={!!iconLeft}
$hasIconRight={!!iconRight}
$variant={variant}
{...rest}
/>
$hasValue={hasValue}
>
{label}
</PlaceholderLabel>
)}

{iconRight && (
<IconWrapperRight
$clickable={!!onClickIconRight}
{error || message ? (
<Message
className="error-text-input"
$hasError={hasError}
onClick={onClickIconRight}
$variant={variant}
>
<RightIconComponent
id={`${id}-right-icon`}
accessibility="ícone do botão"
/>
</IconWrapperRight>
)}

{variant === 'outlined' && (
<Fieldset $hasFocus={hasFocus} $hasError={hasError}>
<legend>
<span>{label}</span>
</legend>
</Fieldset>
)}
</InputWrapper>

{variant !== 'outlined' && (
<PlaceholderLabel
className="text-input-label"
$hasIconLeft={!!iconLeft}
$hasIconRight={!!iconRight}
$hasError={hasError}
$hasValue={hasValue}
>
{label}
</PlaceholderLabel>
)}

{error || message ? (
<Message
className="error-text-input"
$hasError={hasError}
$variant={variant}
>
{variant === 'outlined' && <ErrorIconComponent />}
{error || message}
</Message>
) : null}
</Wrapper>
);
};
{variant === 'outlined' && <ErrorIconComponent />}
{error || message}
</Message>
) : null}
</Wrapper>
);
},
);

export default TextInput;
Loading
Loading