-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from Principes-Artis-Mechanicae/hotfix/Radio
Feature: DropDown 컴포넌트 구현
- Loading branch information
Showing
19 changed files
with
228 additions
and
26 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,89 @@ | ||
import styled from "@emotion/styled"; | ||
import { DropDownProps } from "./DropDown"; | ||
|
||
export const DropDownWrapper = styled.div<Pick<DropDownProps, "width">>` | ||
position: relative; | ||
width: ${(props) => props.width}; | ||
`; | ||
|
||
export const DropDownContainer = styled.div<{ height: string; isOpened: boolean }>` | ||
box-sizing: border-box; | ||
display: flex; | ||
align-items: center; | ||
width: 100%; | ||
height: ${(props) => props.height}; | ||
padding: 0px 20px; | ||
border: 1px solid; | ||
border-radius: 12px; | ||
border-color: ${(props) => { | ||
if (props.isOpened) return "#476FF1"; | ||
else return "#EBEDEF"; | ||
}}; | ||
img { | ||
margin-left: auto; | ||
transform: ${(props) => { | ||
if (props.isOpened) return "rotate(180deg)"; | ||
else return "rotate(0deg)"; | ||
}}; | ||
transition: transform 0.3s ease-in-out; | ||
} | ||
&:hover { | ||
cursor: pointer; | ||
} | ||
`; | ||
|
||
export const DropDownItemsContainer = styled.div<{ isOpened: boolean; maxHeight: string }>` | ||
position: absolute; | ||
top: calc(100% + 10px); | ||
width: 100%; | ||
overflow: scroll; | ||
border-radius: 12px; | ||
border: ${(props) => { | ||
if (props.isOpened) return "1px solid #ebedef"; | ||
else return "none"; | ||
}}; | ||
max-height: ${(props) => { | ||
if (props.isOpened) return props.maxHeight; | ||
else return "0px"; | ||
}}; | ||
transition: max-height 0.3s ease-in-out; | ||
`; | ||
|
||
export const DropDownItemWrapper = styled.div<{ active: boolean }>` | ||
box-sizing: border-box; | ||
display: flex; | ||
align-items: center; | ||
width: 100%; | ||
height: 54px; | ||
padding: 0px 20px; | ||
&:hover { | ||
background-color: #fafafa; | ||
cursor: pointer; | ||
} | ||
font-weight: ${(props) => { | ||
if (props.active) return "bold"; | ||
else return "normal"; | ||
}}; | ||
color: ${(props) => { | ||
if (props.active) return "#476FF1"; | ||
else return "#021026"; | ||
}}; | ||
`; |
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,58 @@ | ||
import { useEffect, useState } from "react"; | ||
import { DropDownContainer, DropDownItemsContainer, DropDownItemWrapper, DropDownWrapper } from "./DropDown.style"; | ||
import { SelectedItem, useDropDown } from "../../context/DropDownContext"; | ||
import { Text } from "../../components/typography/Text"; | ||
import chevronDown from "../../assets/chevron_down.svg"; | ||
|
||
export interface DropDownProps { | ||
width: string; | ||
height: string; | ||
itemContainerHeight: string; | ||
|
||
children?: React.ReactNode; | ||
|
||
placeholder: string; | ||
onValueChange?: (value: SelectedItem) => void; | ||
} | ||
|
||
export interface DropDownItemProps extends SelectedItem {} | ||
|
||
export const DropDown = ({ | ||
width, | ||
height, | ||
children, | ||
placeholder, | ||
itemContainerHeight, | ||
onValueChange, | ||
}: DropDownProps) => { | ||
const [isOpened, setIsOpened] = useState<boolean>(false); | ||
const { selected } = useDropDown(); | ||
|
||
useEffect(() => { | ||
onValueChange && onValueChange(selected); | ||
}, [selected]); | ||
|
||
return ( | ||
<DropDownWrapper width={width}> | ||
<DropDownContainer height={height} isOpened={isOpened} onClick={() => setIsOpened((isOpened) => !isOpened)}> | ||
<Text size="l">{selected.index === -1 ? placeholder : selected.value}</Text> | ||
<img src={chevronDown} /> | ||
</DropDownContainer> | ||
<DropDownItemsContainer isOpened={isOpened} maxHeight={itemContainerHeight}> | ||
{children} | ||
</DropDownItemsContainer> | ||
</DropDownWrapper> | ||
); | ||
}; | ||
|
||
export const DropDownItem = ({ index, value }: DropDownItemProps) => { | ||
const { selected, setSelected } = useDropDown(); | ||
return ( | ||
<DropDownItemWrapper | ||
active={selected.index === index} | ||
onClick={() => setSelected({ ...selected, index, value })} | ||
> | ||
{value} | ||
</DropDownItemWrapper> | ||
); | ||
}; |
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,22 @@ | ||
import { createContext, useContext, useState } from "react"; | ||
|
||
export interface SelectedItem { | ||
index: number; | ||
value: string; | ||
} | ||
|
||
export const DropDownContext = createContext<{ | ||
selected: SelectedItem; | ||
setSelected: React.Dispatch<React.SetStateAction<SelectedItem>>; | ||
} | null>(null); | ||
|
||
export const useDropDown = () => { | ||
const context = useContext(DropDownContext); | ||
if (!context) throw new Error("useDropDown 은 DropDownContext 내부에서 사용되어야 합니다"); | ||
return context; | ||
}; | ||
|
||
export const DropDownContextProvider = ({ children }: { children: React.ReactNode }) => { | ||
const [selected, setSelected] = useState<SelectedItem>({ index: -1, value: "" }); | ||
return <DropDownContext.Provider value={{ selected, setSelected }}>{children}</DropDownContext.Provider>; | ||
}; |
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
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,40 @@ | ||
import { DropDownContextProvider } from "../context/DropDownContext"; | ||
import { DropDown, DropDownItem } from "../components/form/DropDown"; | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
|
||
const meta = { | ||
title: "Form/DropDown", | ||
component: DropDown, | ||
} satisfies Meta<typeof DropDown>; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof meta>; | ||
|
||
export const Default: Story = { | ||
args: { | ||
width: "440px", | ||
height: "54px", | ||
itemContainerHeight: "150px", | ||
placeholder: "카테고리를 선택해주세요", | ||
}, | ||
render: (args) => { | ||
return ( | ||
<DropDownContextProvider> | ||
<DropDown | ||
width={args.width} | ||
height={args.height} | ||
itemContainerHeight={args.itemContainerHeight} | ||
placeholder={args.placeholder} | ||
onValueChange={(value) => { | ||
console.log(value); | ||
}} | ||
> | ||
<DropDownItem index={1} value="프론트엔드 개발" /> | ||
<DropDownItem index={2} value="백엔드 개발" /> | ||
<DropDownItem index={3} value="앱 개발" /> | ||
<DropDownItem index={4} value="프로그램 개발" /> | ||
</DropDown> | ||
</DropDownContextProvider> | ||
); | ||
}, | ||
}; |
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
3 changes: 1 addition & 2 deletions
3
stories/RadioGroup.story.tsx → packages/stories/RadioGroup.story.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
2 changes: 1 addition & 1 deletion
2
stories/SearchBar.story.ts → packages/stories/SearchBar.story.ts
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 was deleted.
Oops, something went wrong.
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