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

[Refactor/#82] TextField, TextArea 리팩토링 #83

Merged
merged 5 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
17 changes: 13 additions & 4 deletions src/components/commons/input/textArea/TextArea.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
import { ChangeEvent, TextareaHTMLAttributes } from "react";
import React, { ChangeEvent, TextareaHTMLAttributes } from "react";
import * as S from "./TextArea.styled";

export interface TextAreaProps extends TextareaHTMLAttributes<HTMLTextAreaElement> {
onChangeValue: (value: string) => void;
onChange: (e: React.ChangeEvent<HTMLTextAreaElement>) => void;
maxLength?: number;
placeholder: string;
}

const TextArea = ({ value, onChangeValue, maxLength, placeholder, ...rest }: TextAreaProps) => {
const TextArea = ({ value, onChange, maxLength, placeholder, ...rest }: TextAreaProps) => {
const handleOnInput = (e: ChangeEvent<HTMLTextAreaElement>) => {
let inputValue = e.target.value;
if (maxLength && inputValue.length > maxLength) {
inputValue = inputValue.slice(0, maxLength);
}
onChangeValue(inputValue);

const newEvent = {
...e,
target: {
...e.target,
value: inputValue,
},
} as ChangeEvent<HTMLTextAreaElement>;

onChange(newEvent);
};
return (
<S.TextAreaWrapper>
Expand Down
47 changes: 38 additions & 9 deletions src/components/commons/input/textField/TextField.tsx
Original file line number Diff line number Diff line change
@@ -1,60 +1,89 @@
import { ChangeEvent, InputHTMLAttributes } from "react";
import React, { ChangeEvent, InputHTMLAttributes, useRef } from "react";
import * as S from "./TextField.styled";

export interface TextFieldProps extends InputHTMLAttributes<HTMLInputElement> {
onChangeValue: (value: string) => void;
onChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
maxLength?: number;
placeholder: string;
narrow?: false | true;
unit?: "time" | "ticket" | "amount"; // 단위 : "분", "매", "원"
filter?: (value: string) => string;
cap?: false | true;
}

const TextField = ({
name,
value,
onChangeValue,
onChange,
maxLength,
placeholder,
narrow,
unit,
filter,
cap,
...rest
}: TextFieldProps) => {
const label = unit === "time" ? "분" : unit === "ticket" ? "매" : "원";

const inputRef = useRef<HTMLInputElement>(null);

// 값 입력될 떄
const handleOnInput = (e: ChangeEvent<HTMLInputElement>) => {
const inputName = e.target.name;
let inputValue = e.target.value;

if (filter) {
inputValue = filter(inputValue);
}
if (maxLength && inputValue.length > maxLength) {
inputValue = inputValue.slice(0, maxLength);
}
onChangeValue(inputValue);

const newEvent = {
...e,
target: {
...e.target,
name: inputName,
value: inputValue,
},
} as ChangeEvent<HTMLInputElement>;

onChange(newEvent);
};

// 값 지울 때
const handleClearInput = () => {
onChangeValue("");
if (inputRef.current) {
const inputName = inputRef.current.name;

// 값 지우기
inputRef.current.value = "";

const newEvent = {
target: {
name: inputName,
value: "",
},
} as ChangeEvent<HTMLInputElement>;

onChange(newEvent);
}
};

return (
<S.TextFieldLayout narrow={narrow}>
<S.TextFieldWrapper>
<S.TextFieldInput
{...rest}
ref={inputRef}
name={name}
value={value}
onChange={handleOnInput}
maxLength={maxLength}
placeholder={placeholder}
{...rest}
/>
{!narrow && !unit && value && <S.TextClear onClick={handleClearInput} />}
{unit && <S.TextUnit>{label}</S.TextUnit>}
</S.TextFieldWrapper>
{maxLength && cap && <S.TextCap>{`${(value as string).length}/${maxLength}`}</S.TextCap>}
{maxLength && <S.TextCap>{`${(value as string).length}/${maxLength}`}</S.TextCap>}
</S.TextFieldLayout>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p3) 이 부분 제가 cap 여부 조건 추가해서 머지했는데 혹시 pull 받고 확인가능할까요??

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

반영했습니다!

);
};
Expand Down
22 changes: 10 additions & 12 deletions src/pages/test/TestPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,19 @@ import Spacing from "@components/commons/spacing/Spacing";
import Stepper from "@components/commons/stepper/Stepper";
import Toast from "@components/commons/toast/Toast";
import useToast from "@hooks/useToast";
import { numericFilter } from "@utils/useInputFilter";
import { useState } from "react";
import { ChangeEvent, useState } from "react";

const TestPage = () => {
const [inputValue, setInputValue] = useState("");
const [inputAreaValue, setInputAreaValue] = useState("");
const [round, setRound] = useState(1);
const { showToast, isToastVisible } = useToast();

const handleChangeInput = (value: string) => {
setInputValue(value);
const handleChangeInput = (e: ChangeEvent<HTMLInputElement>) => {
setInputValue(e.target.value);
};
const handleChangeInputArea = (value: string) => {
setInputAreaValue(value);
const handleChangeInputArea = (e: ChangeEvent<HTMLTextAreaElement>) => {
setInputAreaValue(e.target.value);
};
const onMinusClick = () => {
setRound((prev) => prev - 1);
Expand All @@ -30,17 +29,16 @@ const TestPage = () => {
};
return (
<div style={{ display: "flex", flexDirection: "column", gap: "3rem", alignItems: "center" }}>
<div style={{ display: "flex", flexDirection: "column", gap: "3rem" }}>
<div style={{ display: "flex", flexDirection: "column" }}>
<TextField
value={inputValue}
onChangeValue={handleChangeInput}
filter={numericFilter}
maxLength={4}
placeholder="비밀번호 숫자 4자리"
onChange={handleChangeInput}
maxLength={30}
placeholder="입력해주세요"
/>
<TextArea
value={inputAreaValue}
onChangeValue={handleChangeInputArea}
onChange={handleChangeInputArea}
maxLength={300}
placeholder="입력해주세요"
/>
Expand Down
Loading