gcanti / tcomb-form

Forms library for react
https://gcanti.github.io/tcomb-form
MIT License
1.16k stars 136 forks source link

Reset form not working for textarea. #308

Closed johnkchiu closed 8 years ago

johnkchiu commented 8 years ago

Version

Tell us which versions you are using:

Bind a button to "reset" form to default values. It works for other inputs EXCEPT textarea.

Actual behaviour

Other input types (e.g. textfield) resets, but the textarea input doesn't.

Steps to reproduce

import React from 'react';
import t from 'tcomb-form';

const Form = t.form.Form;

const formSchema = t.struct({
    field1: t.String,
    field2: t.String
});

const formOptions = {
    fields: {
        field1: { type: 'textarea' }
    }
};

export default class Crucible extends React.Component {
    constructor() {
        super();
        this.state = {
            value: {}
        };
        this.onChange = this.onChange.bind(this);
        this.reset = this.reset.bind(this);
    }

    onChange(value) {
        this.setState({ value });
    }

    reset(event) {
        event.preventDefault();
        this.setState({ value: null });
    }

    render() {
        return (
            <div>
                <form>
                    <Form
                      ref="form"
                      type={formSchema}
                      options={formOptions}
                      value={this.state.value}
                      onChange={this.onChange}
                    />
                    <div className="btn-toolbar">
                        <button className="btn btn-default" onClick={this.reset}>Reset</button>
                    </div>
                </form>
            </div>
        );
    }
}

Stack trace and console log

No error or message in console log.

gcanti commented 8 years ago

Relevant https://github.com/facebook/react/issues/2533

Based on what I read there:

reset() {
  this.setState({ value: { field1: '' } })
}

should work

johnkchiu commented 8 years ago

:+1: That worked. It seems that for textarea, you can't set the value to null. Empty spaces '' worked.