From 251acbaae9d5fb428c998f7657b49790c16f23d2 Mon Sep 17 00:00:00 2001 From: HyeongKyeom Kim <97586683+Brokyeom@users.noreply.github.com> Date: Sat, 31 Aug 2024 14:43:27 +0900 Subject: [PATCH] =?UTF-8?q?fix(Select):=20defaultValue=20type=20Option?= =?UTF-8?q?=EB=A1=9C=20=EC=88=98=EC=A0=95=20(#120)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: Select defaultValue 타입 Option로 수정 * cs --- .changeset/beige-roses-sip.md | 5 +++ packages/ui/Input/Select.tsx | 85 ++++++++++++----------------------- 2 files changed, 34 insertions(+), 56 deletions(-) create mode 100644 .changeset/beige-roses-sip.md diff --git a/.changeset/beige-roses-sip.md b/.changeset/beige-roses-sip.md new file mode 100644 index 0000000..cb6c959 --- /dev/null +++ b/.changeset/beige-roses-sip.md @@ -0,0 +1,5 @@ +--- +"@sopt-makers/ui": patch +--- + +Select 컴포넌트 defaultValue type Option로 수정 diff --git a/packages/ui/Input/Select.tsx b/packages/ui/Input/Select.tsx index c98bf2a..ddd784b 100644 --- a/packages/ui/Input/Select.tsx +++ b/packages/ui/Input/Select.tsx @@ -1,6 +1,6 @@ -import { useState, useRef, useEffect, useCallback } from "react"; -import { IconChevronDown, IconUser } from "@sopt-makers/icons"; -import * as S from "./style.css"; +import { useState, useRef, useEffect, useCallback } from 'react'; +import { IconChevronDown, IconUser } from '@sopt-makers/icons'; +import * as S from './style.css'; export interface Option { label: string; @@ -13,28 +13,20 @@ export interface Option { interface SelectProps { className?: string; placeholder?: string; - type: "text" | "textDesc" | "textIcon" | "userList" | "userListDesc"; + type: 'text' | 'textDesc' | 'textIcon' | 'userList' | 'userListDesc'; options: Option[]; visibleOptions?: number; - defaultValue?: T; + defaultValue?: Option; onChange: (value: T) => void; } function Select(props: SelectProps) { - const { - className, - placeholder, - type, - options, - visibleOptions = 5, - defaultValue, - onChange, - } = props; + const { className, placeholder, type, options, visibleOptions = 5, defaultValue, onChange } = props; const optionsRef = useRef(null); const buttonRef = useRef(null); - const [selected, setSelected] = useState(defaultValue ?? null); + const [selected, setSelected] = useState | null>(defaultValue ?? null); const [open, setOpen] = useState(false); const handleToggleOpen = useCallback(() => { @@ -48,24 +40,20 @@ function Select(props: SelectProps) { const calcMaxHeight = useCallback(() => { const getOptionHeight = () => { switch (type) { - case "text": - case "textIcon": + case 'text': + case 'textIcon': return 42; - case "textDesc": - case "userListDesc": + case 'textDesc': + case 'userListDesc': return 62; - case "userList": + case 'userList': return 48; } }; const gapHeight = 6; const paddingHeight = 8; - return ( - getOptionHeight() * visibleOptions + - gapHeight * (visibleOptions - 1) + - paddingHeight * 2 - ); + return getOptionHeight() * visibleOptions + gapHeight * (visibleOptions - 1) + paddingHeight * 2; }, [visibleOptions, type]); useEffect(() => { @@ -79,65 +67,50 @@ function Select(props: SelectProps) { handleToggleClose(); } }; - document.addEventListener("mousedown", handleClickOutside); + document.addEventListener('mousedown', handleClickOutside); return () => { - document.removeEventListener("mousedown", handleClickOutside); + document.removeEventListener('mousedown', handleClickOutside); }; }, [handleToggleClose]); - const handleOptionClick = (value: T) => { + const handleOptionClick = (value: Option) => { setSelected(value); - onChange(value); + onChange(value.value); // value 자체가 아닌 value.value를 onChange에 전달 handleToggleClose(); }; - const selectedLabel = selected - ? options.find((option) => option.value === selected)?.label - : placeholder; + const selectedLabel = selected ? options.find((option) => option.value === selected.value)?.label : placeholder; return (
- {open ? ( -
    +
      {options.map((option) => (