Skip to content

Commit

Permalink
fix: pass defaultValue in to textarea and text input initial state (c…
Browse files Browse the repository at this point in the history
  • Loading branch information
emyarod committed Jun 18, 2019
1 parent 597ab82 commit a0104a8
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 29 deletions.
4 changes: 2 additions & 2 deletions packages/react/src/components/TextArea/TextArea-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('TextArea', () => {
labelText="testlabel"
className="extra-class"
helperText="testHelper"
defaultValue="default value"
/>
);

Expand Down Expand Up @@ -67,8 +68,7 @@ describe('TextArea', () => {
});

it('should set defaultValue as expected', () => {
wrapper.setProps({ defaultValue: 'default value' });
expect(textarea().props().defaultValue).toEqual('default value');
expect(textarea().props().value).toEqual('default value');
});

it('should count length increases in textarea value', () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/react/src/components/TextArea/TextArea.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ const TextArea = ({
light,
charCount,
maxLength,
defaultValue,
renderCharCounter: CharCounter = DefaultCharCounter,
...other
}) => {
const [textareaVal, setInput] = useState('');
const [textareaVal, setInput] = useState(defaultValue);
const textareaProps = {
id,
onChange: evt => {
Expand Down
35 changes: 13 additions & 22 deletions packages/react/src/components/TextInput/TextInput-story.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import React from 'react';
import React, { useState } from 'react';
import { storiesOf } from '@storybook/react';
import { action } from '@storybook/addon-actions';
import {
Expand Down Expand Up @@ -73,27 +73,18 @@ const props = {
}),
};

class ControlledPasswordInputApp extends React.Component {
state = {
type: 'password',
};

togglePasswordVisibility = () => {
this.setState({
type: this.state.type === 'password' ? 'text' : 'password',
});
};

render() {
return (
<TextInput.ControlledPasswordInput
type={this.state.type}
togglePasswordVisibility={this.togglePasswordVisibility}
{...props.passwordInput()}
/>
);
}
}
const ControlledPasswordInputApp = () => {
const [inputType, setInputType] = useState('password');
const togglePasswordVisibility = () =>
setInputType(inputType === 'password' ? 'text' : 'password');
return (
<TextInput.ControlledPasswordInput
type={inputType}
togglePasswordVisibility={togglePasswordVisibility}
{...props.passwordInput()}
/>
);
};

storiesOf('TextInput', module)
.addDecorator(withKnobs)
Expand Down
5 changes: 2 additions & 3 deletions packages/react/src/components/TextInput/TextInput-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('TextInput', () => {
className="extra-class"
labelText="testlabel"
helperText="testHelper"
defaultValue="test"
light
/>
);
Expand Down Expand Up @@ -75,9 +76,7 @@ describe('TextInput', () => {
});

it('should set value as expected', () => {
expect(textInput().props().defaultValue).toEqual(undefined);
wrapper.setProps({ defaultValue: 'test' });
expect(textInput().props().defaultValue).toEqual('test');
expect(textInput().props().value).toEqual('test');
});

it('should count length increases in text input value', () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/react/src/components/TextInput/TextInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ const TextInput = React.forwardRef(function TextInput(
light,
charCount,
renderCharCounter: CharCounter = DefaultCharCounter,
defaultValue,
maxLength,
...other
},
ref
) {
const [inputVal, setInput] = useState('');
const [inputVal, setInput] = useState(defaultValue);
const errorId = id + '-error-msg';
const textInputClasses = classNames(`${prefix}--text-input`, className, {
[`${prefix}--text-input--light`]: light,
Expand Down

0 comments on commit a0104a8

Please sign in to comment.