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

Allows material BoolFields to be checkboxes or toggles #189

Merged
merged 3 commits into from
Feb 10, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
33 changes: 22 additions & 11 deletions packages/uniforms-material/src/BoolField.js
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';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep imports sorted.

import React from 'react';
import connectField from 'uniforms/connectField';
import filterDOMProps from 'uniforms/filterDOMProps';
Expand All @@ -11,18 +12,28 @@ const Bool = ({
name,
onChange,
value,
appearance = 'checkbox',
Copy link
Contributor

Choose a reason for hiding this comment

The 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)}
/>);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two things here:

  • keep props sorted
  • ternary should be formatted like this:
appearance === 'toggle' ? (
    <Toggle
        ...
    />
) : (
    <Checkbox
        ...
    />
)


export default connectField(Bool);
19 changes: 17 additions & 2 deletions packages/uniforms-material/test/combined.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {spy} from 'sinon';
import {stub} from 'sinon';

import MaterialCheckbox from 'material-ui/Checkbox';
import MaterialToggle from 'material-ui/Toggle';
Copy link
Contributor

Choose a reason for hiding this comment

The 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';
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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, '.$');
Expand Down Expand Up @@ -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);

Expand Down