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

Support onChange passed from React Redux Field component to Material UI Select Field #50

Merged
merged 1 commit into from
Nov 18, 2016
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
9 changes: 7 additions & 2 deletions src/SelectField.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ import mapError from './mapError'

export default createComponent(
SelectField,
({ input: { onChange, ...inputProps }, ...props }) => ({
({ input: { onChange, ...inputProps }, onChange:onChangeFromField, ...props }) => ({
...mapError(props),
...inputProps,
onChange: (event, index, value) => onChange(value)
onChange: (event, index, value) => {
onChange(value)
if(onChangeFromField) {
onChangeFromField(value)
}
}
}))
25 changes: 25 additions & 0 deletions src/__tests__/SelectField.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,31 @@ describe('SelectField', () => {
.toHaveBeenCalledWith('TheValue')
})

it('maps onChange from Field property properly', () => {
const reduxFormOnChange = createSpy()
const fieldOnChange = createSpy()

const dom = TestUtils.renderIntoDocument(
<MuiThemeProvider muiTheme={getMuiTheme()}>
<ReduxFormMaterialUISelectField name="mySelect" input={{ onChange:reduxFormOnChange, value: 'Foo' }} onChange={fieldOnChange}/>
</MuiThemeProvider>
)

const select = TestUtils.findRenderedComponentWithType(dom, SelectField)

expect(reduxFormOnChange).toNotHaveBeenCalled()
expect(fieldOnChange).toNotHaveBeenCalled()

select.props.onChange(undefined, 42, 'TheValue')

expect(reduxFormOnChange)
.toHaveBeenCalled()
.toHaveBeenCalledWith('TheValue')
expect(fieldOnChange)
.toHaveBeenCalled()
.toHaveBeenCalledWith('TheValue')
})

it('provides getRenderedComponent', () => {
const dom = TestUtils.renderIntoDocument(
<MuiThemeProvider muiTheme={getMuiTheme()}>
Expand Down