Skip to content

Commit

Permalink
[Textarea] Simplification of the code (#12238)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari authored Jul 22, 2018
1 parent eb1ad7c commit c65d419
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
30 changes: 14 additions & 16 deletions packages/material-ui/src/Input/Textarea.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export const styles = {
background: 'transparent',
},
shadow: {
resize: 'none',
// Overflow also needed to here to remove the extra row
// added to textareas in Firefox.
overflow: 'hidden',
Expand All @@ -43,6 +42,8 @@ export const styles = {
* @ignore - internal component.
*/
class Textarea extends React.Component {
isControlled = this.props.value != null;

shadowRef = null;

singlelineShadowRef = null;
Expand Down Expand Up @@ -106,7 +107,7 @@ class Textarea extends React.Component {
handleChange = event => {
this.value = event.target.value;

if (typeof this.props.value === 'undefined' && this.shadowRef) {
if (!this.isControlled) {
// The component is not controlled, we need to update the shallow value.
this.shadowRef.value = this.value;
this.syncHeightWithShadow();
Expand All @@ -119,12 +120,9 @@ class Textarea extends React.Component {

syncHeightWithShadow() {
const props = this.props;
if (!this.shadowRef || !this.singlelineShadowRef) {
return;
}

// The component is controlled, we need to update the shallow value.
if (typeof props.value !== 'undefined') {
if (this.isControlled) {
// The component is controlled, we need to update the shallow value.
this.shadowRef.value = props.value == null ? '' : String(props.value);
}

Expand Down Expand Up @@ -169,22 +167,22 @@ class Textarea extends React.Component {
<div className={classes.root} style={{ height: this.state.height }}>
<EventListener target="window" onResize={this.handleResize} />
<textarea
aria-hidden="true"
className={classnames(classes.textarea, classes.shadow)}
readOnly
ref={this.handleRefSinglelineShadow}
className={classnames(classes.shadow, classes.textarea)}
tabIndex={-1}
rows="1"
readOnly
aria-hidden="true"
tabIndex={-1}
value=""
/>
<textarea
ref={this.handleRefShadow}
className={classnames(classes.shadow, classes.textarea)}
tabIndex={-1}
rows={rows}
aria-hidden="true"
readOnly
className={classnames(classes.textarea, classes.shadow)}
defaultValue={defaultValue}
readOnly
ref={this.handleRefShadow}
rows={rows}
tabIndex={-1}
value={value}
/>
<textarea
Expand Down
15 changes: 8 additions & 7 deletions packages/material-ui/src/Input/Textarea.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ function assignRefs(wrapper) {
};
}

const TextareaNaked = unwrap(Textarea);

describe('<Textarea />', () => {
let shallow;
let mount;

before(() => {
shallow = createShallow({ dive: true });
shallow = createShallow({ disableLifecycleMethods: true });
mount = createMount();
});

Expand All @@ -35,12 +37,12 @@ describe('<Textarea />', () => {
});

it('should render 3 textareas', () => {
const wrapper = shallow(<Textarea />);
const wrapper = shallow(<TextareaNaked classes={{}} />);
assert.strictEqual(wrapper.find('textarea').length, 3);
});

it('should change its height when the height of its shadows changes', () => {
const wrapper = shallow(<Textarea />);
const wrapper = shallow(<TextareaNaked classes={{}} />);
assert.strictEqual(wrapper.state().height, 19);

const refs = assignRefs(wrapper);
Expand All @@ -63,7 +65,6 @@ describe('<Textarea />', () => {
let wrapper;

beforeEach(() => {
const TextareaNaked = unwrap(Textarea);
wrapper = mount(<TextareaNaked classes={{}} value="f" />);
});

Expand Down Expand Up @@ -94,7 +95,7 @@ describe('<Textarea />', () => {
});

it('should set filled', () => {
const wrapper = shallow(<Textarea />);
const wrapper = shallow(<TextareaNaked classes={{}} />);
assert.strictEqual(wrapper.find('textarea').length, 3);

const refs = assignRefs(wrapper);
Expand All @@ -117,7 +118,7 @@ describe('<Textarea />', () => {
describe('prop: onChange', () => {
it('should be call the callback', () => {
const handleChange = spy();
const wrapper = shallow(<Textarea value="x" onChange={handleChange} />);
const wrapper = shallow(<TextareaNaked classes={{}} value="x" onChange={handleChange} />);
assert.strictEqual(wrapper.find('textarea').length, 3);

const refs = assignRefs(wrapper);
Expand All @@ -141,7 +142,7 @@ describe('<Textarea />', () => {
});

it('should handle the resize event', () => {
const wrapper = shallow(<Textarea />);
const wrapper = shallow(<TextareaNaked classes={{}} />);
const refs = assignRefs(wrapper);
refs.textareaShadowRef.scrollHeight = 43;
refs.singlelineShadowRef.scrollHeight = 43;
Expand Down

0 comments on commit c65d419

Please sign in to comment.