-
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.
- Loading branch information
Showing
13 changed files
with
326 additions
and
21 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
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import { ElementType } from 'react'; | ||
import { ElementType, MouseEvent } from 'react'; | ||
|
||
import { LabelProps } from '@/core/components/Label/types'; | ||
|
||
export interface ChipProps<T extends ElementType> extends LabelProps<T> { | ||
onDelete?: () => void; | ||
onDelete?: (e: MouseEvent<HTMLButtonElement>) => void; | ||
} |
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
74 changes: 74 additions & 0 deletions
74
src/core/components/Dropdown/DropdownMultiple/DropdownMultiple.stories.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,74 @@ | ||
import { Meta } from '@storybook/react'; | ||
import { MouseEvent, useState } from 'react'; | ||
|
||
import DropdownMultiple from './index'; | ||
import { | ||
ValueWithLabel, | ||
ValueWithLabelType, | ||
} from '@/core/components/Dropdown/DropdownMultiple/types'; | ||
|
||
const meta = { | ||
title: 'core/Dropdown/DropdownMultiple', | ||
component: DropdownMultiple, | ||
argTypes: {}, | ||
} satisfies Meta<typeof DropdownMultiple>; | ||
|
||
export default meta; | ||
|
||
export const Default = () => { | ||
const [currentValues, setCurrentValues] = useState<ValueWithLabel<number>[]>( | ||
[], | ||
); | ||
const data = [ | ||
{ value: 0, label: '2024년 5월 선정산' }, | ||
{ value: 1, label: '5월 BIZ 본정산' }, | ||
{ value: 2, label: '5월 비즈 선정산 진짜' }, | ||
{ value: 3, label: '5월 비즈 선정산 진짜 진짜' }, | ||
]; | ||
|
||
const handleDelete = (value: ValueWithLabelType) => { | ||
setCurrentValues((prev) => prev.filter(({ value: v }) => v !== value)); | ||
}; | ||
|
||
const items = data.map((item, idx) => { | ||
const { value, label } = item; | ||
const checked = currentValues.some((item) => item.value === value); | ||
|
||
return ( | ||
<DropdownMultiple.Item | ||
key={idx} | ||
checked={checked} | ||
onClick={(e: MouseEvent<HTMLLIElement>) => { | ||
e.stopPropagation(); | ||
|
||
if (!checked) { | ||
setCurrentValues((prev) => [...prev, item]); | ||
} else { | ||
setCurrentValues((prev) => | ||
prev.filter((prevItem) => prevItem !== item), | ||
); | ||
} | ||
}} | ||
> | ||
{label} | ||
</DropdownMultiple.Item> | ||
); | ||
}); | ||
|
||
return ( | ||
<DropdownMultiple | ||
label={'정산 선택'} | ||
trigger={ | ||
<DropdownMultiple.Trigger | ||
variant={'chip'} | ||
currentValues={currentValues} | ||
placeholder='선택해주세요' | ||
onDelete={handleDelete} | ||
/> | ||
} | ||
content={<DropdownMultiple.Items items={items} />} | ||
className={'w-[500px]'} | ||
required | ||
/> | ||
); | ||
}; |
36 changes: 36 additions & 0 deletions
36
src/core/components/Dropdown/DropdownMultiple/DropdownMultipleItem.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,36 @@ | ||
import clsx from 'clsx'; | ||
import { forwardRef, Ref } from 'react'; | ||
import { Check } from '@phosphor-icons/react'; | ||
|
||
import { DropdownSelectItemProps } from '@/core/components/Dropdown/DropdownSelect/types'; | ||
|
||
const DropdownMultipleItem = forwardRef( | ||
( | ||
{ className, children, checked, ...props }: DropdownSelectItemProps, | ||
ref: Ref<HTMLLIElement>, | ||
) => { | ||
return ( | ||
<li | ||
ref={ref} | ||
role='option' | ||
className={clsx( | ||
'flex cursor-pointer items-center justify-between text-body-01-regular text-gray-08 hover:font-bold', | ||
checked && 'font-bold', | ||
className, | ||
)} | ||
{...props} | ||
> | ||
{children} | ||
<Check | ||
weight='bold' | ||
className={clsx( | ||
'-mt-[2px] flex-shrink-0 text-gray-03', | ||
checked && 'text-primary-03', | ||
)} | ||
/> | ||
</li> | ||
); | ||
}, | ||
); | ||
|
||
export default DropdownMultipleItem; |
105 changes: 105 additions & 0 deletions
105
src/core/components/Dropdown/DropdownMultiple/DropdownMultipleTrigger.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,105 @@ | ||
import { CaretDown } from '@phosphor-icons/react'; | ||
import clsx from 'clsx'; | ||
import { forwardRef, MouseEvent, Ref, useContext } from 'react'; | ||
|
||
import Typography from '../../Typography'; | ||
import { DropdownContext } from '../DropdownBase'; | ||
import { DropdownMultipleTriggerProps, ValueWithLabelType } from './types'; | ||
import { DropdownContextValue } from '@/core/components/Dropdown/DropdownBase/types'; | ||
import { DROPDOWN_MULTIPLE_VARIANT } from '@/core/components/Dropdown/DropdownMultiple/constants'; | ||
import Chip from '@/core/components/Chip'; | ||
|
||
const DropdownMultipleTrigger = forwardRef( | ||
<T extends ValueWithLabelType>( | ||
{ | ||
variant = DROPDOWN_MULTIPLE_VARIANT['TEXT'], | ||
currentValues, | ||
onDelete, | ||
onClick, | ||
...props | ||
}: DropdownMultipleTriggerProps<T>, | ||
ref: Ref<HTMLDivElement>, | ||
) => { | ||
const { isToggle, readOnly, disabled, setIsToggle } = useContext( | ||
DropdownContext, | ||
) as DropdownContextValue; | ||
const { className, placeholder, ...rest } = props; | ||
const hasCurrentValues = currentValues.length > 0; | ||
const showPlaceholder = placeholder && !hasCurrentValues; | ||
const isDisabled = readOnly || disabled; | ||
const isVisibleContent = !readOnly && !disabled && isToggle; | ||
const isText = | ||
!showPlaceholder && variant === DROPDOWN_MULTIPLE_VARIANT['TEXT']; | ||
|
||
const onClickHandler = (e: MouseEvent<HTMLDivElement>) => { | ||
if (isDisabled) return; | ||
|
||
setIsToggle((v) => !v); | ||
onClick?.(e); | ||
}; | ||
|
||
return ( | ||
<div | ||
ref={ref} | ||
role={'button'} | ||
onClick={onClickHandler} | ||
className={clsx( | ||
'flex w-full items-center justify-between gap-x-2 overflow-hidden rounded-xl border border-gray-03 p-3', | ||
isDisabled ? '!cursor-not-allowed bg-gray-09' : 'cursor-pointer', | ||
className, | ||
)} | ||
aria-haspopup='listbox' | ||
aria-expanded={isToggle} | ||
aria-disabled={disabled} | ||
aria-readonly={readOnly} | ||
{...rest} | ||
> | ||
{showPlaceholder || isText ? ( | ||
<Typography | ||
theme='subhead-02-regular' | ||
color={!showPlaceholder && !isDisabled ? 'gray-08' : 'gray-05'} | ||
text={!showPlaceholder ? currentValues.join(', ') : placeholder} | ||
className={clsx( | ||
'flex-1 truncate text-start', | ||
isDisabled && 'mr-[1.75rem]', | ||
)} | ||
/> | ||
) : ( | ||
<ul | ||
className={clsx( | ||
'flex flex-wrap gap-2', | ||
isDisabled && 'mr-[1.75rem]', | ||
)} | ||
> | ||
{currentValues.map(({ label, value }) => ( | ||
<Chip | ||
element={'li'} | ||
key={value} | ||
label={label} | ||
size={'medium'} | ||
rounded={'rounded-6'} | ||
colorTheme={'secondary'} | ||
onDelete={(e: MouseEvent<HTMLButtonElement>) => { | ||
e.stopPropagation(); | ||
onDelete?.(value); | ||
}} | ||
/> | ||
))} | ||
</ul> | ||
)} | ||
{!isDisabled ? ( | ||
<CaretDown | ||
size='24' | ||
className={clsx( | ||
'flex-shrink-0 text-gray-06', | ||
isVisibleContent ? 'rotate-180' : 'rotate-0', | ||
)} | ||
weight='fill' | ||
/> | ||
) : null} | ||
</div> | ||
); | ||
}, | ||
); | ||
|
||
export default DropdownMultipleTrigger; |
4 changes: 4 additions & 0 deletions
4
src/core/components/Dropdown/DropdownMultiple/constants/index.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export const DROPDOWN_MULTIPLE_VARIANT = { | ||
TEXT: 'text', | ||
CHIP: 'chip', | ||
} as const; |
Oops, something went wrong.