Skip to content

Commit

Permalink
Fixing value prop for text input & text area (EightfoldAI#204)
Browse files Browse the repository at this point in the history
* Fixing value prop for text input & text area


Co-authored-by: darora <>
  • Loading branch information
darora-eightfold authored Jun 23, 2022
1 parent fc660f9 commit a007990
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
9 changes: 5 additions & 4 deletions src/components/Inputs/TextArea/TextArea.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, {useState} from 'react';
import { Stories } from '@storybook/addon-docs';
import { ComponentStory, ComponentMeta } from '@storybook/react';
import { TextArea, TextInputTheme, TextInputWidth } from '../index';
Expand Down Expand Up @@ -74,9 +74,10 @@ export default {
},
} as ComponentMeta<typeof TextArea>;

const Text_Area_Story: ComponentStory<typeof TextArea> = (args) => (
<TextArea {...args} />
);
const Text_Area_Story: ComponentStory<typeof TextArea> = (args) => {
const [val, setVal] = useState(args.value);
return <TextArea {...args} value={val} onChange={(e) => setVal(e.target.value)} />
};

export const Text_Area = Text_Area_Story.bind({});

Expand Down
8 changes: 6 additions & 2 deletions src/components/Inputs/TextArea/TextArea.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FC, Ref, useState } from 'react';
import React, {FC, Ref, useEffect, useState} from 'react';
import { Icon, IconName } from '../../Icon';
import { Label } from '../../Label';
import { TextInputWidth, TextAreaProps, TextInputTheme } from '../index';
Expand Down Expand Up @@ -39,6 +39,7 @@ export const TextArea: FC<TextAreaProps> = React.forwardRef(
ref: Ref<HTMLTextAreaElement>
) => {
const [textAreaId] = useState<string>(uniqueId(id || 'textarea-'));
const [inputValue, setInputValue] = useState(value);

const textAreaClassNames: string = mergeClasses([
classNames,
Expand All @@ -55,6 +56,8 @@ export const TextArea: FC<TextAreaProps> = React.forwardRef(
},
]);

useEffect(() => setInputValue(value), [value]);

const debouncedChange = useDebounce<
React.ChangeEvent<HTMLTextAreaElement>
>(
Expand All @@ -70,6 +73,7 @@ export const TextArea: FC<TextAreaProps> = React.forwardRef(
// Reference: https://reactjs.org/docs/legacy-event-pooling.html
const handleChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
e.persist();
setInputValue(e.target.value);
debouncedChange(e);
};

Expand Down Expand Up @@ -98,7 +102,7 @@ export const TextArea: FC<TextAreaProps> = React.forwardRef(
style={style}
rows={textAreaRows}
tabIndex={0}
value={value}
value={inputValue}
/>
{enableExpand && (
<Icon
Expand Down
9 changes: 5 additions & 4 deletions src/components/Inputs/TextInput/TextInput.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useState } from 'react';
import { Stories } from '@storybook/addon-docs';
import { ComponentStory, ComponentMeta } from '@storybook/react';
import { IconName } from '../../Icon';
Expand Down Expand Up @@ -106,9 +106,10 @@ export default {
},
} as ComponentMeta<typeof TextInput>;

const Text_Input_Story: ComponentStory<typeof TextInput> = (args) => (
<TextInput {...args} />
);
const Text_Input_Story: ComponentStory<typeof TextInput> = (args) => {
const [val, setVal] = useState(args.value);
return <TextInput {...args} value={val} onChange={(e) => setVal(e.target.value)} />
};

export const Text_Input = Text_Input_Story.bind({});

Expand Down
7 changes: 6 additions & 1 deletion src/components/Inputs/TextInput/TextInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ export const TextInput: FC<TextInputProps> = React.forwardRef(
},
ref: Ref<HTMLInputElement>
) => {
const [inputValue, setInputValue] = useState(value);

const [clearButtonShown, _setClearButtonShown] =
useState<boolean>(false);
const [inputId] = useState<string>(uniqueId(id || 'input-'));
Expand Down Expand Up @@ -120,6 +122,7 @@ export const TextInput: FC<TextInputProps> = React.forwardRef(
]);

useEffect(() => {
setInputValue(value);
if (value?.toString()?.length > 0) {
return setClearButtonShown(true);
}
Expand All @@ -138,6 +141,7 @@ export const TextInput: FC<TextInputProps> = React.forwardRef(
if (!!inputField) {
(inputField as HTMLInputElement).value = '';
}
setInputValue('');
onClear?.(_event);
setClearButtonShown(false);
};
Expand Down Expand Up @@ -167,6 +171,7 @@ export const TextInput: FC<TextInputProps> = React.forwardRef(
// Reference: https://reactjs.org/docs/legacy-event-pooling.html
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
e.persist();
setInputValue(e.target.value);
debouncedChange(e);
};

Expand Down Expand Up @@ -194,7 +199,7 @@ export const TextInput: FC<TextInputProps> = React.forwardRef(
style={style}
tabIndex={0}
type={numbersOnly ? 'number' : htmlType}
value={value}
value={inputValue}
readOnly={readonly}
/>
{iconProps && (
Expand Down

0 comments on commit a007990

Please sign in to comment.