From 6c5c5a3f23a40fbb19d11e7d7226d5d8c6bacf82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Dombya?= <135591453+hervedombya@users.noreply.github.com> Date: Mon, 18 Dec 2023 15:40:38 +0100 Subject: [PATCH 1/2] Add InputList component and related files Remove unused import in inputlist.stories.tsx Fix disabled state in CopyButton component Add react-hook-form dependency and update input component Update inputlist stories --- package-lock.json | 24 +++- package.json | 3 +- .../buttonv2/CopyButton.component.tsx | 2 +- src/lib/components/inputlist/InputButtons.tsx | 111 ++++++++++++++++++ .../inputlist/InputList.component.tsx | 97 +++++++++++++++ src/lib/components/inputv2/inputv2.tsx | 8 +- src/lib/index.ts | 1 + stories/inputlist.stories.tsx | 57 +++++++++ 8 files changed, 297 insertions(+), 6 deletions(-) create mode 100644 src/lib/components/inputlist/InputButtons.tsx create mode 100644 src/lib/components/inputlist/InputList.component.tsx create mode 100644 stories/inputlist.stories.tsx diff --git a/package-lock.json b/package-lock.json index cb3c9c0d06..dbae1d495b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,8 @@ "dependencies": { "@floating-ui/dom": "^0.1.10", "@storybook/preview-api": "^7.5.3", - "framer-motion": "^4.1.17" + "framer-motion": "^4.1.17", + "react-hook-form": "^7.49.2" }, "devDependencies": { "@babel/cli": "^7.17.10", @@ -23924,6 +23925,22 @@ "react": ">=16.13.1" } }, + "node_modules/react-hook-form": { + "version": "7.49.2", + "resolved": "https://registry.npmjs.org/react-hook-form/-/react-hook-form-7.49.2.tgz", + "integrity": "sha512-TZcnSc17+LPPVpMRIDNVITY6w20deMdNi6iehTFLV1x8SqThXGwu93HjlUVU09pzFgZH7qZOvLMM7UYf2ShAHA==", + "engines": { + "node": ">=18", + "pnpm": "8" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/react-hook-form" + }, + "peerDependencies": { + "react": "^16.8.0 || ^17 || ^18" + } + }, "node_modules/react-input-autosize": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/react-input-autosize/-/react-input-autosize-3.0.0.tgz", @@ -46438,6 +46455,11 @@ "@babel/runtime": "^7.12.5" } }, + "react-hook-form": { + "version": "7.49.2", + "resolved": "https://registry.npmjs.org/react-hook-form/-/react-hook-form-7.49.2.tgz", + "integrity": "sha512-TZcnSc17+LPPVpMRIDNVITY6w20deMdNi6iehTFLV1x8SqThXGwu93HjlUVU09pzFgZH7qZOvLMM7UYf2ShAHA==" + }, "react-input-autosize": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/react-input-autosize/-/react-input-autosize-3.0.0.tgz", diff --git a/package.json b/package.json index c58e2fc77f..875df0fe6c 100644 --- a/package.json +++ b/package.json @@ -151,6 +151,7 @@ "dependencies": { "@floating-ui/dom": "^0.1.10", "@storybook/preview-api": "^7.5.3", - "framer-motion": "^4.1.17" + "framer-motion": "^4.1.17", + "react-hook-form": "^7.49.2" } } diff --git a/src/lib/components/buttonv2/CopyButton.component.tsx b/src/lib/components/buttonv2/CopyButton.component.tsx index dce9beb6bd..ff06b70cee 100644 --- a/src/lib/components/buttonv2/CopyButton.component.tsx +++ b/src/lib/components/buttonv2/CopyButton.component.tsx @@ -67,7 +67,7 @@ export const CopyButton = ({ } /> } - disabled={copyStatus === COPY_STATE_SUCCESS} + disabled={copyStatus === COPY_STATE_SUCCESS || props.disabled} onClick={() => copy(textToCopy)} type="button" tooltip={ diff --git a/src/lib/components/inputlist/InputButtons.tsx b/src/lib/components/inputlist/InputButtons.tsx new file mode 100644 index 0000000000..50e8a49ef9 --- /dev/null +++ b/src/lib/components/inputlist/InputButtons.tsx @@ -0,0 +1,111 @@ +import styled, { CSSProperties } from 'styled-components'; +import { Button } from '../buttonv2/Buttonv2.component'; +import { useCallback, useMemo } from 'react'; +import { Box } from '../box/Box'; +import { Icon } from '../icon/Icon.component'; + +const CustomButton = styled(Button)<{ isVisible?: boolean }>` + ${(props) => + !props.isVisible + ? ` + display: none; + ` + : ''} +`; +const isEmptyItem = (item) => item.key === '' && item.value === ''; + +type AddButtonProps = { + index: number; + items: Array; + insertEntry: () => void; + disabled?: boolean; + iconStyle?: CSSProperties; +}; + +export const AddButton = ({ + index, + items, + insertEntry, + disabled, + iconStyle, +}: AddButtonProps) => { + const itemsLength = items.length; + const itemsIndex = items[index]; + const itemsIndexKey = (items[index] as { key: string }).key; + const itemsIndexValue = (items[index] as { value: string }).value; + + const isDisabled = useMemo(() => { + if (itemsIndex && itemsIndexKey === '' && itemsIndexValue === '') { + return true; + } + return disabled || false; + }, [itemsIndex, itemsIndexKey, itemsIndexValue, disabled]); + + const isVisible = useMemo(() => { + return !(itemsLength > 0 && index !== itemsLength - 1); + }, [itemsLength, index]); + + const onClickFn = useCallback(() => { + if (!(itemsLength > 0 && index !== itemsLength - 1)) { + insertEntry(); + } + }, [itemsLength, index, insertEntry]); + + return ( + <> + {!isVisible && } + } + /> + + ); +}; +type SubButtonProps = { + index: number; + items: Array; + deleteEntry: (arg0: number) => void; + disabled?: boolean; + iconStyle?: CSSProperties; +}; +export const SubButton = ({ + index, + items, + deleteEntry, + disabled, + iconStyle, +}: SubButtonProps) => { + let isDisabled = disabled || false; + + if (items.length === 1 && isEmptyItem(items[0])) { + isDisabled = true; + } + + return ( +