-
-
Notifications
You must be signed in to change notification settings - Fork 239
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
Allows material BoolFields to be checkboxes or toggles #189
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import Checkbox from 'material-ui/Checkbox'; | ||
import Toggle from 'material-ui/Toggle'; | ||
import React from 'react'; | ||
import connectField from 'uniforms/connectField'; | ||
import filterDOMProps from 'uniforms/filterDOMProps'; | ||
|
@@ -11,18 +12,28 @@ const Bool = ({ | |
name, | ||
onChange, | ||
value, | ||
appearance = 'checkbox', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Props also should be sorted. |
||
...props | ||
}) => | ||
<Checkbox | ||
checked={!!value} | ||
disabled={disabled} | ||
id={id} | ||
label={label} | ||
name={name} | ||
onCheck={(event, value) => disabled || onChange(value)} | ||
ref={inputRef} | ||
{...filterDOMProps(props)} | ||
/> | ||
; | ||
appearance === 'toggle' ? | ||
(<Toggle | ||
toggled={!!value} | ||
disabled={disabled} | ||
id={id} | ||
label={label} | ||
name={name} | ||
onToggle={(event, value) => disabled || onChange(value)} | ||
ref={inputRef}{...filterDOMProps(props)} | ||
/>) | ||
: | ||
(<Checkbox | ||
checked={!!value} | ||
disabled={disabled} | ||
id={id} | ||
label={label} | ||
name={name} | ||
onCheck={(event, value) => disabled || onChange(value)} | ||
ref={inputRef}{...filterDOMProps(props)} | ||
/>); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two things here:
appearance === 'toggle' ? (
<Toggle
...
/>
) : (
<Checkbox
...
/>
) |
||
|
||
export default connectField(Bool); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ import {spy} from 'sinon'; | |
import {stub} from 'sinon'; | ||
|
||
import MaterialCheckbox from 'material-ui/Checkbox'; | ||
import MaterialToggle from 'material-ui/Toggle'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep them sorted. |
||
import MaterialDatePicker from 'material-ui/DatePicker'; | ||
import MaterialRadio from 'material-ui/RadioButton'; | ||
import MaterialRadioGroup from 'material-ui/RadioButton/RadioButtonGroup'; | ||
|
@@ -57,7 +58,7 @@ describe('Everything', () => { | |
const transform = x => x; | ||
const checkboxes = true; | ||
const allowedValues = ['1', '2', '3']; | ||
const base = {label, required}; | ||
const base = {label, required}; | ||
|
||
const schema = { | ||
'x00': {...base, __type__: Number}, | ||
|
@@ -92,7 +93,8 @@ describe('Everything', () => { | |
'x31': {...base, __type__: String, allowedValues, checkboxes, component: SelectField}, | ||
'x32': {...base, __type__: String, component: HiddenField}, | ||
'x33': {...base, __type__: String, component: HiddenField, value: undefined}, | ||
'x34': {...base, __type__: Number, step: 4} | ||
'x34': {...base, __type__: Number, step: 4}, | ||
'x35': {...base, __type__: Boolean, appearance: 'toggle'} | ||
}; | ||
|
||
const bridgeName = name => name.replace(/\.\d+/g, '.$'); | ||
|
@@ -448,6 +450,19 @@ describe('Everything', () => { | |
expect(onSubmit.lastCall.calledWithMatch({x32: 'x32'})).to.be.ok; | ||
}); | ||
|
||
it('works (BoolField, isToggle)', async () => { | ||
const find = () => wrapper.find(MaterialToggle).filterWhere(x => x.props().name === 'x35'); | ||
|
||
expect(find().props()).to.have.property('toggled', false); | ||
expect(find().props().onToggle({}, true)).to.equal(undefined); | ||
expect(find().props()).to.have.property('toggled', true); | ||
|
||
await new Promise(resolve => setTimeout(resolve, 5)); | ||
|
||
expect(onChange.lastCall.calledWith('x35', true)).to.be.ok; | ||
expect(onSubmit.lastCall.calledWithMatch({x35: true})).to.be.ok; | ||
}); | ||
|
||
it('works (ListField, custom children)', async function _ () { | ||
this.timeout(30000); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep imports sorted.