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: set default defaultValue for TextInput and TextArea #3097

Closed
Show file tree
Hide file tree
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 @@ -71,8 +71,8 @@ exports[`ModalWrapper should render 1`] = `
className="bx--modal bx--modal-tall"
id="modal"
onBlur={[Function]}
onClick={[Function]}
onKeyDown={[Function]}
onMouseDown={[Function]}
role="presentation"
tabIndex={-1}
>
Expand Down
9 changes: 7 additions & 2 deletions packages/react/src/components/TextArea/TextArea.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import PropTypes from 'prop-types';
import React, { useState } from 'react';
import React, { useState, useEffect } from 'react';
import classNames from 'classnames';
import { settings } from 'carbon-components';
import WarningFilled16 from '@carbon/icons-react/lib/warning--filled/16';
Expand Down Expand Up @@ -50,7 +50,7 @@ const TextArea = ({
renderCharCounter: CharCounter = DefaultCharCounter,
...other
}) => {
const [textareaVal, setInput] = useState(defaultValue);
const [textareaVal, setInput] = useState(other.value || defaultValue);
const textareaProps = {
id,
onChange: evt => {
Expand Down Expand Up @@ -142,6 +142,10 @@ const TextArea = ({
/>
);

useEffect(() => {
setInput(other.value || defaultValue);
}, [other, defaultValue]);

return (
<div className={`${prefix}--form-item`}>
{label}
Expand Down Expand Up @@ -266,6 +270,7 @@ TextArea.defaultProps = {
invalidText: '',
helperText: '',
light: false,
defaultValue: '',
};

export default TextArea;
9 changes: 7 additions & 2 deletions packages/react/src/components/TextInput/TextInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import PropTypes from 'prop-types';
import React, { useState } from 'react';
import React, { useState, useEffect } from 'react';
import classNames from 'classnames';
import { settings } from 'carbon-components';
import WarningFilled16 from '@carbon/icons-react/lib/warning--filled/16';
Expand Down Expand Up @@ -56,7 +56,7 @@ const TextInput = React.forwardRef(function TextInput(
},
ref
) {
const [inputVal, setInput] = useState(defaultValue);
const [inputVal, setInput] = useState(other.value || defaultValue);
const errorId = id + '-error-msg';
const textInputClasses = classNames(`${prefix}--text-input`, className, {
[`${prefix}--text-input--light`]: light,
Expand Down Expand Up @@ -140,6 +140,10 @@ const TextInput = React.forwardRef(function TextInput(
return helperContent;
})();

useEffect(() => {
setInput(other.value || defaultValue);
}, [other, defaultValue]);

return (
<div className={`${prefix}--form-item ${prefix}--text-input-wrapper`}>
{label}
Expand Down Expand Up @@ -258,6 +262,7 @@ TextInput.defaultProps = {
invalidText: '',
helperText: '',
light: false,
defaultValue: '',
};

export default TextInput;