From a6b83dba5baeeedc0fb05bd3c2599646e7760f60 Mon Sep 17 00:00:00 2001 From: Prince Rajpoot Date: Fri, 6 Oct 2023 02:14:21 +0530 Subject: [PATCH 1/8] added ChipInput component --- .../src/components/ChipInput.stories.tsx | 7 +++ packages/ui/components/ChipInput.tsx | 49 +++++++++++++++++++ packages/ui/components/index.tsx | 1 + 3 files changed, 57 insertions(+) create mode 100644 apps/design-system/src/components/ChipInput.stories.tsx create mode 100644 packages/ui/components/ChipInput.tsx diff --git a/apps/design-system/src/components/ChipInput.stories.tsx b/apps/design-system/src/components/ChipInput.stories.tsx new file mode 100644 index 000000000..1a3d2bd24 --- /dev/null +++ b/apps/design-system/src/components/ChipInput.stories.tsx @@ -0,0 +1,7 @@ +import { ChipInput } from '@asyncapi/studio-ui'; + +export default { + component: ChipInput, +}; + +export const Default = () => ; diff --git a/packages/ui/components/ChipInput.tsx b/packages/ui/components/ChipInput.tsx new file mode 100644 index 000000000..8c9876d50 --- /dev/null +++ b/packages/ui/components/ChipInput.tsx @@ -0,0 +1,49 @@ +import { useState, KeyboardEvent, FunctionComponent } from 'react'; + +interface ChipInputProps { + initialChips?: string[]; + initialInputValue?: string; +} + +export const ChipInput: FunctionComponent = ({ + initialChips = [], + initialInputValue = 'registr' +}) => { + const [chips, setChips] = useState(initialChips); + const [inputValue, setInputValue] = useState(initialInputValue); + + const handleKeyDown = (event: KeyboardEvent) => { + if (event.key === 'Enter' && inputValue.trim()) { + setChips((prevChips) => [...prevChips, inputValue.trim()]); + setInputValue(''); + } + }; + + const handleDelete = (chipToDelete: string) => () => { + setChips((chips) => chips.filter((chip) => chip !== chipToDelete)); + }; + + return ( +
+
Tags
+ {chips.map((chip) => ( +
+ {chip} + +
+ ))} + setInputValue(e.target.value)} + onKeyDown={handleKeyDown} + className="p-1 bg-gray-900 text-white rounded outline-none" + placeholder="Add a chip" + /> +
+ ); +}; diff --git a/packages/ui/components/index.tsx b/packages/ui/components/index.tsx index 50e048646..a1058b7ad 100644 --- a/packages/ui/components/index.tsx +++ b/packages/ui/components/index.tsx @@ -2,6 +2,7 @@ import './styles.css' // components +export * from './ChipInput' export * from './EditorSwitch' export * from './DropdownMenu' export * from './Logo' From fdcec514a0c4e69e2ebc322e00c6dd8fbc012d26 Mon Sep 17 00:00:00 2001 From: Prince Rajpoot Date: Sat, 7 Oct 2023 02:44:31 +0530 Subject: [PATCH 2/8] made changes --- .../src/components/ChipInput.stories.tsx | 28 ++++++++- packages/ui/components/ChipInput.tsx | 61 ++++++++++--------- 2 files changed, 58 insertions(+), 31 deletions(-) diff --git a/apps/design-system/src/components/ChipInput.stories.tsx b/apps/design-system/src/components/ChipInput.stories.tsx index 1a3d2bd24..422b01a92 100644 --- a/apps/design-system/src/components/ChipInput.stories.tsx +++ b/apps/design-system/src/components/ChipInput.stories.tsx @@ -1,7 +1,29 @@ -import { ChipInput } from '@asyncapi/studio-ui'; +import { useState } from "react"; +import { ChipInput } from "@asyncapi/studio-ui"; -export default { +const meta = { component: ChipInput, + parameters: { + layout: 'fullscreen', + backgrounds: { + default: 'light' + } + }, }; -export const Default = () => ; +export default meta; + +export const Default = () => { + const [currentChips, setCurrentChips] = useState(['production', 'plateform']); + + return ( +
+ +
+ ); +}; diff --git a/packages/ui/components/ChipInput.tsx b/packages/ui/components/ChipInput.tsx index 8c9876d50..b520d13b0 100644 --- a/packages/ui/components/ChipInput.tsx +++ b/packages/ui/components/ChipInput.tsx @@ -1,48 +1,53 @@ -import { useState, KeyboardEvent, FunctionComponent } from 'react'; +import { FunctionComponent, KeyboardEvent } from 'react'; interface ChipInputProps { - initialChips?: string[]; - initialInputValue?: string; + name: string; + id: string; + className?: string; + chips: string[]; + onChange: (chips: string[]) => void; + isDisabled?: boolean; + placeholder?: string; } -export const ChipInput: FunctionComponent = ({ - initialChips = [], - initialInputValue = 'registr' +export const ChipInput: FunctionComponent = ({ + name, + id, + className, + chips, + onChange, + isDisabled = false, + placeholder = 'Add a chip' }) => { - const [chips, setChips] = useState(initialChips); - const [inputValue, setInputValue] = useState(initialInputValue); - const handleKeyDown = (event: KeyboardEvent) => { - if (event.key === 'Enter' && inputValue.trim()) { - setChips((prevChips) => [...prevChips, inputValue.trim()]); - setInputValue(''); + if (event.key === 'Enter' && event.currentTarget.value.trim()) { + onChange([...chips, event.currentTarget.value.trim()]); + event.currentTarget.value = ''; } }; const handleDelete = (chipToDelete: string) => () => { - setChips((chips) => chips.filter((chip) => chip !== chipToDelete)); + const updatedChips = chips.filter(chip => chip !== chipToDelete); + onChange(updatedChips); }; return ( -
-
Tags
- {chips.map((chip) => ( -
- {chip} - +
+ {chips.map(chip => ( +
+ {chip} +
))} - setInputValue(e.target.value)} +
); From 40623179d1427c25b73ed53e955cdf4c345f7858 Mon Sep 17 00:00:00 2001 From: Prince Rajpoot Date: Tue, 10 Oct 2023 02:43:55 +0530 Subject: [PATCH 3/8] added multiple cases in design system --- .../src/components/ChipInput.stories.tsx | 55 +++++++++++++++++-- packages/ui/components/ChipInput.tsx | 6 +- 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/apps/design-system/src/components/ChipInput.stories.tsx b/apps/design-system/src/components/ChipInput.stories.tsx index 422b01a92..fa129f7f3 100644 --- a/apps/design-system/src/components/ChipInput.stories.tsx +++ b/apps/design-system/src/components/ChipInput.stories.tsx @@ -14,15 +14,62 @@ const meta = { export default meta; export const Default = () => { - const [currentChips, setCurrentChips] = useState(['production', 'plateform']); + const [currentChips, setCurrentChips] = useState([]); + + return ( +
+ +
+ ); +}; + +export const WithTags = () => { + const [currentChips, setCurrentChips] = useState(['production', 'platform']); + + return ( +
+ +
+ ); + }; + +export const WithSomeDefaultText = () => { + const [currentChips, setCurrentChips] = useState(['production', 'platform']); + + return ( +
+ +
+ ); +}; +export const WithCustomPlaceholder = () => { + const [currentChips, setCurrentChips] = useState([]); + return (
); diff --git a/packages/ui/components/ChipInput.tsx b/packages/ui/components/ChipInput.tsx index b520d13b0..f912854e7 100644 --- a/packages/ui/components/ChipInput.tsx +++ b/packages/ui/components/ChipInput.tsx @@ -8,6 +8,7 @@ interface ChipInputProps { onChange: (chips: string[]) => void; isDisabled?: boolean; placeholder?: string; + defaultValue?: string; } export const ChipInput: FunctionComponent = ({ @@ -17,7 +18,8 @@ export const ChipInput: FunctionComponent = ({ chips, onChange, isDisabled = false, - placeholder = 'Add a chip' + placeholder = 'Add Tags', + defaultValue = '' }) => { const handleKeyDown = (event: KeyboardEvent) => { if (event.key === 'Enter' && event.currentTarget.value.trim()) { @@ -47,7 +49,7 @@ export const ChipInput: FunctionComponent = ({ className="p-1 bg-gray-900 text-white rounded outline-none" placeholder={placeholder} disabled={isDisabled} - defaultValue={'registr'} + defaultValue={defaultValue} />
); From 523edc6870f5a86e49db7e5d4540d330cb72755a Mon Sep 17 00:00:00 2001 From: Prince Rajpoot Date: Tue, 10 Oct 2023 02:55:49 +0530 Subject: [PATCH 4/8] remove chip with delete and navigate through tab --- packages/ui/components/ChipInput.tsx | 34 +++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/packages/ui/components/ChipInput.tsx b/packages/ui/components/ChipInput.tsx index f912854e7..420e21afa 100644 --- a/packages/ui/components/ChipInput.tsx +++ b/packages/ui/components/ChipInput.tsx @@ -1,4 +1,4 @@ -import { FunctionComponent, KeyboardEvent } from 'react'; +import { FunctionComponent, KeyboardEvent, useRef } from 'react'; interface ChipInputProps { name: string; @@ -21,10 +21,32 @@ export const ChipInput: FunctionComponent = ({ placeholder = 'Add Tags', defaultValue = '' }) => { + const inputRef = useRef(null); + const handleKeyDown = (event: KeyboardEvent) => { if (event.key === 'Enter' && event.currentTarget.value.trim()) { onChange([...chips, event.currentTarget.value.trim()]); event.currentTarget.value = ''; + } else if (event.key === 'Backspace' && !event.currentTarget.value) { + // Handle delete if input is empty + onChange(chips.slice(0, -1)); + } else if (event.key === 'Tab') { + // Focus on the next chip + event.preventDefault(); + if (inputRef.current) { + const nextChip = inputRef.current.previousSibling as HTMLElement; + nextChip?.focus(); + } + } + }; + + const handleChipKeyDown = (index: number) => (event: KeyboardEvent) => { + if (event.key === 'Backspace') { + const updatedChips = [...chips]; + updatedChips.splice(index, 1); + onChange(updatedChips); + } else if (event.key === 'Tab' && index === chips.length - 1) { + inputRef.current?.focus(); } }; @@ -35,13 +57,19 @@ export const ChipInput: FunctionComponent = ({ return (
- {chips.map(chip => ( -
+ {chips.map((chip, index) => ( +
{chip}
))} Date: Tue, 10 Oct 2023 03:08:52 +0530 Subject: [PATCH 5/8] fix: now pressing tab will navigate to the first chip --- packages/ui/components/ChipInput.tsx | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/packages/ui/components/ChipInput.tsx b/packages/ui/components/ChipInput.tsx index 420e21afa..e208e3381 100644 --- a/packages/ui/components/ChipInput.tsx +++ b/packages/ui/components/ChipInput.tsx @@ -22,21 +22,17 @@ export const ChipInput: FunctionComponent = ({ defaultValue = '' }) => { const inputRef = useRef(null); + const firstChipRef = useRef(null); const handleKeyDown = (event: KeyboardEvent) => { if (event.key === 'Enter' && event.currentTarget.value.trim()) { onChange([...chips, event.currentTarget.value.trim()]); event.currentTarget.value = ''; } else if (event.key === 'Backspace' && !event.currentTarget.value) { - // Handle delete if input is empty onChange(chips.slice(0, -1)); - } else if (event.key === 'Tab') { - // Focus on the next chip + } else if (event.key === 'Tab' && !event.shiftKey) { event.preventDefault(); - if (inputRef.current) { - const nextChip = inputRef.current.previousSibling as HTMLElement; - nextChip?.focus(); - } + firstChipRef.current?.focus(); } }; @@ -45,8 +41,6 @@ export const ChipInput: FunctionComponent = ({ const updatedChips = [...chips]; updatedChips.splice(index, 1); onChange(updatedChips); - } else if (event.key === 'Tab' && index === chips.length - 1) { - inputRef.current?.focus(); } }; @@ -63,9 +57,10 @@ export const ChipInput: FunctionComponent = ({ className="m-1 bg-gray-800 text-white rounded px-2 py-1 flex items-center" tabIndex={0} onKeyDown={handleChipKeyDown(index)} + ref={index === 0 ? firstChipRef : undefined} > {chip} - +
))} Date: Tue, 10 Oct 2023 12:05:31 +0530 Subject: [PATCH 6/8] fix height inconsistency --- packages/ui/components/ChipInput.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/ui/components/ChipInput.tsx b/packages/ui/components/ChipInput.tsx index e208e3381..d54803e02 100644 --- a/packages/ui/components/ChipInput.tsx +++ b/packages/ui/components/ChipInput.tsx @@ -23,7 +23,7 @@ export const ChipInput: FunctionComponent = ({ }) => { const inputRef = useRef(null); const firstChipRef = useRef(null); - + const handleKeyDown = (event: KeyboardEvent) => { if (event.key === 'Enter' && event.currentTarget.value.trim()) { onChange([...chips, event.currentTarget.value.trim()]); @@ -50,7 +50,7 @@ export const ChipInput: FunctionComponent = ({ }; return ( -
+
{chips.map((chip, index) => (
Date: Tue, 10 Oct 2023 14:21:03 +0530 Subject: [PATCH 7/8] ui changes --- packages/ui/components/ChipInput.tsx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/ui/components/ChipInput.tsx b/packages/ui/components/ChipInput.tsx index d54803e02..047239359 100644 --- a/packages/ui/components/ChipInput.tsx +++ b/packages/ui/components/ChipInput.tsx @@ -50,11 +50,12 @@ export const ChipInput: FunctionComponent = ({ }; return ( -
+
{chips.map((chip, index) => (
Date: Tue, 10 Oct 2023 15:02:26 +0530 Subject: [PATCH 8/8] increase focused border --- packages/ui/components/ChipInput.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/ui/components/ChipInput.tsx b/packages/ui/components/ChipInput.tsx index 047239359..f3d45c694 100644 --- a/packages/ui/components/ChipInput.tsx +++ b/packages/ui/components/ChipInput.tsx @@ -54,8 +54,8 @@ export const ChipInput: FunctionComponent = ({ {chips.map((chip, index) => (