mirror of
https://github.com/kingomarnajjar/flex-template-web.git
synced 2026-07-25 22:37:18 +10:00
Update FieldTextInput: use defaultValue if uncontrolled
This commit is contained in:
parent
cb242413f8
commit
fb2c11cae2
1 changed files with 29 additions and 3 deletions
|
|
@ -1,5 +1,5 @@
|
|||
import React, { Component } from 'react';
|
||||
import { func, object, shape, string } from 'prop-types';
|
||||
import { bool, func, object, shape, string } from 'prop-types';
|
||||
import { Field } from 'react-final-form';
|
||||
import classNames from 'classnames';
|
||||
import { ValidationError, ExpandingTextarea } from '../../components';
|
||||
|
|
@ -22,6 +22,8 @@ class FieldTextInputComponent extends Component {
|
|||
input,
|
||||
meta,
|
||||
onUnmount,
|
||||
isUncontrolled,
|
||||
inputRef,
|
||||
...rest
|
||||
} = this.props;
|
||||
/* eslint-enable no-unused-vars */
|
||||
|
|
@ -41,6 +43,11 @@ class FieldTextInputComponent extends Component {
|
|||
|
||||
const fieldMeta = { touched: hasError, error: errorText };
|
||||
|
||||
// Uncontrolled input uses defaultValue instead of value.
|
||||
const { value: defaultValue, ...inputWithoutValue } = input;
|
||||
// Use inputRef if it is passed as prop.
|
||||
const refMaybe = inputRef ? { ref: inputRef } : {};
|
||||
|
||||
const inputClasses =
|
||||
inputRootClass ||
|
||||
classNames(css.input, {
|
||||
|
|
@ -48,9 +55,20 @@ class FieldTextInputComponent extends Component {
|
|||
[css.inputError]: hasError,
|
||||
[css.textarea]: isTextarea,
|
||||
});
|
||||
const maxLength = CONTENT_MAX_LENGTH;
|
||||
const inputProps = isTextarea
|
||||
? { className: inputClasses, id, rows: 1, maxLength: CONTENT_MAX_LENGTH, ...input, ...rest }
|
||||
: { className: inputClasses, id, type, ...input, ...rest };
|
||||
? { className: inputClasses, id, rows: 1, maxLength, ...refMaybe, ...input, ...rest }
|
||||
: isUncontrolled
|
||||
? {
|
||||
className: inputClasses,
|
||||
id,
|
||||
type,
|
||||
defaultValue,
|
||||
...refMaybe,
|
||||
...inputWithoutValue,
|
||||
...rest,
|
||||
}
|
||||
: { className: inputClasses, id, type, ...refMaybe, ...input, ...rest };
|
||||
|
||||
const classes = classNames(rootClassName || css.root, className);
|
||||
return (
|
||||
|
|
@ -71,6 +89,8 @@ FieldTextInputComponent.defaultProps = {
|
|||
customErrorText: null,
|
||||
id: null,
|
||||
label: null,
|
||||
isUncontrolled: false,
|
||||
inputRef: null,
|
||||
};
|
||||
|
||||
FieldTextInputComponent.propTypes = {
|
||||
|
|
@ -92,6 +112,12 @@ FieldTextInputComponent.propTypes = {
|
|||
// Either 'textarea' or something that is passed to the input element
|
||||
type: string.isRequired,
|
||||
|
||||
// Uncontrolled input uses defaultValue prop, but doesn't pass value from form to the field.
|
||||
// https://reactjs.org/docs/uncontrolled-components.html#default-values
|
||||
isUncontrolled: bool,
|
||||
// a ref object passed for input element.
|
||||
inputRef: object,
|
||||
|
||||
// Generated by final-form's Field component
|
||||
input: shape({
|
||||
onChange: func.isRequired,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue