Skip to content

Commit

Permalink
feat(ffe-form-react): Add support for forwarding refs
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Requires react@^16.3

This commit adds support for forwarding refs to the `Input` and
`TextArea` components, so consumers can handle focus programmatically.
  • Loading branch information
Kristofer Selbekk committed Oct 2, 2018
1 parent bb71ad2 commit cdc58c6
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/ffe-form-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"peerDependencies": {
"@sb1/ffe-core": "^12.0.0 || ^13.0.0",
"@sb1/ffe-form": "^9.0.0",
"react": "^16.2.0"
"react": "^16.3.0"
},
"publishConfig": {
"access": "public"
Expand Down
6 changes: 4 additions & 2 deletions packages/ffe-form-react/src/Input.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import React from 'react';
import { bool, string } from 'prop-types';
import classNames from 'classnames';

const Input = ({ className, inline, textLike, ...rest }) => {
const Input = React.forwardRef((props, ref) => {
const { className, inline, textLike, ...rest } = props;
return (
<input
className={classNames(
Expand All @@ -11,10 +12,11 @@ const Input = ({ className, inline, textLike, ...rest }) => {
{ 'ffe-input-field--text-like': textLike },
className,
)}
ref={ref}
{...rest}
/>
);
};
});

Input.propTypes = {
className: string,
Expand Down
15 changes: 8 additions & 7 deletions packages/ffe-form-react/src/InputGroup.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('<InputGroup>', () => {

it('renders the given child', () => {
const wrapper = getWrapper();
expect(wrapper.find('Input')).toHaveLength(1);
expect(wrapper.find(Input)).toHaveLength(1);
});

it('renders a Label if a string passed from the label prop', () => {
Expand All @@ -37,7 +37,7 @@ describe('<InputGroup>', () => {
it('renders a Label with htmlFor set to the same value of the children id', () => {
const wrapper = getWrapper();

const inputId = wrapper.find('Input').prop('id');
const inputId = wrapper.find(Input).prop('id');
expect(wrapper.find('Label').prop('htmlFor')).toBe(inputId);
});

Expand All @@ -53,7 +53,7 @@ describe('<InputGroup>', () => {
expect(wrapper.find('ErrorFieldMessage').prop('children')).toBe(
'such error',
);
expect(wrapper.find('Input').prop('aria-invalid')).toBe('true');
expect(wrapper.find(Input).prop('aria-invalid')).toBe('true');
});

it('renders a Label component if passed a label prop', () => {
Expand All @@ -80,7 +80,7 @@ describe('<InputGroup>', () => {
expect(wrapper.find('ErrorFieldMessage').prop('children')).toBe(
'Some error',
);
expect(wrapper.find('Input').prop('aria-invalid')).toBe('true');
expect(wrapper.find(Input).prop('aria-invalid')).toBe('true');
});

it('renders a SuccessFieldMessage if passed as fieldMessage prop', () => {
Expand All @@ -94,7 +94,7 @@ describe('<InputGroup>', () => {
expect(wrapper.find('SuccessFieldMessage').prop('children')).toBe(
'Some success',
);
expect(wrapper.find('Input').prop('aria-invalid')).toBe('false');
expect(wrapper.find(Input).prop('aria-invalid')).toBe('false');
});

it('throws error when receiving multiple children', () => {
Expand Down Expand Up @@ -146,8 +146,9 @@ describe('<InputGroup>', () => {
</div>
),
});
expect(wrapper.find('Input').prop('id')).toHaveLength(42);
expect(wrapper.find('Input').prop('aria-invalid')).toBe('false');

expect(wrapper.find(Input).prop('id')).toHaveLength(42);
expect(wrapper.find(Input).prop('aria-invalid')).toBe('false');
});

it('renders a static tooltip if description is set', () => {
Expand Down
11 changes: 8 additions & 3 deletions packages/ffe-form-react/src/TextArea.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ import React from 'react';
import { string } from 'prop-types';
import classNames from 'classnames';

const TextArea = ({ className, ...rest }) => {
const TextArea = React.forwardRef((props, ref) => {
const { className, ...rest } = props;
return (
<textarea className={classNames('ffe-textarea', className)} {...rest} />
<textarea
className={classNames('ffe-textarea', className)}
ref={ref}
{...rest}
/>
);
};
});

TextArea.propTypes = {
className: string,
Expand Down

0 comments on commit cdc58c6

Please sign in to comment.