-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Refactor] DropDownOption 에 대해서 context 로 분리 (#93)
* feat : 서치바 구현 * feat : 스토리북 추가 * feat : 서치바 테스트 코드 작성 및 aria 속성 수정 * refactor: useFormCOntrol 로 분리 * chore: 체인지셋 작성 * refactor : 드롭다운 컨텍스트로 분리 * refactor : 절대경로로 수정 * fix : ts 로 변경 * feat : DropDownOption 에 use client 추가, useSafeContext 추가 * refactor: useSafeContext 의 타입 추론 강화, aria 속성 추가 * fix : 접근성 테스트 해결, test ci 해결 * fix: 텍필 원복 * style : trigger 가 있을 때는 100% 유지 * refactor: useDropDownstate 수정 * refactor : DropDownTrigger 로 리팩토링 * refactor : collection context 로 분리 * refactor : 안쓰는 import 삭제, 제외하는 컴포넌트 build 스크립트 추가 * refactor : useCallback 추가해서 렌더링 최소화 * fix : 필요없는 코드 삭제 * refactor: values함수 내부로 이동 * fix : flat 해주는 훅 삭제 * chore : changeset 추가 * fix : defaultValue 관련 수정, 드롭다운 width 스타일 수정 * feat : 스토리북 보완 * fix: 스토리북 수정 * refacotor: OptionList 에서 focus 로직 처리하도록 분리 * refactor : itemMap 객체에서 변경
- Loading branch information
Showing
15 changed files
with
557 additions
and
321 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"wowds-ui": patch | ||
--- | ||
|
||
DropDownOption 컴포넌트를 context 를 이용하여 리팩토링 합니다. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 39 additions & 13 deletions
52
packages/wow-ui/src/components/DropDown/DropDownOption.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
packages/wow-ui/src/components/DropDown/DropDownOptionList.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import { cva } from "@styled-system/css"; | ||
import { styled } from "@styled-system/jsx"; | ||
import { | ||
type KeyboardEvent, | ||
type PropsWithChildren, | ||
useCallback, | ||
useEffect, | ||
useRef, | ||
} from "react"; | ||
|
||
import { useCollection } from "./context/CollectionContext"; | ||
import { useDropDownContext } from "./context/DropDownContext"; | ||
|
||
interface DropDownWrapperProps extends PropsWithChildren { | ||
hasCustomTrigger?: boolean; | ||
} | ||
export const DropDownOptionList = ({ | ||
children, | ||
hasCustomTrigger, | ||
}: DropDownWrapperProps) => { | ||
const { open, setFocusedValue, focusedValue, handleSelect } = | ||
useDropDownContext(); | ||
const itemMap = useCollection(); | ||
const listRef = useRef<HTMLUListElement>(null); | ||
|
||
useEffect(() => { | ||
if (open && listRef.current) { | ||
listRef.current.focus(); | ||
} | ||
}, [open]); | ||
|
||
const updateFocusedValue = useCallback( | ||
(direction: number) => { | ||
const values = Array.from(itemMap.keys()); | ||
setFocusedValue((prevValue) => { | ||
const currentIndex = values.indexOf(prevValue ?? ""); | ||
const nextIndex = | ||
(currentIndex + direction + values.length) % values.length; | ||
return values[nextIndex] ?? ""; | ||
}); | ||
}, | ||
[itemMap, setFocusedValue] | ||
); | ||
|
||
const handleKeyDown = useCallback( | ||
(event: KeyboardEvent<HTMLUListElement>) => { | ||
if (!open) return; | ||
|
||
const { key } = event; | ||
|
||
if (key === "ArrowDown") { | ||
updateFocusedValue(1); | ||
event.preventDefault(); | ||
} else if (key === "ArrowUp") { | ||
updateFocusedValue(-1); | ||
event.preventDefault(); | ||
} else if (key === "Enter" && focusedValue !== null) { | ||
handleSelect(focusedValue, itemMap.get(focusedValue)); | ||
event.preventDefault(); | ||
} | ||
}, | ||
[open, focusedValue, updateFocusedValue, handleSelect, itemMap] | ||
); | ||
|
||
return ( | ||
<styled.ul | ||
display="flex" | ||
flexDirection="column" | ||
ref={listRef} | ||
role="listbox" | ||
style={{ visibility: open ? "visible" : "hidden" }} | ||
tabIndex={0} | ||
visibility={open ? "visible" : "hidden"} | ||
className={dropdownContentStyle({ | ||
type: hasCustomTrigger ? "custom" : "default", | ||
})} | ||
onKeyDown={handleKeyDown} | ||
> | ||
{children} | ||
</styled.ul> | ||
); | ||
}; | ||
|
||
const dropdownContentStyle = cva({ | ||
base: { | ||
position: "absolute", | ||
outline: "none", | ||
top: "calc(100% + 0.5rem)", | ||
left: 0, | ||
zIndex: "dropdown", | ||
maxHeight: "18.75rem", | ||
width: "100%", | ||
lg: { | ||
maxWidth: "22.375rem", | ||
}, | ||
smDown: { | ||
width: "100%", | ||
}, | ||
backgroundColor: "backgroundNormal", | ||
border: "1px solid", | ||
borderRadius: "sm", | ||
borderColor: "outline", | ||
overflow: "auto", | ||
_scrollbar: { | ||
width: "2px", | ||
}, | ||
_scrollbarThumb: { | ||
width: "2px", | ||
height: "65px", | ||
borderRadius: "sm", | ||
backgroundColor: "outline", | ||
}, | ||
_scrollbarTrack: { | ||
marginTop: "2px", | ||
marginBottom: "2px", | ||
}, | ||
}, | ||
variants: { | ||
type: { | ||
custom: { | ||
lg: {}, | ||
}, | ||
default: {}, | ||
}, | ||
}, | ||
defaultVariants: { type: "default" }, | ||
}); |
Oops, something went wrong.