From 59d962d57b874fbde65b5a432f6a9aa8bfae94ca Mon Sep 17 00:00:00 2001 From: Arlindo Pereira Date: Thu, 21 Nov 2024 15:28:25 +0100 Subject: [PATCH] Move update logic to the component --- .../fields/InputField/InputField.module.scss | 2 +- .../fields/InputField/InputField.stories.tsx | 22 +++----- .../forms/fields/InputField/InputField.tsx | 51 +++++++++++++++++-- 3 files changed, 53 insertions(+), 22 deletions(-) diff --git a/src/components/forms/fields/InputField/InputField.module.scss b/src/components/forms/fields/InputField/InputField.module.scss index 57267c3..ffaacda 100644 --- a/src/components/forms/fields/InputField/InputField.module.scss +++ b/src/components/forms/fields/InputField/InputField.module.scss @@ -22,7 +22,7 @@ } } - .bk-input-field__tags-and-input { + .bk-input-field__container { display: flex; flex-direction: row; gap: bk.$spacing-2; diff --git a/src/components/forms/fields/InputField/InputField.stories.tsx b/src/components/forms/fields/InputField/InputField.stories.tsx index 6fd01f3..b97f7e1 100644 --- a/src/components/forms/fields/InputField/InputField.stories.tsx +++ b/src/components/forms/fields/InputField/InputField.stories.tsx @@ -60,30 +60,20 @@ export const InputWithTags: Story = { const [tags, setTags] = React.useState>(['Tag Title', 'Tag Title 2']); const [inputText, setInputText] = React.useState('Example'); - const onChange = (e: React.ChangeEvent) => { - setInputText(e.target.value); + const handleUpdate = (newInputText: string) => { + setInputText(newInputText); }; - const onKeyUp = (e: React.KeyboardEvent) => { - if (e.key === 'Backspace' && inputText === '') { - setTags(tags.slice(0,-1)); - } - if (e.key === 'Enter' && inputText !== '') { - setTags([...tags, inputText]); - setInputText(''); - } - }; - const onRemove = (index: number) => { - setTags(tags.filter((_, idx) => idx !== index)); + const handleUpdateTags = (newTags: string[]) => { + setTags(newTags); }; return ( ); } diff --git a/src/components/forms/fields/InputField/InputField.tsx b/src/components/forms/fields/InputField/InputField.tsx index a5e7210..e144924 100644 --- a/src/components/forms/fields/InputField/InputField.tsx +++ b/src/components/forms/fields/InputField/InputField.tsx @@ -30,8 +30,11 @@ export type InputFieldProps = ComponentProps<'input'> & { /** Tags to be displayed inside the input field */ tags?: undefined | string[], - /** Callback to remove a specific Tag, passed down to Tag component. */ - tagRemoveCallback?: (index: number) => void, + /** Callback to update the input value. Internally hooks to onChange */ + onUpdate?: undefined | ((arg0: string) => void), + + /** Callback to update the tags. Internally hooks to onKeyUp */ + onUpdateTags?: undefined | ((arg0: string[]) => void), }; /** * Input field. @@ -43,7 +46,8 @@ export const InputField = (props: InputFieldProps) => { labelProps = {}, wrapperProps = {}, tags = [], - tagRemoveCallback, + onUpdate = null, + onUpdateTags = null, ...inputProps } = props; @@ -55,6 +59,41 @@ export const InputField = (props: InputFieldProps) => { unstyled: tags && tags.length > 0, }; + const onChange = (e: React.ChangeEvent) => { + // first handle supplied onChange, if exists + if (inputProps.onChange) { + inputProps.onChange(e); + } + // then return value to onUpdate + if (onUpdate) { + onUpdate(e.target.value); + } + }; + + const onKeyUp = (e: React.KeyboardEvent) => { + // first handle supplied onKeyUp, if exists + if (inputProps.onKeyUp) { + inputProps.onKeyUp(e); + } + // then return value to onUpdateTags + if (onUpdateTags && onUpdate) { + const { value } = inputProps; + if (e.key === 'Backspace' && value === '') { + onUpdateTags(tags.slice(0,-1)); + } + if (e.key === 'Enter' && value !== '') { + onUpdateTags([...tags as string[], value as string]); + onUpdate(''); + } + } + }; + + const onRemoveTag = (index: number) => { + if (onUpdateTags) { + onUpdateTags(tags.filter((_, idx) => idx !== index)); + } + }; + return (
{ {label} } -
+
{tags && ( - tags.map((tag, idx) => tagRemoveCallback?.(idx)}/>) + tags.map((tag, idx) => onRemoveTag(idx)}/>) )}