-
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.
✨ feat: implement TagSelect context and components with type definitions
- Loading branch information
Showing
19 changed files
with
383 additions
and
210 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,4 +1,5 @@ | ||
export type TagProps = { | ||
id: string | number; | ||
label: string; | ||
color?: | ||
| 'gray' | ||
|
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,136 +1,14 @@ | ||
import { | ||
ComponentRef, | ||
FC, | ||
forwardRef, | ||
useEffect, | ||
useId, | ||
useImperativeHandle, | ||
useRef, | ||
} from 'react'; | ||
import { ChevronUp } from 'react-feather'; | ||
import { FC, forwardRef } from 'react'; | ||
|
||
import { Tag } from '@/components/Tag/Tag'; | ||
import { useTheme } from '@/contexts'; | ||
import { cn } from '@/utils'; | ||
|
||
import { useTagSelect } from './hooks/useTagSelect'; | ||
import { Wrapper } from './components'; | ||
import { TagSelectProvider } from './contexts'; | ||
import { TagSelectProps } from './TagSelect.types'; | ||
import { | ||
labelVariants, | ||
tagItemVariants, | ||
tagListVariants, | ||
tagSelectVariants, | ||
wrapperTagSelectVariants, | ||
} from './TagSelect.variants'; | ||
|
||
export const TagSelect: FC<TagSelectProps> = forwardRef< | ||
HTMLInputElement, | ||
TagSelectProps | ||
>( | ||
( | ||
{ | ||
label, | ||
name, | ||
options, | ||
placeholder = 'Select a value...', | ||
theme, | ||
labelClassName, | ||
wrapperClassName, | ||
}, | ||
ref, | ||
) => { | ||
const id = useId(); | ||
const { theme: themeContext } = useTheme(); | ||
const inheritTheme = theme ?? themeContext; | ||
const inputRef = useRef<ComponentRef<'input'>>(null); | ||
const { | ||
isOpen, | ||
selectedTag, | ||
value, | ||
wrapperRef, | ||
handleClickTag, | ||
handleOpenDropdown, | ||
} = useTagSelect(); | ||
|
||
useImperativeHandle(ref, () => inputRef.current!, [inputRef]); | ||
|
||
useEffect(() => { | ||
if (inputRef.current) { | ||
inputRef.current.value = value; | ||
} | ||
}, [value]); | ||
|
||
return ( | ||
<div | ||
ref={wrapperRef} | ||
className={cn( | ||
wrapperTagSelectVariants({ | ||
theme: inheritTheme, | ||
className: wrapperClassName, | ||
}), | ||
)} | ||
> | ||
{label ? ( | ||
<label | ||
htmlFor={name ?? id} | ||
className={cn( | ||
labelVariants({ | ||
theme: inheritTheme, | ||
className: labelClassName, | ||
}), | ||
)} | ||
onClick={() => handleOpenDropdown(true)} | ||
> | ||
{label} | ||
</label> | ||
) : null} | ||
|
||
<div | ||
id={name ?? id} | ||
className={cn(tagSelectVariants({ theme: inheritTheme }))} | ||
role="combobox" | ||
onClick={() => handleOpenDropdown()} | ||
aria-expanded={isOpen} | ||
> | ||
{!selectedTag ? ( | ||
<span className="text-base text-inherit">{placeholder}</span> | ||
) : ( | ||
<Tag {...selectedTag} /> | ||
)} | ||
|
||
<ChevronUp | ||
className={cn( | ||
'w-4 h-4 text-inherit transition-all duration-50', | ||
isOpen ? 'rotate-0' : 'rotate-180', | ||
)} | ||
/> | ||
</div> | ||
|
||
<input ref={inputRef} type="text" name={name} className="hidden" /> | ||
|
||
{isOpen ? ( | ||
<ul | ||
role="listbox" | ||
className={cn(tagListVariants({ theme: inheritTheme }))} | ||
> | ||
{options.map((tag) => ( | ||
<li | ||
className={cn(tagItemVariants({ theme: inheritTheme }))} | ||
role="option" | ||
> | ||
<button | ||
type="button" | ||
role="button" | ||
className="m-0 p-0 w-full" | ||
onClick={() => handleClickTag(tag)} | ||
> | ||
<Tag label={tag.label} color={tag.color} /> | ||
</button> | ||
</li> | ||
))} | ||
</ul> | ||
) : null} | ||
</div> | ||
); | ||
}, | ||
); | ||
>(({ ...delegated }, ref) => ( | ||
<TagSelectProvider> | ||
<Wrapper ref={ref} {...delegated} /> | ||
</TagSelectProvider> | ||
)); |
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,26 @@ | ||
import { FC } from 'react'; | ||
|
||
import { Tag } from '@/components/Tag/Tag'; | ||
import { useTheme } from '@/contexts'; | ||
import { cn } from '@/utils'; | ||
|
||
import { ItemProps } from './Item.types'; | ||
import { wrapperVariants } from './Item.variants'; | ||
|
||
export const Item: FC<ItemProps> = ({ option, theme, handleClickTag }) => { | ||
const { theme: contextTheme } = useTheme(); | ||
const inheritTheme = theme ?? contextTheme; | ||
|
||
return ( | ||
<li className={cn(wrapperVariants({ theme: inheritTheme }))} role="option"> | ||
<button | ||
type="button" | ||
role="button" | ||
className="m-0 p-0 w-full" | ||
onClick={() => handleClickTag(option)} | ||
> | ||
<Tag {...option} /> | ||
</button> | ||
</li> | ||
); | ||
}; |
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,9 @@ | ||
import { TagProps } from '@/components/Tag/Tag.types'; | ||
|
||
import { ListProps } from '../List/List.types'; | ||
|
||
export type ItemProps = { | ||
option: TagProps; | ||
theme: ListProps['theme']; | ||
handleClickTag: (option: TagProps) => 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { cva } from 'class-variance-authority'; | ||
|
||
export const wrapperVariants = cva( | ||
['cursor-pointer', 'py-1', 'px-2', 'last:mb-2', 'first:mt-2', 'h-full'], | ||
{ | ||
variants: { | ||
theme: { | ||
colony: 'hover:bg-red-100', | ||
kubefirst: 'hover:bg-purple-100', | ||
civo: '', | ||
}, | ||
}, | ||
defaultVariants: { | ||
theme: 'kubefirst', | ||
}, | ||
}, | ||
); |
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,26 @@ | ||
import { FC } from 'react'; | ||
|
||
import { cn } from '@/utils'; | ||
import { useTheme } from '@/contexts'; | ||
|
||
import { Item } from '../Item/Item'; | ||
|
||
import { ListProps } from './List.types'; | ||
import { wrapperVariants } from './List.variants'; | ||
|
||
export const List: FC<ListProps> = ({ theme, options, handleClickTag }) => { | ||
const { theme: contextTheme } = useTheme(); | ||
const inheritTheme = theme ?? contextTheme; | ||
|
||
return ( | ||
<ul role="listbox" className={cn(wrapperVariants({ theme: inheritTheme }))}> | ||
{options.map((option) => ( | ||
<Item | ||
theme={inheritTheme} | ||
option={option} | ||
handleClickTag={handleClickTag} | ||
/> | ||
))} | ||
</ul> | ||
); | ||
}; |
Oops, something went wrong.