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

breaking(Form): remove onSubmit serializer #1367

Merged
merged 1 commit into from
Feb 22, 2017

Conversation

levithomason
Copy link
Member

@levithomason levithomason commented Feb 22, 2017

Breaking Changes!

Form onSubmit no longer includes a second parameter with the serialized form values. The serializer was accomplished by reading from the DOM, which is an anti-pattern in React. The onSubmit prop itself remains in-tact and functions identically to a vanilla React form's onSubmit prop.

Fixes #1362. See the related issues #1348 and #833 for more context.

Before

class OrderView extends React.Component {
  handleSubmit = (e, formData) => {
    e.preventDefault()
    console.log('Quantity:', formData.quantity)
  }

  render() {
    return (
      <Form onSubmit={this.handleSubmit}>
        <Form.Input name='quantity' defaultValue={this.state.quantity} />
      </Form>
    )
  }
}

After

You should hoist your data up the tree by either setting state, or using a state management solution such as Redux.

setState Example

class OrderView extends React.Component {
  state = {}

  handleChange = (e) => this.setState({ quantity: e.target.value })

  handleSubmit = (e) => {
    e.preventDefault()
    console.log('Quantity:', this.state.quantity)
  }

  render() {
    return (
      <Form onSubmit={this.handleSubmit}>
        <Form.Input name='quantity' value={this.state.quantity} onChange={this.handleChange} />
      </Form>
    )
  }
}

@codecov-io
Copy link

codecov-io commented Feb 22, 2017

Codecov Report

Merging #1367 into master will decrease coverage by -0.01%.
The diff coverage is n/a.

@@            Coverage Diff             @@
##           master    #1367      +/-   ##
==========================================
- Coverage   99.75%   99.74%   -0.01%     
==========================================
  Files         140      140              
  Lines        2400     2345      -55     
==========================================
- Hits         2394     2339      -55     
  Misses          6        6
Impacted Files Coverage Δ
src/collections/Form/Form.js 100% <ø> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 89a46e8...f11ac5a. Read the comment docs.

@levithomason levithomason merged commit 4dbaaf0 into master Feb 22, 2017
@levithomason levithomason deleted the fix/form-remove-serialzer-warnings branch February 22, 2017 01:15
@levithomason
Copy link
Member Author

Released in semantic-ui-react@0.66.0.

@qoalu
Copy link
Contributor

qoalu commented May 17, 2017

Here is a handleToggle for Checkbox Groups state tracking compatible with the new API. Checkboxes have an unmanaged state and just pass current value up.

JSX:

<Form.Checkbox key={key} label={text} name='groupName' value={value1} onChange={this.handleToggle} />
<Form.Checkbox key={key} label={text} name='groupName' value={value2} onChange={this.handleToggle} />

<Form.Checkbox key={key} label={text} name='groupName' value={value3} onChange={this.handleToggle} />

Action Function:

handleToggle = (e, { name, value, checked }) => {
	this.setState({ [name]: {
		...this.state[name],
		[value]: checked
	}
	});
};

Resulting State:

this.state = {
	groupName: {
		"value1": false,
		"value2": false,
		"value3": false
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants