Skip to content

Commit

Permalink
feat: add inputRequired property to React Textarea
Browse files Browse the repository at this point in the history
  • Loading branch information
Robbert authored and Yolijn committed Oct 12, 2024
1 parent a26f139 commit 4c0f337
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/rotten-crews-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@utrecht/component-library-react": minor
---

Add `inputRequired` property to React `Textarea`.
35 changes: 32 additions & 3 deletions packages/component-library-react/src/Textarea.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ describe('Textarea', () => {

it('can have a invalid state in CSS', () => {
const handleChange = () => {};
const { container } = render(<Textarea required onChange={handleChange} />);
const { container } = render(<Textarea inputRequired onChange={handleChange} />);

const textarea = container.querySelector(':invalid');

Expand Down Expand Up @@ -222,12 +222,41 @@ describe('Textarea', () => {
expect(textarea).not.toHaveAttribute('required');
});

it('can have a required state in CSS', () => {
it('does not have a required state in CSS', () => {
const { container } = render(<Textarea required />);

const textarea = container.querySelector(':required');

expect(textarea).toBeInTheDocument();
expect(textarea).not.toBeInTheDocument();
});
});

describe('inputRequired variant', () => {
it('can have a required state', () => {
render(<Textarea inputRequired />);
const textbox = screen.getByRole('textbox');
expect(textbox).toBeRequired();
});
it('is not required by default', () => {
render(<Textarea />);
const textbox = screen.getByRole('textbox');
expect(textbox).not.toBeRequired();
});
it('renders a design system BEM modifier class name', () => {
const { container } = render(<Textarea inputRequired />);
const textbox = container.querySelector('.utrecht-textarea');
expect(textbox).toHaveClass('utrecht-textarea--required');
});
it('omits non-essential required attributes when not required', () => {
render(<Textarea inputRequired={false} />);
const textbox = screen.getByRole('textbox');
expect(textbox).not.toHaveAttribute('aria-required');
expect(textbox).not.toHaveAttribute('required');
});
it('can have a required state in CSS', () => {
const { container } = render(<Textarea inputRequired />);
const textbox = container.querySelector(':required');
expect(textbox).toBeInTheDocument();
});
});

Expand Down
8 changes: 5 additions & 3 deletions packages/component-library-react/src/Textarea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import clsx from 'clsx';
import { ForwardedRef, forwardRef, TextareaHTMLAttributes } from 'react';

export interface TextareaProps extends TextareaHTMLAttributes<HTMLTextAreaElement> {
inputRequired?: boolean;
invalid?: boolean;
}

export const Textarea = forwardRef(
(
{ dir, disabled, invalid, readOnly, required, className, ...restProps }: TextareaProps,
{ dir, disabled, inputRequired, invalid, readOnly, required, className, ...restProps }: TextareaProps,
ref: ForwardedRef<HTMLTextAreaElement>,
) => (
<textarea
Expand All @@ -19,13 +20,14 @@ export const Textarea = forwardRef(
disabled && 'utrecht-textarea--disabled',
invalid && 'utrecht-textarea--invalid',
readOnly && 'utrecht-textarea--readonly',
required && 'utrecht-textarea--required',
(required || inputRequired) && 'utrecht-textarea--required',
className,
)}
dir={dir ?? 'auto'}
disabled={disabled}
readOnly={readOnly}
required={required}
aria-required={required ? required : undefined}
required={inputRequired}
aria-invalid={invalid || undefined}
/>
),
Expand Down

0 comments on commit 4c0f337

Please sign in to comment.