Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(forms): Correct ref and prop passing for textareas #9233

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,29 @@ import PropTypes from 'prop-types';
import React from 'react';
import TextareaAutosize from 'react-autosize-textarea';
import styled from 'react-emotion';
import isPropValid from '@emotion/is-prop-valid';

import {inputStyles} from 'app/styles/input';

class TextareaOrAutosize extends React.Component {
static propTypes = {
/**
* Enable autosizing of the textarea.
*/
autosize: PropTypes.bool,

/**
* Number of rows to start with if autosize
*/
rows: PropTypes.number,

innerRef: PropTypes.func,
};

render() {
let {
autosize,
rows,
innerRef,
highlighted, // eslint-disable-line
stacked, // eslint-disable-line
inline, // eslint-disable-line
field, // eslint-disable-line
multiline, // eslint-disable-line
getValue, // eslint-disable-line
setValue, // eslint-disable-line
error, // eslint-disable-line
initialData, // eslint-disable-line
getData, // eslint-disable-line
extraHelp, // eslint-disable-line
...props
} = this.props;

if (autosize) {
return (
<TextareaAutosize
rows={rows ? rows : 2}
async={true}
innerRef={innerRef}
{...props}
/>
);
}

return <textarea {...props} ref={innerRef} />;
}
}

const TextArea = styled(TextareaOrAutosize)`
const TextAreaControl = React.forwardRef(
({autosize, rows, ...p}, ref) =>
autosize ? (
<TextareaAutosize async innerRef={ref} rows={rows ? rows : 2} {...p} />
) : (
<textarea ref={ref} {...p} />
)
);

TextAreaControl.propTypes = {
/**
* Enable autosizing of the textarea.
*/
autosize: PropTypes.bool,
};

const propFilter = p => ['autosize', 'rows', 'maxRows'].includes(p) || isPropValid(p);

const TextArea = styled(TextAreaControl, {shouldForwardProp: propFilter})`
${inputStyles};
`;

Expand Down