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
18 changes: 8 additions & 10 deletions src/Field.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,16 @@ export default class Field extends React.PureComponent<Props, State> {
value = ''
}
const input = { name, value, ...this.handlers }
if (component === 'input') {
if (rest.type === 'checkbox') {
if (_value === undefined) {
input.checked = !!value
} else {
input.checked = Array.isArray(value) && ~value.indexOf(_value)
input.value = _value
}
} else if (rest.type === 'radio') {
input.checked = value === _value
if (rest.type === 'checkbox') {
if (_value === undefined) {
input.checked = !!value
} else {
input.checked = Array.isArray(value) && ~value.indexOf(_value)
input.value = _value
}
} else if (rest.type === 'radio') {
input.checked = value === _value
input.value = _value
}
if (component === 'select' && rest.multiple) {
input.value = input.value || []
Expand Down
34 changes: 34 additions & 0 deletions src/Field.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,40 @@ describe('Field', () => {
expect(bazInput.checked).toBe(true)
})

it('should render custom radio component with checked prop', () => {
const Radio = jest.fn(() => null)
TestUtils.renderIntoDocument(
<Form
onSubmit={onSubmitMock}
render={() => (
<form>
<Field name="foo" type="radio" value="yes" component={Radio} />
</form>
)}
initialValues={{ foo: 'yes' }}
/>
)
expect(Radio.mock.calls[0][0]).toMatchObject({ input: { checked: true } })
})

it('should render custom checkbox component with checked prop', () => {
const Checkbox = jest.fn(() => null)
TestUtils.renderIntoDocument(
<Form
onSubmit={onSubmitMock}
render={() => (
<form>
<Field name="foo" type="checkbox" component={Checkbox} />
</form>
)}
initialValues={{ foo: true }}
/>
)
expect(Checkbox.mock.calls[0][0]).toMatchObject({
input: { checked: true }
})
})

it('should use isEqual to calculate dirty/pristine', () => {
const input = jest.fn(({ input }) => <input {...input} />)

Expand Down