From 146337e8f407ccb34fa52cec9d45ea37d69f3f3a Mon Sep 17 00:00:00 2001 From: jinhojang6 Date: Wed, 22 Feb 2023 02:48:42 +0900 Subject: [PATCH] refactor: refactor textfield component --- .../components/TextField/TextField.classes.ts | 5 +- .../components/TextField/TextField.styles.ts | 34 ++++----- .../src/components/TextField/TextField.tsx | 74 ++++++++++--------- 3 files changed, 56 insertions(+), 57 deletions(-) diff --git a/packages/lsd-react/src/components/TextField/TextField.classes.ts b/packages/lsd-react/src/components/TextField/TextField.classes.ts index ab3c2bd..86e4657 100644 --- a/packages/lsd-react/src/components/TextField/TextField.classes.ts +++ b/packages/lsd-react/src/components/TextField/TextField.classes.ts @@ -2,14 +2,13 @@ export const textFieldClasses = { root: `lsd-textField`, input: `lsd-textField__input`, - filled: `lsd-textField__icon`, + icon: `lsd-textField__icon`, supportingText: 'lsd-textField__supporting-text', - supportingTextLarge: 'lsd-textField__supporting-text--large', - supportingTextMedium: 'lsd-textField__supporting-text--medium', disabled: `lsd-textField--disabled`, error: 'lsd-textField--error', + large: `lsd-textField--large`, medium: `lsd-textField--medium`, withIcon: `lsd-textField--with-icon`, diff --git a/packages/lsd-react/src/components/TextField/TextField.styles.ts b/packages/lsd-react/src/components/TextField/TextField.styles.ts index c7bceeb..7cf9003 100644 --- a/packages/lsd-react/src/components/TextField/TextField.styles.ts +++ b/packages/lsd-react/src/components/TextField/TextField.styles.ts @@ -6,17 +6,25 @@ export const TextFieldStyles = css` width: auto; border-bottom: 1px solid rgb(var(--lsd-border-primary)); box-sizing: border-box; - display: flex; align-items: center; } + .${textFieldClasses.root} > div { + display: flex; + justify-content: space-between; + } + + .${textFieldClasses.disabled} { + opacity: 0.34; + } + .${textFieldClasses.input} { border: none; outline: none; font-size: 14px; color: rgb(var(--lsd-text-primary)); background: none; - width: inherit; + width: 100%; } .${textFieldClasses.input}:hover { @@ -28,19 +36,13 @@ export const TextFieldStyles = css` opacity: 0.3; } - .${textFieldClasses.disabled} { - cursor: default; - opacity: 0.34; - } - .${textFieldClasses.error} { text-decoration: line-through; } .${textFieldClasses.supportingText} { width: fit-content; - font-size: 12px; - line-height: 16px; + margin-top: 20px; } .${textFieldClasses.large} { @@ -55,22 +57,12 @@ export const TextFieldStyles = css` padding: 6px 12px; } - .${textFieldClasses.supportingTextLarge} { - margin: 8px 14px; - } - - .${textFieldClasses.supportingTextMedium} { - margin: 8px 12px; - } - .${textFieldClasses.withIcon} { } - .${textFieldClasses.filled} { + .${textFieldClasses.icon} { cursor: pointer; display: flex; - svg path { - --lsd-icon-primary: var(--lsd-icon-primary); - } + align-items: center; } ` diff --git a/packages/lsd-react/src/components/TextField/TextField.tsx b/packages/lsd-react/src/components/TextField/TextField.tsx index ce4ebd9..d0ea0fe 100644 --- a/packages/lsd-react/src/components/TextField/TextField.tsx +++ b/packages/lsd-react/src/components/TextField/TextField.tsx @@ -1,50 +1,62 @@ import clsx from 'clsx' -import React, { useState } from 'react' +import React, { useRef, useState } from 'react' import { CheckIcon, CloseIcon, ErrorIcon } from '../Icons' import { Typography } from '../Typography' import { textFieldClasses } from './TextField.classes' -export type TextFieldProps = React.InputHTMLAttributes & { +export type TextFieldProps = Omit< + React.HTMLAttributes, + 'onChange' | 'value' +> & { size?: 'large' | 'medium' withIcon?: boolean error?: boolean + disabled?: boolean supportingText?: string + value?: string + onChange?: (value: any) => void } export const TextField: React.FC & { classes: typeof textFieldClasses } = ({ size = 'large', - withIcon = 'false', - supportingText = 'Supporting text', + withIcon = false, + supportingText, error = false, children, + value, + onChange, ...props }) => { + const ref = useRef(null) const [inputValue, setInputValue] = useState('') - const onChange = (e: React.FormEvent) => { + const handleChange = (e: React.FormEvent) => { const newValue = e.currentTarget.value + if (onChange) onChange(newValue) setInputValue(newValue) } const onCancel = () => { setInputValue('') + if (typeof onChange !== 'undefined') onChange('') } return ( - <> -
+
+
& { {...props} /> {withIcon && error ? ( - - + + ) : withIcon && !inputValue.length ? ( - + ) : withIcon && inputValue.length ? ( - + ) : null}
{supportingText && ( - - {supportingText} - +
+ + {supportingText} + +
)} - +
) }