diff options
| author | Armand Philippot <git@armandphilippot.com> | 2023-09-22 19:34:01 +0200 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2023-10-24 12:23:48 +0200 |
| commit | a6ff5eee45215effb3344cb5d631a27a7c0369aa (patch) | |
| tree | 5051747acf72318b4fc5c18d603e3757fbefdfdb /src/components/molecules/forms/labelled-field | |
| parent | 651ea4fc992e77d2f36b3c68f8e7a70644246067 (diff) | |
refactor(components): rewrite form components
Diffstat (limited to 'src/components/molecules/forms/labelled-field')
5 files changed, 248 insertions, 0 deletions
diff --git a/src/components/molecules/forms/labelled-field/index.ts b/src/components/molecules/forms/labelled-field/index.ts new file mode 100644 index 0000000..b0d9889 --- /dev/null +++ b/src/components/molecules/forms/labelled-field/index.ts @@ -0,0 +1 @@ +export * from './labelled-field'; diff --git a/src/components/molecules/forms/labelled-field/labelled-field.module.scss b/src/components/molecules/forms/labelled-field/labelled-field.module.scss new file mode 100644 index 0000000..bb37dc7 --- /dev/null +++ b/src/components/molecules/forms/labelled-field/labelled-field.module.scss @@ -0,0 +1,22 @@ +.wrapper { + display: flex; + gap: var(--spacing-2xs); + width: fit-content; + + &--inline { + flex-flow: row wrap; + align-items: center; + } + + &--inline#{&}--reverse { + flex-flow: row-reverse wrap; + } + + &--stack { + flex-flow: column wrap; + } + + &--stack#{&}--reverse { + flex-flow: column-reverse wrap; + } +} diff --git a/src/components/molecules/forms/labelled-field/labelled-field.stories.tsx b/src/components/molecules/forms/labelled-field/labelled-field.stories.tsx new file mode 100644 index 0000000..1f29830 --- /dev/null +++ b/src/components/molecules/forms/labelled-field/labelled-field.stories.tsx @@ -0,0 +1,130 @@ +import { ComponentMeta, ComponentStory } from '@storybook/react'; +import { ChangeEvent, useState } from 'react'; +import { Input, Label } from '../../../atoms'; +import { LabelledField } from './labelled-field'; + +/** + * LabelledField - Storybook Meta + */ +export default { + title: 'Molecules/Forms/Field', + component: LabelledField, + argTypes: { + className: { + control: { + type: 'text', + }, + description: 'Set additional classnames to the field.', + table: { + category: 'Styles', + }, + type: { + name: 'string', + required: false, + }, + }, + field: { + control: { + type: null, + }, + description: 'A component: Checkbox, Input, Select, Radio or TextArea.', + type: { + name: 'function', + required: true, + }, + }, + label: { + control: { + type: null, + }, + description: 'A Label component.', + type: { + name: 'function', + required: true, + }, + }, + isInline: { + control: { + type: 'boolean', + }, + description: 'Should the label and the field be inlined?', + table: { + category: 'Options', + defaultValue: { summary: false }, + }, + type: { + name: 'boolean', + required: false, + }, + }, + isReversedOrder: { + control: { + type: 'boolean', + }, + description: 'Should the label and the field be reversed?', + table: { + category: 'Options', + defaultValue: { summary: false }, + }, + type: { + name: 'boolean', + required: false, + }, + }, + }, +} as ComponentMeta<typeof LabelledField>; + +const Template: ComponentStory<typeof LabelledField> = ({ ...args }) => { + const id = 'sunt'; + const [value, setValue] = useState<string>(''); + const updateValue = (e: ChangeEvent<HTMLInputElement>) => { + setValue(e.target.value); + }; + + return ( + <LabelledField + {...args} + field={ + <Input + id={id} + name={id} + onChange={updateValue} + type="text" + value={value} + /> + } + label={<Label htmlFor={id}>A label</Label>} + /> + ); +}; + +/** + * Labelled Field Stories - Left + */ +export const Left = Template.bind({}); +Left.args = { + isInline: true, +}; + +/** + * Labelled Field Stories - Right + */ +export const Right = Template.bind({}); +Right.args = { + isInline: true, + isReversedOrder: true, +}; + +/** + * Labelled Field Stories - Top + */ +export const Top = Template.bind({}); +Top.args = {}; + +/** + * Labelled Field Stories - Bottom + */ +export const Bottom = Template.bind({}); +Bottom.args = { + isReversedOrder: true, +}; diff --git a/src/components/molecules/forms/labelled-field/labelled-field.test.tsx b/src/components/molecules/forms/labelled-field/labelled-field.test.tsx new file mode 100644 index 0000000..9e39e1f --- /dev/null +++ b/src/components/molecules/forms/labelled-field/labelled-field.test.tsx @@ -0,0 +1,32 @@ +import { render, screen } from '../../../../../tests/utils'; +import { Input, Label } from '../../../atoms'; +import { LabelledField } from './labelled-field'; + +const doNothing = () => { + // Do nothing +}; + +describe('LabelledField', () => { + it('renders a labelled field', () => { + const id = 'enim'; + const label = 'eum aliquam culpa'; + const value = 'vitae'; + + render( + <LabelledField + field={ + <Input + id={id} + name="text-field" + onChange={doNothing} + type="text" + value={value} + /> + } + label={<Label htmlFor={id}>{label}</Label>} + /> + ); + expect(screen.getByLabelText(label)).toBeInTheDocument(); + expect(screen.getByRole('textbox')).toHaveValue(value); + }); +}); diff --git a/src/components/molecules/forms/labelled-field/labelled-field.tsx b/src/components/molecules/forms/labelled-field/labelled-field.tsx new file mode 100644 index 0000000..af492b3 --- /dev/null +++ b/src/components/molecules/forms/labelled-field/labelled-field.tsx @@ -0,0 +1,63 @@ +import { FC, HTMLAttributes, ReactElement } from 'react'; +import { + CheckboxProps, + InputProps, + LabelProps, + RadioProps, + SelectProps, + TextAreaProps, +} from '../../../atoms'; +import styles from './labelled-field.module.scss'; + +export type LabelledFieldProps = Omit< + HTMLAttributes<HTMLDivElement>, + 'children' +> & { + /** + * The field. + */ + field: ReactElement< + CheckboxProps | InputProps | RadioProps | SelectProps | TextAreaProps + >; + /** + * Should the label and the field be inlined? + * + * @default false + */ + isInline?: boolean; + /** + * If true, the label is displayed after the field. + * + * @default false + */ + isReversedOrder?: boolean; + /** + * The field label. + */ + label: ReactElement<LabelProps>; +}; + +/** + * LabelledField component + * + * Render a field tied to a label. + */ +export const LabelledField: FC<LabelledFieldProps> = ({ + className = '', + field, + isInline = false, + isReversedOrder = false, + label, + ...props +}) => { + const layoutClass = isInline ? 'wrapper--inline' : 'wrapper--stack'; + const orderClass = isReversedOrder ? 'wrapper--reverse' : ''; + const wrapperClass = `${styles.wrapper} ${styles[layoutClass]} ${styles[orderClass]} ${className}`; + + return ( + <div {...props} className={wrapperClass}> + {label} + {field} + </div> + ); +}; |
