vigetlabs / microcosm

Flux with actions at center stage. Write optimistic updates, cancel requests, and track changes with ease.
http://code.viget.com/microcosm/
MIT License
487 stars 29 forks source link

Add value prop to ActionForm #486

Closed nhunzaker closed 6 years ago

nhunzaker commented 6 years ago

This commit adds a value prop to the ActionForm component of microcosm-dom. When set, the value prop is always passed as the serialized value of the form. This is intended to better support controlled forms.

This also achieves a nice parity with ActionButton, which lets you set a similar value property as the callback value when the button is clicked.

import { login } from '../actions/session'

class Login extends React.Component {
  state = {
    email: '',
    password: ''
  }

  render() {
    let { email, password } = this.state

    return (
      <ActionForm action={login} value={this.state}>
        <input type="email" name="email" value={email} onChange={this.setField} />
        <input type="password" name="password" value={password} onChange={this.setField} />
      </ActionForm>
    )
  }

  setField = event => this.setState({ [event.target.name]: [event.target.value] })
}

Fixes #429

codecov-io commented 6 years ago

Codecov Report

Merging #486 into master will increase coverage by 0.55%. The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #486      +/-   ##
==========================================
+ Coverage   97.99%   98.54%   +0.55%     
==========================================
  Files          29       29              
  Lines         698      687      -11     
  Branches      135      135              
==========================================
- Hits          684      677       -7     
+ Misses         12        8       -4     
  Partials        2        2
Impacted Files Coverage Δ
packages/microcosm-dom/src/action-form.js 100% <100%> (ø) :arrow_up:
packages/microcosm/src/domain.js 97.91% <0%> (-0.05%) :arrow_down:
packages/microcosm/src/subject.js 100% <0%> (ø) :arrow_up:
packages/microcosm/src/microcosm.js 100% <0%> (ø) :arrow_up:
packages/microcosm/src/observable.js 100% <0%> (+2.27%) :arrow_up:
packages/microcosm/src/ledger.js 100% <0%> (+11.11%) :arrow_up:

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 0c66124...f79383e. Read the comment docs.

nhunzaker commented 6 years ago

Ack yes. Good call!

dce commented 6 years ago

When you say

When set, the value prop is always passed as the serialized value of the form.

Do you mean "the value prop is always passed intead of the serialized value of the form"?

nhunzaker commented 6 years ago

@dce yes. That is what I meant to say.

nhunzaker commented 6 years ago

@cwmanning Documentation added!