-
Notifications
You must be signed in to change notification settings - Fork 0
feat(ui) - Add Combobox and ScrollArea #297
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
51642df
feat(ui): add Combobox component and integrate ScrollArea
kylengn acbc834
feat(ui): add Combobox component with ScrollArea
kylengn 0436e92
refactor(ui): simplify Combobox story by removing useMemo and using s…
kylengn 2ab1ffd
fix(ui): ScrollArea styling
kylengn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 @@ | ||
| --- | ||
| "@shipfox/react-ui": minor | ||
| --- | ||
|
|
||
| Add Combobox and ScrollArea |
135 changes: 135 additions & 0 deletions
135
libs/react/ui/src/components/combobox/combobox.stories.tsx
This file contains hidden or 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,135 @@ | ||
| import type {Meta, StoryObj} from '@storybook/react'; | ||
| import {Label} from 'components/label'; | ||
| import {useState} from 'react'; | ||
| import {Combobox, type ComboboxOption} from './combobox'; | ||
|
|
||
| const sampleItems: ComboboxOption[] = [ | ||
| {value: 'apache', label: 'apache'}, | ||
| {value: 'apache-superset', label: 'apache-superset'}, | ||
| {value: 'apaleo', label: 'apaleo'}, | ||
| {value: 'apollo', label: 'apollo'}, | ||
| {value: 'apple', label: 'apple'}, | ||
| {value: 'apache-kafka', label: 'apache-kafka'}, | ||
| {value: 'apex', label: 'apex'}, | ||
| {value: 'appsmith', label: 'appsmith'}, | ||
| {value: 'applitools', label: 'applitools'}, | ||
| {value: 'approzium', label: 'approzium'}, | ||
| {value: 'apify', label: 'apify'}, | ||
| {value: 'apicurio', label: 'apicurio'}, | ||
| {value: 'apitable', label: 'apitable'}, | ||
| {value: 'apollographql', label: 'apollographql'}, | ||
| {value: 'aptos', label: 'aptos'}, | ||
| ]; | ||
|
|
||
| const meta = { | ||
| title: 'Components/Combobox', | ||
| component: Combobox, | ||
| tags: ['autodocs'], | ||
| parameters: { | ||
| layout: 'centered', | ||
| }, | ||
| } satisfies Meta<typeof Combobox>; | ||
|
|
||
| export default meta; | ||
| type Story = StoryObj<typeof meta>; | ||
|
|
||
| export const Default: Story = { | ||
| args: {} as never, | ||
| render: () => { | ||
| const [value, setValue] = useState(''); | ||
|
|
||
| return ( | ||
| <div className="w-[80vw] md:w-500"> | ||
| <Label htmlFor="combobox-default">Search repositories</Label> | ||
| <Combobox | ||
| id="combobox-default" | ||
| options={sampleItems} | ||
| value={value} | ||
| onValueChange={setValue} | ||
| placeholder="Type to search..." | ||
| searchPlaceholder="Search repositories..." | ||
| emptyState="No repository found." | ||
| /> | ||
| </div> | ||
| ); | ||
| }, | ||
| }; | ||
|
|
||
| export const EmptyState: Story = { | ||
| args: {} as never, | ||
| render: () => { | ||
| const [value, setValue] = useState('abcxyz'); | ||
|
|
||
| return ( | ||
| <div className="w-[80vw] md:w-500"> | ||
| <Label htmlFor="combobox-empty">No results</Label> | ||
| <Combobox | ||
| id="combobox-empty" | ||
| options={[]} | ||
| value={value} | ||
| onValueChange={setValue} | ||
| placeholder="Type to search..." | ||
| searchPlaceholder="Search repositories..." | ||
| emptyState={ | ||
| <p className="px-4 whitespace-pre-wrap"> | ||
| Repository list is limited to 100.{' '} | ||
| <a | ||
| href="https://support.example.com" | ||
| target="_blank" | ||
| rel="noopener noreferrer" | ||
| className="underline text-foreground-neutral-base" | ||
| > | ||
| Contact us | ||
| </a>{' '} | ||
| for support. | ||
| </p> | ||
| } | ||
| /> | ||
| </div> | ||
| ); | ||
| }, | ||
| }; | ||
|
|
||
| export const LoadingState: Story = { | ||
| args: {} as never, | ||
| render: () => { | ||
| const [value, setValue] = useState(''); | ||
| return ( | ||
| <div className="w-[80vw] md:w-500"> | ||
| <Label htmlFor="combobox-loading">Loading</Label> | ||
| <Combobox | ||
| id="combobox-loading" | ||
| options={sampleItems} | ||
| value={value} | ||
| onValueChange={setValue} | ||
| placeholder="Type to search..." | ||
| searchPlaceholder="Search repositories..." | ||
| isLoading | ||
| /> | ||
| </div> | ||
| ); | ||
| }, | ||
| }; | ||
|
|
||
| export const DisabledState: Story = { | ||
| args: {} as never, | ||
| render: () => { | ||
| const [value, setValue] = useState('apache'); | ||
|
|
||
| return ( | ||
| <div className="w-[80vw] md:w-500"> | ||
| <Label htmlFor="combobox-disabled">Disabled</Label> | ||
| <Combobox | ||
| id="combobox-disabled" | ||
| options={sampleItems} | ||
| value={value} | ||
| onValueChange={setValue} | ||
| disabled | ||
| placeholder="Disabled input" | ||
| searchPlaceholder="Search repositories..." | ||
| emptyState="No results found" | ||
| /> | ||
| </div> | ||
| ); | ||
| }, | ||
| }; | ||
This file contains hidden or 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,124 @@ | ||
| import {ScrollArea} from 'components/scroll-area'; | ||
kylengn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import * as React from 'react'; | ||
| import {cn} from 'utils/cn'; | ||
| import { | ||
| Command, | ||
| CommandEmpty, | ||
| CommandGroup, | ||
| CommandInput, | ||
| CommandItem, | ||
| CommandList, | ||
| CommandTrigger, | ||
| type CommandTriggerProps, | ||
| } from '../command'; | ||
| import {Icon} from '../icon'; | ||
| import {Popover, PopoverContent, PopoverTrigger} from '../popover'; | ||
|
|
||
| export type ComboboxOption = { | ||
| value: string; | ||
| label: string; | ||
| }; | ||
|
|
||
| export type ComboboxProps = Omit<CommandTriggerProps, 'children' | 'placeholder'> & { | ||
| options: ComboboxOption[]; | ||
| value?: string; | ||
| onValueChange?: (value: string) => void; | ||
| placeholder?: string; | ||
| emptyState?: string | React.ReactNode; | ||
| searchPlaceholder?: string; | ||
| className?: string; | ||
| popoverClassName?: string; | ||
| align?: 'start' | 'center' | 'end'; | ||
| sideOffset?: number; | ||
| isLoading?: boolean; | ||
| }; | ||
|
|
||
| export function Combobox({ | ||
| options, | ||
| value, | ||
| onValueChange, | ||
| placeholder = 'Select option...', | ||
| emptyState = 'No option found.', | ||
| searchPlaceholder = 'Search...', | ||
| className, | ||
| popoverClassName, | ||
| align = 'start', | ||
| sideOffset = 4, | ||
| variant, | ||
| size, | ||
| isLoading, | ||
| ...triggerProps | ||
| }: ComboboxProps) { | ||
| const [open, setOpen] = React.useState(false); | ||
| const [internalValue, setInternalValue] = React.useState(''); | ||
|
|
||
| const isControlled = value !== undefined; | ||
| const currentValue = isControlled ? value : internalValue; | ||
|
|
||
| const handleValueChange = React.useCallback( | ||
| (newValue: string) => { | ||
| if (!isControlled) { | ||
| setInternalValue(newValue); | ||
| } | ||
| onValueChange?.(newValue); | ||
| }, | ||
| [isControlled, onValueChange], | ||
| ); | ||
|
|
||
| const selectedOption = options.find((option) => option.value === currentValue); | ||
|
|
||
| return ( | ||
| <Popover open={open} onOpenChange={setOpen}> | ||
| <PopoverTrigger asChild> | ||
| <CommandTrigger | ||
| variant={variant} | ||
| size={size} | ||
| placeholder={placeholder} | ||
| className={className} | ||
| isLoading={isLoading} | ||
| {...triggerProps} | ||
| > | ||
| {selectedOption?.label} | ||
| </CommandTrigger> | ||
| </PopoverTrigger> | ||
| <PopoverContent | ||
| className={cn('w-(--radix-popover-trigger-width) p-0', popoverClassName)} | ||
| align={align} | ||
| sideOffset={sideOffset} | ||
| onWheel={(e) => e.stopPropagation()} | ||
| onTouchStart={(e) => e.stopPropagation()} | ||
| onTouchMove={(e) => e.stopPropagation()} | ||
| > | ||
| <Command> | ||
| <CommandInput placeholder={searchPlaceholder} /> | ||
| <ScrollArea> | ||
| <CommandList className="max-h-300"> | ||
| <CommandEmpty>{emptyState}</CommandEmpty> | ||
| <CommandGroup> | ||
| {options.map((option) => ( | ||
| <CommandItem | ||
| key={option.value} | ||
| value={option.value} | ||
| onSelect={(selectedValue) => { | ||
| handleValueChange(currentValue === selectedValue ? '' : selectedValue); | ||
| setOpen(false); | ||
| }} | ||
| > | ||
| <Icon | ||
| name="check" | ||
| className={cn( | ||
| 'size-16 mr-8', | ||
| currentValue === option.value ? 'opacity-100' : 'opacity-0', | ||
| )} | ||
| /> | ||
| {option.label} | ||
| </CommandItem> | ||
| ))} | ||
| </CommandGroup> | ||
| </CommandList> | ||
| </ScrollArea> | ||
| </Command> | ||
| </PopoverContent> | ||
| </Popover> | ||
| ); | ||
| } | ||
This file contains hidden or 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 @@ | ||
| export * from './combobox'; |
This file contains hidden or 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 hidden or 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 hidden or 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 @@ | ||
| export * from './scroll-area'; |
This file contains hidden or 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,53 @@ | ||
| import * as ScrollAreaPrimitive from '@radix-ui/react-scroll-area'; | ||
| import type {ComponentProps} from 'react'; | ||
| import {cn} from 'utils/cn'; | ||
|
|
||
| function ScrollArea({ | ||
| className, | ||
| children, | ||
| ...props | ||
| }: ComponentProps<typeof ScrollAreaPrimitive.Root>) { | ||
| return ( | ||
| <ScrollAreaPrimitive.Root | ||
| data-slot="scroll-area" | ||
| className={cn('relative overflow-hidden', className)} | ||
| {...props} | ||
| > | ||
| <ScrollAreaPrimitive.Viewport | ||
| data-slot="scroll-area-viewport" | ||
| className="h-full w-full rounded-[inherit]" | ||
| > | ||
| {children} | ||
| </ScrollAreaPrimitive.Viewport> | ||
| <ScrollBar /> | ||
| <ScrollAreaPrimitive.Corner /> | ||
| </ScrollAreaPrimitive.Root> | ||
| ); | ||
| } | ||
|
|
||
| function ScrollBar({ | ||
| className, | ||
| orientation = 'vertical', | ||
| ...props | ||
| }: ComponentProps<typeof ScrollAreaPrimitive.ScrollAreaScrollbar>) { | ||
| return ( | ||
| <ScrollAreaPrimitive.ScrollAreaScrollbar | ||
| data-slot="scroll-area-scrollbar" | ||
| orientation={orientation} | ||
| className={cn( | ||
| 'flex touch-none p-px transition-colors select-none', | ||
| orientation === 'vertical' && 'h-full w-10 border-l border-l-transparent', | ||
| orientation === 'horizontal' && 'h-10 flex-col border-t border-t-transparent', | ||
| className, | ||
| )} | ||
| {...props} | ||
| > | ||
| <ScrollAreaPrimitive.ScrollAreaThumb | ||
| data-slot="scroll-area-thumb" | ||
| className="bg-border-neutral-strong relative flex-1 rounded-full" | ||
| /> | ||
| </ScrollAreaPrimitive.ScrollAreaScrollbar> | ||
| ); | ||
| } | ||
|
|
||
| export {ScrollArea, ScrollBar}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.