Skip to content

Commit 025acbc

Browse files
committed
created adapter config parameter and used redux-form-material-ui lib in Mat UI example
1 parent 1d05fdd commit 025acbc

File tree

4 files changed

+46
-43
lines changed

4 files changed

+46
-43
lines changed

material-ui/.babelrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"presets": ["react", "es2015"]
2+
"presets": ["react", "es2015", "stage-2"]
33
}

material-ui/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"react-tap-event-plugin": "^1.0.0",
2222
"redux": "^3.3.1",
2323
"redux-form": "file:../../",
24+
"redux-form-material-ui": "^1.0.0",
2425
"redux-form-website-template": "0.0.29"
2526
},
2627
"devDependencies": {
@@ -29,6 +30,7 @@
2930
"babel-loader": "^6.2.0",
3031
"babel-preset-es2015": "^6.3.13",
3132
"babel-preset-react": "^6.3.13",
33+
"babel-preset-stage-2": "^6.1.18",
3234
"cross-env": "^1.0.7",
3335
"eslint": "^1.6.0",
3436
"eslint-config-rackt": "^1.1.1",

material-ui/src/MaterialUi.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ Read more [here](../../../docs/api/Field.md#usage).
1313
The delay between when you click "Submit" and when the alert dialog pops up is intentional,
1414
to simulate server latency.
1515

16-
### Field Adapters
16+
### Field Adapter
1717

18-
Notice that we define simple functions, like `renderTextField`, `renderCheckbox`, and
19-
`renderSelectField` to act as adapters between `redux-form` and the Material UI input
20-
components. You would only need to define these in one place in your application and reuse
21-
them in each form.
18+
In this example, we are importing the
19+
[`redux-form-material-ui`](https://github.com/erikras/redux-form-material-ui) adapter library,
20+
which knows how to map the strings passed to `component` to the input components from the
21+
Material UI library.
2222

2323
### How to use async validation in form:
2424

material-ui/src/MaterialUiForm.js

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import React from 'react'
22
import { Field, reduxForm } from 'redux-form'
3-
import TextField from 'material-ui/TextField'
4-
import { RadioButton, RadioButtonGroup } from 'material-ui/RadioButton'
5-
import Checkbox from 'material-ui/Checkbox'
6-
import SelectField from 'material-ui/SelectField'
3+
import { RadioButton } from 'material-ui/RadioButton'
74
import MenuItem from 'material-ui/MenuItem'
85
import asyncValidate from './asyncValidate'
6+
import adapter from 'redux-form-material-ui'
97

108
const validate = values => {
119
const errors = {}
@@ -21,71 +19,74 @@ const validate = values => {
2119
return errors
2220
}
2321

24-
const renderTextField = props => (
25-
<TextField hintText={props.label}
26-
floatingLabelText={props.label}
27-
errorText={props.touched && props.error}
28-
{...props}
29-
/>
30-
)
31-
32-
const renderCheckbox = props => (
33-
<Checkbox label={props.label}
34-
checked={props.value ? true : false}
35-
onCheck={props.onChange}/>
36-
)
37-
38-
const renderSelectField = props => (
39-
<SelectField
40-
floatingLabelText={props.label}
41-
errorText={props.touched && props.error}
42-
{...props}
43-
onChange={(event, index, value) => props.onChange(value)}>
44-
</SelectField>
45-
)
46-
4722
const MaterialUiForm = props => {
4823
const { handleSubmit, pristine, reset, submitting } = props
4924
return (
5025
<form onSubmit={handleSubmit}>
5126
<div>
52-
<Field name="firstName" component={renderTextField} label="First Name"/>
27+
<Field name="firstName"
28+
component="TextField"
29+
hintText="First Name"
30+
floatingLabelText="First Name"/>
5331
</div>
5432
<div>
55-
<Field name="lastName" component={renderTextField} label="Last Name"/>
33+
<Field
34+
name="lastName"
35+
component="TextField"
36+
hintText="Last Name"
37+
floatingLabelText="Last Name"/>
5638
</div>
5739
<div>
58-
<Field name="email" component={renderTextField} label="Email"/>
40+
<Field
41+
name="email"
42+
component="TextField"
43+
hintText="Email"
44+
floatingLabelText="Email"/>
5945
</div>
6046
<div>
61-
<Field name="sex" component={RadioButtonGroup}>
62-
<RadioButton value="male" label="male"/>
63-
<RadioButton value="female" label="female"/>
47+
<Field name="sex" component="RadioButtonGroup">
48+
<RadioButton value="male" label="Male"/>
49+
<RadioButton value="female" label="Female"/>
6450
</Field>
6551
</div>
6652
<div>
67-
<Field name="favoriteColor" component={renderSelectField} label="Favorite Color">
53+
<Field
54+
name="favoriteColor"
55+
component="SelectField"
56+
hintText="Favorite Color"
57+
floatingLabelText="Favorite Color">
6858
<MenuItem value={'ff0000'} primaryText="Red"/>
6959
<MenuItem value={'00ff00'} primaryText="Green"/>
7060
<MenuItem value={'0000ff'} primaryText="Blue"/>
7161
</Field>
7262
</div>
7363
<div>
74-
<Field name="employed" component={renderCheckbox} label="Employed"/>
64+
<Field name="employed" component="Checkbox" label="Employed"/>
65+
</div>
66+
<div>
67+
<Field name="married" component="Toggle" label="Married" labelPosition="right"/>
7568
</div>
7669
<div>
77-
<Field name="notes" component={renderTextField} label="Notes" multiLine={true} rows={2}/>
70+
<Field
71+
name="notes"
72+
component="TextField"
73+
hintText="Notes"
74+
floatingLabelText="Notes"
75+
multiLine={true}
76+
rows={2}/>
7877
</div>
7978
<div>
8079
<button type="submit" disabled={pristine || submitting}>Submit</button>
81-
<button type="button" disabled={pristine || submitting} onClick={reset}>Clear Values</button>
80+
<button type="button" disabled={pristine || submitting} onClick={reset}>Clear Values
81+
</button>
8282
</div>
8383
</form>
8484
)
8585
}
8686

8787
export default reduxForm({
8888
form: 'MaterialUiForm', // a unique identifier for this form
89+
adapter,
8990
validate,
9091
asyncValidate
9192
})(MaterialUiForm)

0 commit comments

Comments
 (0)