From 8cb48332a7d7fa856009a2d3d06faeec6e087903 Mon Sep 17 00:00:00 2001 From: Khayal Alasgarov Date: Mon, 27 Jan 2025 15:41:40 -0800 Subject: [PATCH] feat: create textarea (long answer) component tckt-399 (#459) * feat: create long answer textarea component TCKT-399 * test(storybook): add stories for textarea component TCKT-399 * feat: create long answer textarea edit component TCKT-399 * test(storybook): add stories for textarea edit component TCKT-399 * feat: add schema config and validtion logic for textarea TCKT-399 * test: add schema config and validtion tests for textarea TCKT-399 --------- Co-authored-by: Khayal Alasgarov --- packages/common/src/locales/en/app.ts | 7 + .../components/TextArea/TextArea.stories.tsx | 81 +++++++++ .../src/Form/components/TextArea/index.tsx | 68 +++++++ packages/design/src/Form/components/index.tsx | 2 + .../FormEdit/AddPatternDropdown.tsx | 1 + .../TextAreaPattermEdit.stories.tsx | 52 ++++++ .../components/TextAreaPatternEdit/index.tsx | 149 +++++++++++++++ .../FormManager/FormEdit/components/index.ts | 2 + packages/forms/src/components.ts | 11 ++ packages/forms/src/patterns/index.ts | 3 + .../src/patterns/text-area/text-area.test.ts | 170 ++++++++++++++++++ .../forms/src/patterns/text-area/text-area.ts | 95 ++++++++++ 12 files changed, 641 insertions(+) create mode 100644 packages/design/src/Form/components/TextArea/TextArea.stories.tsx create mode 100644 packages/design/src/Form/components/TextArea/index.tsx create mode 100644 packages/design/src/FormManager/FormEdit/components/TextAreaPatternEdit/TextAreaPattermEdit.stories.tsx create mode 100644 packages/design/src/FormManager/FormEdit/components/TextAreaPatternEdit/index.tsx create mode 100644 packages/forms/src/patterns/text-area/text-area.test.ts create mode 100644 packages/forms/src/patterns/text-area/text-area.ts diff --git a/packages/common/src/locales/en/app.ts b/packages/common/src/locales/en/app.ts index 2e5558a5..1e4bb864 100644 --- a/packages/common/src/locales/en/app.ts +++ b/packages/common/src/locales/en/app.ts @@ -30,6 +30,13 @@ export const en = { displayName: 'Short answer', maxLength: 'Maximum length', }, + textarea: { + ...defaults, + displayName: 'Long answer', + maxLength: 'Maximum length', + hintLabel: 'Hint Text (optional)', + hint: 'The more specific you can be, the better. Use the space below and/or attach additional pages.', + }, packageDownload: { ...defaults, displayName: 'Package download', diff --git a/packages/design/src/Form/components/TextArea/TextArea.stories.tsx b/packages/design/src/Form/components/TextArea/TextArea.stories.tsx new file mode 100644 index 00000000..7e0e1d52 --- /dev/null +++ b/packages/design/src/Form/components/TextArea/TextArea.stories.tsx @@ -0,0 +1,81 @@ +import React from 'react'; +import { FormProvider, useForm } from 'react-hook-form'; +import type { Meta, StoryObj } from '@storybook/react'; + +import TextArea from './index.js'; + +const meta: Meta = { + title: 'patterns/TextArea', + component: TextArea, + decorators: [ + (Story, args) => { + const FormDecorator = () => { + const formMethods = useForm(); + return ( +
+ + + +
+ ); + }; + return ; + }, + ], + tags: ['autodocs'], +}; + +export default meta; + +export const Required: StoryObj = { + args: { + _patternId: '', + type: 'text-area', + inputId: 'test-textarea', + value: '', + label: 'Please enter your comments', + required: true, + maxLength: 500, + }, +}; + +export const NotRequired: StoryObj = { + args: { + _patternId: '', + type: 'text-area', + inputId: 'test-textarea', + value: '', + label: 'Please enter your comments', + required: false, + maxLength: 500, + }, +}; + +export const ErrorState: StoryObj = { + args: { + _patternId: '', + type: 'text-area', + inputId: 'test-textarea', + value: '', + label: 'Please enter your comments', + required: true, + maxLength: 500, + error: { + type: 'required', + message: 'This field is required', + }, + }, +}; + +export const WithHint: StoryObj = { + args: { + _patternId: '', + type: 'text-area', + inputId: 'test-textarea', + value: '', + label: 'Please enter your comments', + required: false, + maxLength: 500, + hint: 'This is a hint that provides additional context to the user.', + }, +}; diff --git a/packages/design/src/Form/components/TextArea/index.tsx b/packages/design/src/Form/components/TextArea/index.tsx new file mode 100644 index 00000000..4d13960a --- /dev/null +++ b/packages/design/src/Form/components/TextArea/index.tsx @@ -0,0 +1,68 @@ +import React from 'react'; +import classNames from 'classnames'; +import { useFormContext } from 'react-hook-form'; + +import { type TextAreaProps } from '@atj/forms'; +import { type PatternComponent } from '../../../Form/index.js'; + +const TextArea: PatternComponent = ({ + inputId, + label, + required, + error, + value, + hint, + maxLength, +}) => { + const { register } = useFormContext(); + const errorId = `input-error-message-${inputId}`; + const hintId = `input-hint-${inputId}`; + + return ( +
+
+
+ + {hint && ( +
+ {hint} +
+ )} + {error && ( + + )} +