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(Form): call preventDefault in onSubmit only when it exists #1788

Merged
merged 2 commits into from
Jun 23, 2017

Conversation

layershifter
Copy link
Member

@layershifter layershifter commented Jun 23, 2017

Fixes #1790.

In #1786 we implimented a small, but cool enhancement, but as our user reports it's not fully compatible with third-party libraries. This PR fixes this.

@codecov-io
Copy link

codecov-io commented Jun 23, 2017

Codecov Report

Merging #1788 into master will decrease coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1788      +/-   ##
==========================================
- Coverage   99.75%   99.75%   -0.01%     
==========================================
  Files         144      144              
  Lines        2466     2465       -1     
==========================================
- Hits         2460     2459       -1     
  Misses          6        6
Impacted Files Coverage Δ
src/collections/Form/Form.js 100% <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 ea964eb...1b1d7c1. Read the comment docs.

if (onSubmit) onSubmit(e, { ...this.props })
const { action } = this.props

if (!action) _.invoke(e, 'preventDefault')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about a line comment linking to the issue for this? Looking at this later, I could see it getting reverted as it appears unnecessary.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added an issue that describes why this fix is needed. (#1790)

if (onSubmit) onSubmit(e, { ...this.props })
handleSubmit = (...args) => {
const { action } = this.props
const [e] = args
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@levithomason I've updated this part, too. Main idea to pass all args from third-party lib correctly.

Let me know what you think.

Copy link
Member

@levithomason levithomason Jun 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I see the issue. I don't like that this makes the signature awkward as the position of the props argument can never be known for certain anymore. It depends on how many arguments the underlying component passed back. I'd like to keep it (e, data) for consistency with all our signatures.

That said, perhaps we should extend out signatures to pass any other args after these. So:

handleSubmit = (e, ...args) => {
  const { action } = this.props

  if (!action) _.invoke(e, 'preventDefault')
  _.invoke(this.props, 'onSubmit', e, this.props, ...args)
}

This way our signature is (e, data, ...args). Thoughts?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sence, it also will be more cleaner 👍

.simulate('submit', event)
preventDefault.should.not.have.been.called()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand how this is working, event is now undefined.

.simulate('submit', event)

Shouldn't this be throwing?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heads up, window.event is a nonstandard proprietary IE global. This test is not throwing because it is passing the undefined window.event to simulate().

However, this test is also not testing anything since the event passed to simulate does not include the preventDefault spy under test. I'll update these to assert on the actual event object again.

event.preventDefault.should.have.been.calledOnce()
shallow(<Form onSubmit={_.noop} />)
.simulate('submit', { preventDefault })
preventDefault.should.have.been.calledOnce()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is OK for this PR but I'd prefer we don't make arbitrary refactors such as this. It helps keeps the PRs smaller.

An exception would be refactors for consistency in coding standards, however, we use const event = {} quite a bit in tests.

@levithomason
Copy link
Member

Rebased and updated. Will merge on pass.

@levithomason levithomason merged commit 1b6589e into master Jun 23, 2017
@levithomason levithomason deleted the fix/form-e-error branch June 23, 2017 20:07
@levithomason
Copy link
Member

Released in semantic-ui-react@0.70.0

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

Successfully merging this pull request may close these issues.

Form: using a third-party form library causes "Uncaught TypeError: e.preventDefault is not a function"
4 participants