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

fix(Textarea): update defaultValue and value props logic and stories #1680

Merged
merged 2 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions src/components/Textarea/Textarea.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import { IconClipboard16, IconNook16, IconQuestionMark16 } from '@foundation/Icon';
import { Textarea } from './Textarea';
import { Validation } from '@utilities/validation';
import { Validation, validationClassMap } from '@utilities/validation';
import { ExtraAction } from 'src/types/input';

const TEXTAREA_ID = '[data-test-id=fondue-textarea]';
const TEXTAREA_DECORATOR_ID = '[data-test-id=fondue-textarea-decorator]';
Expand All @@ -13,7 +14,7 @@ const DEFAULT_TEXT = 'I am some text text.';
const PLACEHOLDER = 'Enter some text in the textarea';
const INPUT_TEXT = 'I am some input text';
const ROW_HEIGHT = 20;
const EXTRA_ACTIONS = [
const EXTRA_ACTIONS: ExtraAction[] = [
{
icon: <IconClipboard16 />,
title: 'Save to Clipboard',
Expand Down Expand Up @@ -48,6 +49,11 @@ describe('Textarea Unit tests', () => {
cy.get(TEXTAREA_ID).should('have.value', DEFAULT_TEXT);
});

it('sets and gets the value', () => {
cy.mount(<Textarea id="12345" />);
cy.get(TEXTAREA_ID).should('have.attr', 'id', '12345');
});

it('should allow for text to be typed', () => {
cy.mount(<Textarea />);
cy.get(TEXTAREA_ID).type(INPUT_TEXT);
Expand All @@ -64,11 +70,6 @@ describe('Textarea Unit tests', () => {
cy.get(TEXTAREA_DECORATOR_ID).should('be.visible');
});

it('should render status icon', () => {
cy.mount(<Textarea status={Validation.Error} />);
cy.get(TEXTAREA_STATUS_ICON_ID).should('be.visible');
});

it('has the required attribute', () => {
cy.mount(<Textarea required={true} />);
cy.get(TEXTAREA_ID).should('have.attr', 'required');
Expand Down Expand Up @@ -199,4 +200,14 @@ describe('Textarea Unit tests', () => {
cy.mount(<Textarea extraActions={EXTRA_ACTIONS} />);
cy.get(TEXTAREA_EXTRA_ACTION_ID).should('be.visible');
});

it('should render the correct validation styling', () => {
cy.mount(<Textarea status={Validation.Error} />);
cy.get(TEXTAREA_ID).should('have.class', validationClassMap[Validation.Error]);
});

it('should render status icon', () => {
cy.mount(<Textarea status={Validation.Error} />);
cy.get(TEXTAREA_STATUS_ICON_ID).should('be.visible');
});
});
17 changes: 16 additions & 1 deletion src/components/Textarea/Textarea.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ export default {
defaultValue: { summary: undefined },
},
},
value: {
type: 'string',
description: 'Value set by parent',
table: {
type: { summary: 'string | undefined' },
defaultValue: { summary: undefined },
},
},
disabled: {
type: 'boolean',
table: {
Expand Down Expand Up @@ -152,6 +160,7 @@ export default {
type: 'function',
description: 'Callback function to return current value on the `Textarea`',
table: {
type: { summary: '((value: string) => void) | undefined' },
defaultValue: { summary: undefined },
},
},
Expand Down Expand Up @@ -206,7 +215,13 @@ export default {
} as Meta<TextareaProps>;

const TextareaTemplate: StoryFn<TextareaProps> = (args) => {
const [input, setInput] = useState<string | undefined>(undefined);
const { value } = args;
const [input, setInput] = useState<string | undefined>(value ?? undefined);
useEffect(() => {
if (value) {
setInput(value);
}
}, [value, setInput]);

return (
<Box className="tw-w-[80%] tw-ml-auto tw-mr-auto">
Expand Down
12 changes: 10 additions & 2 deletions src/components/Textarea/Textarea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ export const Textarea = ({
focusOnMount && textareaRef.current?.focus();
}, [focusOnMount, textareaRef]);

useEffect(() => {
const isValueSet = value !== textareaRef.current?.value;
if (textareaRef.current && value && !isValueSet) {
textareaRef.current.value = value;
}
}, [value, textareaRef]);

const autosizeProps = { minRows, maxRows };

const getPaddingRight = () => {
Expand Down Expand Up @@ -145,7 +152,8 @@ export const Textarea = ({
readOnly={readOnly}
ref={textareaRef}
required={required}
value={defaultValue ?? value}
defaultValue={defaultValue}
value={value}
placeholder={placeholder}
onBlur={onBlur}
onChange={handleOnChange}
Expand All @@ -162,12 +170,12 @@ export const Textarea = ({
aria-label={dataTestId}
data-test-id={dataTestId}
className={merge([
InputStyles.base,
hugWidth ? '' : InputStyles.width,
minRows ? '' : InputStyles.height,
InputStyles.disabled,
InputStyles.readOnly,
InputStyles.element,
InputStyles.base,
InputStyles.focus,
InputStyles.hover,
isFocusVisible && FOCUS_STYLE,
Expand Down
Loading