Skip to content

Commit a7dfc8d

Browse files
committed
Fix #14: Auto grow textarea height
1 parent 0075ddf commit a7dfc8d

File tree

1 file changed

+40
-10
lines changed

1 file changed

+40
-10
lines changed

src/components/form.js

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -317,17 +317,47 @@ export class FormFileInput extends React.Component {
317317
}
318318

319319

320-
export function FormTextareaInput({label, help_text, error, inputRef, ...props}) {
320+
export class FormTextareaInput extends React.Component {
321+
constructor(props) {
322+
super(props);
321323

322-
delete props.type;
324+
if (!props.inputRef)
325+
this.inputRef = React.createRef();
326+
}
323327

324-
if (inputRef)
325-
props.ref = inputRef;
328+
handleChange = (e) => {
329+
this.updateHeight(e.target);
326330

327-
return (
328-
<div>
329-
{label && <label>{label}</label>}
330-
<textarea {...props} />
331-
</div>
332-
);
331+
if (this.props.onChange)
332+
this.props.onChange(e);
333+
}
334+
335+
updateHeight = (el) => {
336+
let offset = el.offsetHeight - el.clientHeight;
337+
el.style.height = 'auto';
338+
el.style.height = (el.scrollHeight + offset) + 'px';
339+
}
340+
341+
componentDidMount() {
342+
if (this.props.inputRef)
343+
this.updateHeight(this.props.inputRef.current);
344+
else
345+
this.updateHeight(this.inputRef.current);
346+
}
347+
348+
render() {
349+
let {label, help_text, error, inputRef, ...props} = this.props;
350+
351+
delete props.type;
352+
353+
props.ref = inputRef || this.inputRef;
354+
props.onChange = this.handleChange;
355+
356+
return (
357+
<div>
358+
{label && <label>{label}</label>}
359+
<textarea {...props} />
360+
</div>
361+
);
362+
}
333363
}

0 commit comments

Comments
 (0)