forked from asyncapi/studio
-
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.
- Loading branch information
1 parent
786c802
commit 739dd5c
Showing
8 changed files
with
240 additions
and
13 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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Meta, StoryObj } from '@storybook/react'; | ||
import { fn } from '@storybook/test'; | ||
import { Chip } from '@asyncapi/studio-ui'; | ||
|
||
|
||
const meta: Meta<typeof Chip> = { | ||
component: Chip, | ||
parameters: { | ||
layout: "fullscreen", | ||
backgrounds: { | ||
default: "dark", | ||
}, | ||
}, | ||
args: { | ||
onDelete: fn() | ||
} | ||
} | ||
export default meta; | ||
|
||
|
||
type Story = StoryObj<typeof Chip> | ||
|
||
|
||
export const Default: Story = { | ||
args: { | ||
chip: 'Chip', | ||
}, | ||
} |
64 changes: 64 additions & 0 deletions
64
apps/design-system/src/components/DropdownChipInput.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,64 @@ | ||
import { Meta, StoryObj } from '@storybook/react'; | ||
import { DropdownChipInput } from '@asyncapi/studio-ui'; | ||
import { useState } from 'react'; | ||
|
||
const meta: Meta<typeof DropdownChipInput> = { | ||
component: DropdownChipInput, | ||
parameters: { | ||
layout: "fullscreen", | ||
backgrounds: { | ||
default: "light", | ||
}, | ||
} | ||
} | ||
export default meta; | ||
|
||
|
||
type Story = StoryObj<typeof DropdownChipInput> | ||
|
||
const OPTIONS = ["string", "number", "boolean", "object", "array", "null"]; | ||
|
||
export const Default: Story = { | ||
render: () => { | ||
const [currentChips, setCurrentChips] = useState<string[]>(["boolean"]); | ||
return ( | ||
<DropdownChipInput | ||
className='w-96' | ||
id="chip-input-id-chip-text" | ||
chips={currentChips} | ||
onChange={setCurrentChips} | ||
chipsOptions={OPTIONS}/> | ||
) | ||
} | ||
} | ||
|
||
|
||
export const Disabled: Story = { | ||
render: () => { | ||
const [currentChips, setCurrentChips] = useState<string[]>(["string"]); | ||
return ( | ||
<DropdownChipInput | ||
className='w-96' | ||
id="chip-input-id-chip-text" | ||
chips={currentChips} | ||
onChange={setCurrentChips} | ||
isDisabled={true} | ||
chipsOptions={OPTIONS}/> | ||
) | ||
} | ||
} | ||
|
||
export const WithPlaceholder: Story = { | ||
render: () => { | ||
const [currentChips, setCurrentChips] = useState<string[]>([]); | ||
return ( | ||
<DropdownChipInput | ||
className='w-96' | ||
placeholder='Add a chip' | ||
id="chip-input-id-chip-text" | ||
chips={currentChips} | ||
onChange={setCurrentChips} | ||
chipsOptions={OPTIONS}/> | ||
) | ||
} | ||
} |
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,35 @@ | ||
import React from "react" | ||
import { cn } from "@asyncapi/studio-utils" | ||
interface ChipInputProps { | ||
chip: string | ||
onDelete: (chip: string) => void | ||
} | ||
|
||
export const Chip = React.forwardRef<HTMLDivElement, React.HTMLAttributes<HTMLDivElement> & ChipInputProps>( | ||
({ chip, onDelete,className , ...props }, ref) => { | ||
return ( | ||
<div | ||
className={cn("m-1 w-fit bg-gray-100 text-gray-900 rounded px-2 py-1 flex items-center border border-gray-400 focus:border-blue-500 focus:border-2 focus:outline-none", className)} | ||
style={{ height: "28px", borderStyle: "solid" }} | ||
ref={ref} | ||
onClick={(e) => { | ||
console.log("Chip clicked") | ||
}} | ||
{...props} | ||
> | ||
<span>{chip}</span> | ||
<button | ||
onClick={(e) => { | ||
console.log("Delete button clicked") | ||
onDelete(chip) | ||
}} | ||
tabIndex={-1} | ||
className="ml-1 text-gray-400 focus:outline-none" | ||
aria-label="Close" | ||
> | ||
× | ||
</button> | ||
</div> | ||
) | ||
} | ||
) |
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,81 @@ | ||
"use client" | ||
|
||
import { FunctionComponent, KeyboardEvent, useRef } from 'react'; | ||
import { DropdownMenuCheckboxItemProps } from "@radix-ui/react-dropdown-menu" | ||
import { Chip } from './Chip'; | ||
import { | ||
DropdownMenu, | ||
DropdownMenuCheckboxItem, | ||
DropdownMenuContent, | ||
DropdownMenuTrigger, | ||
} from "./DropdownMenu" | ||
import { PlusCircleIcon } from './icons'; | ||
import { cn } from '@asyncapi/studio-utils'; | ||
|
||
type Checked = DropdownMenuCheckboxItemProps["checked"] | ||
|
||
interface DropdownChipInputProps { | ||
id: string; | ||
className?: string; | ||
chips: string[]; | ||
chipsOptions: string[]; | ||
placeholder?: string; | ||
onChange: (chips: string[]) => void; | ||
isDisabled?: boolean; | ||
} | ||
|
||
export const DropdownChipInput: FunctionComponent<DropdownChipInputProps> = ({ | ||
className, | ||
chips, | ||
chipsOptions, | ||
onChange, | ||
placeholder, | ||
isDisabled = false, | ||
}) => { | ||
const firstChipRef = useRef<HTMLDivElement>(null); | ||
|
||
|
||
const onChipCheckedChange = (chip: string) => { | ||
if (chips.includes(chip)) { | ||
handleDelete(chip); | ||
} else { | ||
onChange([...chips, chip]); | ||
} | ||
} | ||
const handleChipKeyDown = (index: number) => (event: KeyboardEvent<HTMLDivElement>) => { | ||
event.stopPropagation(); | ||
if (event.key === 'Backspace') { | ||
const updatedChips = [...chips]; | ||
updatedChips.splice(index, 1); | ||
onChange(updatedChips); | ||
} | ||
}; | ||
|
||
const handleDelete = (chipToDelete: string) => { | ||
const updatedChips = chips.filter(chip => chip !== chipToDelete); | ||
onChange(updatedChips); | ||
}; | ||
|
||
return ( | ||
<div className={cn("flex h-11 p-1 bg-gray-900 rounded border border-gray-800 items-center",isDisabled && "opacity-50 pointer-events-none", className)}> | ||
<div className='flex overflow-scroll no-scrollbar'> | ||
{chips.length === 0 && placeholder && <span className='text-gray-500'>{placeholder}</span>} | ||
{chips.map((chip, index) => ( | ||
<Chip key={chip} chip={chip} tabIndex={0} onDelete={handleDelete} onKeyDown={handleChipKeyDown(index)} ref={index === 0 ? firstChipRef : undefined} /> | ||
))} | ||
</div> | ||
<DropdownMenu> | ||
<DropdownMenuTrigger asChild> | ||
<PlusCircleIcon className='min-w-7 w-7 h-7 min-h-7 text-gray-500 ml-auto ' /> | ||
</DropdownMenuTrigger> | ||
<DropdownMenuContent> | ||
{chipsOptions.map((chipOption) => ( | ||
<DropdownMenuCheckboxItem checked={chips.includes(chipOption)} onCheckedChange={() => onChipCheckedChange(chipOption)}> | ||
{chipOption} | ||
</DropdownMenuCheckboxItem> | ||
))} | ||
</DropdownMenuContent> | ||
</DropdownMenu> | ||
</div> | ||
); | ||
}; |
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