-
-
Notifications
You must be signed in to change notification settings - Fork 99
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
Add ChipInput component #808
Changes from 1 commit
a6b83db
fdcec51
4062317
523edc6
ac1e8b7
b2f94a4
6d8077b
fef44ca
d080f7e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { ChipInput } from '@asyncapi/studio-ui'; | ||
|
||
export default { | ||
Check warning on line 3 in apps/design-system/src/components/ChipInput.stories.tsx GitHub Actions / Test NodeJS PR - ubuntu-latest
|
||
component: ChipInput, | ||
}; | ||
|
||
export const Default = () => <ChipInput initialChips={['production', 'platform']} />; |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,49 @@ | ||||
import { useState, KeyboardEvent, FunctionComponent } from 'react'; | ||||
|
||||
interface ChipInputProps { | ||||
initialChips?: string[]; | ||||
initialInputValue?: string; | ||||
} | ||||
|
||||
export const ChipInput: FunctionComponent<ChipInputProps> = ({ | ||||
initialChips = [], | ||||
initialInputValue = 'registr' | ||||
}) => { | ||||
const [chips, setChips] = useState<string[]>(initialChips); | ||||
const [inputValue, setInputValue] = useState<string>(initialInputValue); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the state of this component is going to be managed from outside. no state management should be done in the component. |
||||
|
||||
const handleKeyDown = (event: KeyboardEvent<HTMLInputElement>) => { | ||||
if (event.key === 'Enter' && inputValue.trim()) { | ||||
setChips((prevChips) => [...prevChips, inputValue.trim()]); | ||||
setInputValue(''); | ||||
} | ||||
}; | ||||
|
||||
const handleDelete = (chipToDelete: string) => () => { | ||||
setChips((chips) => chips.filter((chip) => chip !== chipToDelete)); | ||||
}; | ||||
|
||||
return ( | ||||
<div className="flex flex-wrap items-center p-2 bg-gray-900 rounded"> | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. based on the design, this component should have a border as well. |
||||
<div className="w-full text-gray-100 mb-2">Tags</div> | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
{chips.map((chip) => ( | ||||
<div key={chip} className="m-1 bg-gray-100 border-0.5 border-gray-400 text-gray-900 rounded px-3 py-1 flex items-center relative"> | ||||
<span className="mr-3">{chip}</span> | ||||
<button | ||||
onClick={handleDelete(chip)} | ||||
className="text-gray-900 absolute top-1/2 transform -translate-y-1/2 right-2 focus:outline-none" | ||||
style={{ fontSize: '18px' }} | ||||
>×</button> | ||||
</div> | ||||
))} | ||||
<input | ||||
type="text" | ||||
value={inputValue} | ||||
onChange={(e) => setInputValue(e.target.value)} | ||||
onKeyDown={handleKeyDown} | ||||
className="p-1 bg-gray-900 text-white rounded outline-none" | ||||
placeholder="Add a chip" | ||||
/> | ||||
</div> | ||||
); | ||||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest we add support for the following props: