Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 10.7.0

- Added `prepare` method for processing form parameters after they
are serialized.

## 10.6.1

- Made a few performance tweaks to achieve deeper v8 optimization
Expand Down
31 changes: 30 additions & 1 deletion docs/api/form.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,41 @@ A string value to send to Presenters. If a Presenter is registered to
that string via its `register()` method, it will execute the
associated callback.

### serializer
### serializer(form)

The serialization function. By default this uses
[`form-serialize`](https://github.com/defunctzombie/form-serialize). On
submission, this function is given the associated form HTML element.

### prepare(params)

Executed after serialization to allow for extra parameter
manipulation. This is useful for ensuring proper date formats, or
other data formats that may come directly from a form input.

```javascript
class MyForm extends React.Component {
prepare (params) {
params.start = new Date(params.start).toISOString()
params.end = new Date(params.start).toISOString()

return params
}

render () {
return (
<Form intent={actions.create} prepare={this.prepare}>
<input name="name" />
<input name="start" type="datetime-local" />
<input name="end" type="datetime-local" />

<input type="submit" />
</Form>
)
}
}
```

### onSubmit(event, action)

An event callback executed immediately after the form submits and the
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "microcosm",
"version": "10.6.1",
"version": "10.7.0",
"private": true,
"description": "Flux with first-class actions and state sandboxing.",
"main": "src/microcosm.js",
Expand Down
4 changes: 3 additions & 1 deletion src/addons/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const Form = React.createClass({
propTypes: {
intent : PropTypes.oneOfType([ PropTypes.string, PropTypes.func]),
serializer : PropTypes.func,
prepare : PropTypes.func,
onSubmit : PropTypes.func,
onDone : PropTypes.func,
onUpdate : PropTypes.func,
Expand All @@ -23,6 +24,7 @@ const Form = React.createClass({
return {
intent : null,
serializer : form => serialize(form, { hash: true, empty: true }),
prepare : n => n,
onSubmit : () => {}
}
},
Expand All @@ -48,7 +50,7 @@ const Form = React.createClass({

submit(event) {
const form = this.refs.form
const params = this.props.serializer(form)
const params = this.props.prepare(this.props.serializer(form))
const action = this.context.send(this.props.intent, params)

if (action && action instanceof Action) {
Expand Down
25 changes: 24 additions & 1 deletion test/addons/form.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ test('does not execute onUpdate if not given an action', function () {
expect(onUpdate).not.toHaveBeenCalled()
})


test('submit can be called directly on the component instance', function () {
const onDone = jest.fn()

Expand All @@ -98,3 +97,27 @@ test('submit can be called directly on the component instance', function () {

expect(onDone).toHaveBeenCalledWith(true)
})

describe ('prepare', function() {

test('can prepare serialized data', function () {
const send = jest.fn()

const prepare = function (params) {
params.name = "BILLY"

return params
}

const form = mount((
<Form intent="test" prepare={prepare}>
<input name="name" defaultValue="Billy"/>
</Form>
), { context: { send } })

form.instance().submit()

expect(send).toHaveBeenCalledWith("test", { name: "BILLY" })
})

})