Skip to content
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

Merged
merged 9 commits into from
Oct 10, 2023
7 changes: 7 additions & 0 deletions apps/design-system/src/components/ChipInput.stories.tsx
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

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

Assign object to a variable before exporting as module default

Check warning on line 3 in apps/design-system/src/components/ChipInput.stories.tsx

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - macos-latest

Assign object to a variable before exporting as module default
component: ChipInput,
};

export const Default = () => <ChipInput initialChips={['production', 'platform']} />;
49 changes: 49 additions & 0 deletions packages/ui/components/ChipInput.tsx
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;
Copy link
Member

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:

Suggested change
initialChips?: string[];
initialInputValue?: string;
name: string;
id: string;
className: string;
onChange: function;
isDisabled: boolean;
initialChips?: string[];
initialInputValue?: string;

}

export const ChipInput: FunctionComponent<ChipInputProps> = ({
initialChips = [],
initialInputValue = 'registr'
}) => {
const [chips, setChips] = useState<string[]>(initialChips);
const [inputValue, setInputValue] = useState<string>(initialInputValue);
Copy link
Member

Choose a reason for hiding this comment

The 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">
Copy link
Member

Choose a reason for hiding this comment

The 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>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<div className="w-full text-gray-100 mb-2">Tags</div>

{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>
);
};
1 change: 1 addition & 0 deletions packages/ui/components/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import './styles.css'

// components
export * from './ChipInput'
export * from './EditorSwitch'
export * from './DropdownMenu'
export * from './Logo'
Expand Down