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

Input, TextArea 글자 수 카운트 추가 #47

Merged
merged 7 commits into from
Jul 18, 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
16 changes: 11 additions & 5 deletions src/app/test/Staging.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Fragment, useEffect, useState } from "react";
import { BottomSheet } from "@/component/BottomSheet";
import { Button, ButtonProvider } from "@/component/common/button";
import { CheckBox, CheckBoxGroup } from "@/component/common/checkBox";
import { Input, InputLabelContainer, Label } from "@/component/common/input";
import { Input, InputLabelContainer, Label, TextArea } from "@/component/common/input";
import { ProgressBar } from "@/component/common/ProgressBar";
import { Radio, RadioButtonGroup } from "@/component/common/radioButton";
import { useBottomSheet } from "@/hooks/useBottomSheet.ts";
Expand All @@ -14,11 +14,12 @@ import { useRadioButton } from "@/hooks/useRadioButton";
import { DefaultLayout } from "@/layout/DefaultLayout.tsx";

export default function Staging() {
const [isRadioChecked, onChange, selectedValue] = useRadioButton();
const [isCheckBoxChecked, toggle, selectedValues] = useCheckBox();
const { isChecked: isRadioChecked, onChange, selectedValue } = useRadioButton();
const { isChecked: isCheckBoxChecked, toggle, selectedValues } = useCheckBox();
const { openBottomSheet, bottomSheetState } = useBottomSheet();
const [number, setNumber] = useState(0);
const [layerName, handleChangeName] = useInput();
const { value: layerName, handleInputChange: handleChangeName } = useInput();
const { value: description, handleInputChange: handleChangeDescription } = useInput();

useEffect(() => {
console.log("라디오 버튼 선택 value:", selectedValue);
Expand Down Expand Up @@ -69,7 +70,12 @@ export default function Staging() {
<br />
<InputLabelContainer id={"retro"}>
<Label order={1}>회고 이름</Label>
<Input onChange={handleChangeName} value={layerName} />
<Input onChange={handleChangeName} value={layerName} maxLength={10} count />
</InputLabelContainer>

<InputLabelContainer id={"description"}>
<Label>한 줄 설명</Label>
<TextArea onChange={handleChangeDescription} value={description} maxLength={20} count />
</InputLabelContainer>

<ButtonProvider>
Expand Down
13 changes: 11 additions & 2 deletions src/component/common/input/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,27 @@ import { forwardRef, useContext } from "react";

import { InputContext } from "./InputLabelContainer";

import { Typography } from "@/component/common/typography";

type InputProps = {
value: string;
onChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
width?: string;
count?: boolean;
} & React.InputHTMLAttributes<HTMLInputElement>;

export const Input = forwardRef(function ({ id, width = "100%", ...props }: InputProps) {
export const Input = forwardRef(function ({ id, width = "100%", count, ...props }: InputProps) {
const { maxLength, value } = props;
const inputContext = useContext(InputContext);
return (
<div>
<div
css={css`
width: ${width};
border: 1px solid #e3e6ea; // FIXME: 디자인 토큰 적용하기
border: 1px solid ${"#e3e6ea"}; // FIXME: 디자인 토큰 적용하기
border-radius: 0.8rem;
padding: 1.6rem;
display: flex;
`}
>
<input
Expand All @@ -26,6 +33,8 @@ export const Input = forwardRef(function ({ id, width = "100%", ...props }: Inpu
`}
{...props}
/>
{/* FIXME - typography 컬러 넣기 !! */}
{count && maxLength && <Typography variant="CAPTION" color={"lightGrey"}>{`${value.length}/${maxLength}`}</Typography>}
</div>
</div>
);
Expand Down
54 changes: 54 additions & 0 deletions src/component/common/input/TextArea.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { css } from "@emotion/react";
import { forwardRef, useContext } from "react";

import { InputContext } from "./InputLabelContainer";

import { Typography } from "@/component/common/typography";

type TextAreaProps = {
value: string;
onChange: (e: React.ChangeEvent<HTMLTextAreaElement>) => void;
width?: string;
height?: string;
count?: boolean;
} & React.TextareaHTMLAttributes<HTMLTextAreaElement>;

export const TextArea = forwardRef(function ({ id, width = "100%", height = "8.4rem", count, ...props }: TextAreaProps) {
const { maxLength, value } = props;
const textareaContext = useContext(InputContext);
return (
<div>
<div
css={css`
width: ${width};
border: 1px solid ${"#e3e6ea"}; // FIXME: 디자인 토큰 적용하기
border-radius: 0.8rem;
padding: 1.6rem;
display: flex;
flex-direction: column;
height: ${height};
`}
>
<textarea
id={id || textareaContext?.id}
css={css`
width: 100%;
height: 100%;
`}
{...props}
/>
{count && maxLength && (
<div
css={css`
align-self: flex-end;
`}
>
<Typography variant="CAPTION" color={"lightGrey"}>{`${value.length}/${maxLength}`}</Typography>
</div>
)}
</div>
</div>
);
});

TextArea.displayName = "TextArea";
1 change: 1 addition & 0 deletions src/component/common/input/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { Input } from "./Input";
export { Label } from "./Label";
export { InputLabelContainer } from "./InputLabelContainer";
export { TextArea } from "./TextArea";
2 changes: 1 addition & 1 deletion src/hooks/useCheckBox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ export const useCheckBox = () => {
const toggle = (value: string) => setCheckedStates((prev) => ({ ...prev, [value]: !prev[value] }));
const selectedValues = Object.keys(checkedStates).filter((key) => checkedStates[key]);

return [isChecked, toggle, selectedValues] as const;
return { isChecked, toggle, selectedValues };
};
6 changes: 3 additions & 3 deletions src/hooks/useInput.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { useCallback, useState } from "react";

export const useInput = (defaultValue?: string) => {
export const useInput = <T extends HTMLInputElement | HTMLTextAreaElement>(defaultValue?: string) => {
const [value, setValue] = useState(defaultValue || "");

const handleInputChange = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
const handleInputChange = useCallback((e: React.ChangeEvent<T>) => {
setValue(e.target.value);
}, []);

return [value, handleInputChange] as const;
return { value, handleInputChange };
};
2 changes: 1 addition & 1 deletion src/hooks/useRadioButton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ export const useRadioButton = () => {
const isChecked = (value: string) => selectedValue === value;
const onChange = (value: string) => setSelectedValue(value);

return [isChecked, onChange, selectedValue] as const;
return { isChecked, onChange, selectedValue };
};
19 changes: 18 additions & 1 deletion src/style/global.css
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,25 @@ button {
user-select: none;
}

input {
input,
textarea {
border: none;
background-color: inherit;
outline: none;
padding: 0;
}

textarea {
resize: none;
}

::-webkit-scrollbar {
border: none;
width: 0.8rem;
}

::-webkit-scrollbar-thumb {
background-color: rgb(231, 231, 231);
border-radius: 0.8rem;
border: 0.4rem solid transparent;
}
1 change: 1 addition & 0 deletions src/style/variable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export const DESIGN_SYSTEM_COLOR = {
inverseGrey700: "#c3c3c6",
inverseGrey800: "#e4e4e5",
inverseGrey900: "#fff",
lightGrey: "#ced4da",
background: "#fff",
darkBackground: "#17171c",
greyBackground: "#f2f4f6",
Expand Down
Loading