Skip to content

Commit 34f900f

Browse files
authored
✨ feat: add textareaRef prop (#123)
1 parent fd61e2b commit 34f900f

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/components/textarea/Textarea.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
import React, { HTMLProps } from 'react';
1+
import React, { HTMLProps, MutableRefObject } from 'react';
22
import classNames from 'classnames';
33
import { FormElementProps } from '../../util/types/FormTypes';
44
import FormGroup from '../../util/FormGroup';
55

6-
type TextareaProps = HTMLProps<HTMLTextAreaElement> & FormElementProps;
6+
type TextareaProps = HTMLProps<HTMLTextAreaElement> &
7+
FormElementProps & { textareaRef?: MutableRefObject<HTMLTextAreaElement | null> };
78

89
const Textarea: React.FC<TextareaProps> = (props) => (
910
<FormGroup<TextareaProps> inputType="textarea" {...props}>
10-
{({ className, error, ...rest }) => (
11+
{({ className, error, textareaRef, ...rest }) => (
1112
<textarea
1213
className={classNames('nhsuk-textarea', { 'nhsuk-textarea--error': error }, className)}
14+
ref={textareaRef}
1315
{...rest}
1416
/>
1517
)}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import React, { createRef } from 'react';
2+
import { mount } from 'enzyme';
3+
import Textarea from '..';
4+
5+
describe('Textarea', () => {
6+
it('render with a given ref', () => {
7+
const ref = createRef<HTMLTextAreaElement>();
8+
const wrapper = mount(<Textarea textareaRef={ref} />);
9+
expect(wrapper.find('.nhsuk-textarea')).toHaveLength(1);
10+
expect(ref.current?.className).toContain('nhsuk-textarea');
11+
});
12+
});

0 commit comments

Comments
 (0)