Skip to content

Commit

Permalink
Support passing callback passed from Field to the underlying componen…
Browse files Browse the repository at this point in the history
…ts (#68)

* Pass callbacks defined on the Field to the wrapped component

* Handle component callbacks

* Renamed callbacks by suffixing it with Func
  • Loading branch information
chaitanya-bhagavan authored and erikras committed Dec 1, 2016
1 parent b97b455 commit 43a8476
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 12 deletions.
19 changes: 17 additions & 2 deletions example/src/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ class Form extends Component {
<Field name="thinCrust" component={Toggle} label="Thin Crust" labelPosition="right"/>
</div>
<div>
<Field name="pepperoni" component={Checkbox} label="Pepperoni"/>
<Field name="pepperoni" component={Checkbox}
onCheck={value => {
console.log('onCheck ', value )
}}
label="Pepperoni"/>
</div>
<div>
<Field name="mushrooms" component={Checkbox} label="Mushrooms"/>
Expand All @@ -95,13 +99,19 @@ class Form extends Component {
component={DatePicker}
defaultValue={null} // DatePicker requires an object,
// and redux-form defaults to ''
onChange={(value) => {
console.log('date changed ', value)
}}
hintText="Day of delivery?"/>
</div>
<div>
<Field name="at"
component={TimePicker}
defaultValue={null} // TimePicker requires an object,
// and redux-form defaults to ''
onChange={(value) => {
console.log('time changed ', value)
}}
hintText="At what time?"/>
</div>
<div>
Expand All @@ -120,6 +130,9 @@ class Form extends Component {
floatingLabelText="Cheese"
openOnFocus={true}
filter={MUIAutoComplete.fuzzyFilter}
onNewRequest={value => {
console.log('AutoComplete ', value)
}}
dataSource={[ 'Cheddar', 'Mozzarella', 'Parmesan', 'Provolone' ]}
/>
</div>
Expand All @@ -136,7 +149,9 @@ export default reduxForm({
form: 'example',
initialValues: {
delivery: 'delivery',
name: 'Jane Doe'
name: 'Jane Doe',
at: new Date(),
cheese: 'Cheddar'
},
validate
})(Form)
11 changes: 8 additions & 3 deletions src/AutoComplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ import mapError from './mapError'

export default createComponent(
AutoComplete,
({ input: { onChange, value, ...inputProps }, ...props }) => ({
({ input: { value, ...inputProps }, onNewRequest: onNewRequestFunc, dataSourceConfig, ...props }) => ({
...mapError(props),
...inputProps,
searchText: value,
onNewRequest: value => onChange(value)
searchText: dataSourceConfig ? value[dataSourceConfig.text] : value,
onNewRequest: value => {
inputProps.onChange(value)
if(onNewRequestFunc && typeof onNewRequestFunc === 'function') {
onNewRequestFunc(value)
}
}
}))
8 changes: 7 additions & 1 deletion src/Checkbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@ export default createComponent(
...inputProps
},
meta, // eslint-disable-line no-unused-vars
onCheck: onCheckFunc,
...props
}) => ({
...inputProps,
...props,
checked: value ? true : false,
onCheck: onChange
onCheck: (event, isInputChecked) => {
onChange(isInputChecked)
if(onCheckFunc && typeof onCheckFunc === 'function') {
onCheckFunc(isInputChecked)
}
}
})
)
9 changes: 7 additions & 2 deletions src/DatePicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ export default createComponent(
({
input: {
onBlur, // eslint-disable-line no-unused-vars
onChange,
...inputProps
},
onChange: onChangeFunc,
...props
}) => ({
...inputProps,
...mapError(props),
onChange: (event, value) => onChange(value)
onChange: (event, value) => {
inputProps.onChange(value)
if(onChangeFunc && typeof onChangeFunc === 'function') {
onChangeFunc(value)
}
}
})
)
9 changes: 7 additions & 2 deletions src/Slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ import mapError from './mapError'

export default createComponent(
Slider,
({ input: { onDragStart, ...inputProps }, ...props }) => // eslint-disable-line no-unused-vars
({ input: { onDragStart, ...inputProps }, onChange: onChangeFunc, ...props }) => // eslint-disable-line no-unused-vars
({
...mapError({ ...props, input: inputProps }, 'error'),
onChange: (event, value) => inputProps.onChange(value)
onChange: (event, value) => {
inputProps.onChange(value)
if(onChangeFunc && typeof onChangeFunc === 'function') {
onChangeFunc(value)
}
}
})
)
9 changes: 7 additions & 2 deletions src/TimePicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ export default createComponent(
({
input: {
onBlur, // eslint-disable-line no-unused-vars
onChange,
...inputProps
},
onChange: onChangeFunc,
...props
}) => ({
...inputProps,
...mapError(props),
onChange: (event, value) => onChange(value)
onChange: (event, value) => {
inputProps.onChange(value)
if(onChangeFunc && typeof onChangeFunc === 'function') {
onChangeFunc(value)
}
}
})
)

0 comments on commit 43a8476

Please sign in to comment.