Skip to content

Commit

Permalink
Multiple checkboxes in SelectField (closes #33).
Browse files Browse the repository at this point in the history
  • Loading branch information
radekmie committed Jun 18, 2016
1 parent dae4685 commit 5b7fa60
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 74 deletions.
74 changes: 54 additions & 20 deletions packages/uniforms-bootstrap3/src/components/fields/SelectField.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,62 @@ import {connectField} from 'uniforms';

import FormGroup from './FormGroup';

const xor = (item, array) => {
let index = array.indexOf(item);
if (index === -1) {
return array.concat([item]);
}

return array.slice(0, index).concat(array.slice(index + 1));
};

const renderCheckboxes = props =>
props.allowedValues.map(item =>
<section key={item} className={classnames(props.inputClassName, `checkbox${props.inline ? '-inline' : ''}`)}>
<label htmlFor={`${props.id}-${item}`}>
<input
checked={props.fieldType === Array ? props.value.includes(item) : props.value === item}
disabled={props.disabled}
id={`${props.id}-${item}`}
name={props.name}
onChange={() => props.onChange(props.fieldType === Array ? xor(item, props.value) : item)}
type="checkbox"
/>
{props.transform ? props.transform(item) : item}
</label>
</section>
)
;

const renderSelect = props =>
<select
className={classnames(props.inputClassName, 'form-control', {'form-control-danger': props.error})}
disabled={props.disabled}
id={props.id}
name={props.name}
onChange={event => props.onChange(event.target.value)}
value={props.value}
>
{props.placeholder && (
<option value="" disabled hidden>
{props.placeholder}
</option>
)}

{props.allowedValues.map(value =>
<option key={value} value={value}>
{props.transform ? props.transform(value) : value}
</option>
)}
</select>
;

const Select = props =>
<FormGroup {...props}>
<select
className={classnames(props.inputClassName, 'c-select form-control', {'form-control-danger': props.error})}
disabled={props.disabled}
id={props.id}
name={props.name}
onChange={event => props.onChange(event.target.value)}
value={props.value}
>
{props.placeholder && (
<option value="" disabled hidden>
{props.placeholder}
</option>
)}

{props.allowedValues.map(value =>
<option key={value} value={value}>
{props.transform ? props.transform(value) : value}
</option>
)}
</select>
{props.checkboxes || props.fieldType === Array
? renderCheckboxes(props)
: renderSelect(props)
}
</FormGroup>
;

Expand Down
74 changes: 54 additions & 20 deletions packages/uniforms-bootstrap4/src/components/fields/SelectField.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,62 @@ import {connectField} from 'uniforms';

import FormGroup from './FormGroup';

const xor = (item, array) => {
let index = array.indexOf(item);
if (index === -1) {
return array.concat([item]);
}

return array.slice(0, index).concat(array.slice(index + 1));
};

const renderCheckboxes = props =>
props.allowedValues.map(item =>
<section key={item} className={classnames(props.inputClassName, `checkbox${props.inline ? '-inline' : ''}`)}>
<label htmlFor={`${props.id}-${item}`}>
<input
checked={props.fieldType === Array ? props.value.includes(item) : props.value === item}
disabled={props.disabled}
id={`${props.id}-${item}`}
name={props.name}
onChange={() => props.onChange(props.fieldType === Array ? xor(item, props.value) : item)}
type="checkbox"
/>
{props.transform ? props.transform(item) : item}
</label>
</section>
)
;

const renderSelect = props =>
<select
className={classnames(props.inputClassName, 'c-select form-control', {'form-control-danger': props.error})}
disabled={props.disabled}
id={props.id}
name={props.name}
onChange={event => props.onChange(event.target.value)}
value={props.value}
>
{props.placeholder && (
<option value="" disabled hidden>
{props.placeholder}
</option>
)}

{props.allowedValues.map(value =>
<option key={value} value={value}>
{props.transform ? props.transform(value) : value}
</option>
)}
</select>
;

const Select = props =>
<FormGroup {...props}>
<select
className={classnames(props.inputClassName, 'c-select form-control', {'form-control-danger': props.error})}
disabled={props.disabled}
id={props.id}
name={props.name}
onChange={event => props.onChange(event.target.value)}
value={props.value}
>
{props.placeholder && (
<option value="" disabled hidden>
{props.placeholder}
</option>
)}

{props.allowedValues.map(value =>
<option key={value} value={value}>
{props.transform ? props.transform(value) : value}
</option>
)}
</select>
{props.checkboxes || props.fieldType === Array
? renderCheckboxes(props)
: renderSelect(props)
}
</FormGroup>
;

Expand Down
81 changes: 60 additions & 21 deletions packages/uniforms-semantic/src/components/fields/SelectField.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,65 @@ import React from 'react';
import classnames from 'classnames';
import {connectField} from 'uniforms';

const xor = (item, array) => {
let index = array.indexOf(item);
if (index === -1) {
return array.concat([item]);
}

return array.slice(0, index).concat(array.slice(index + 1));
};

const renderCheckboxes = ({allowedValues, disabled, fieldType, id, name, onChange, transform, value}) =>
allowedValues.map(item =>
<section className="field" key={item}>
<section className="ui checkbox">
<input
checked={fieldType === Array ? value.includes(item) : value === item}
disabled={disabled}
id={`${id}-${item}`}
name={name}
onChange={() => onChange(fieldType === Array ? xor(item, value) : item)}
type="checkbox"
/>

<label htmlFor={`${id}-${item}`}>
{transform ? transform(item) : item}
</label>
</section>
</section>
)
;

const renderSelect = ({allowedValues, disabled, id, name, onChange, placeholder, transform, value}) =>
<select
disabled={disabled}
id={id}
name={name}
onChange={event => onChange(event.target.value)}
value={value}
>
{placeholder && (
<option value="" disabled hidden>
{placeholder}
</option>
)}

{allowedValues.map(value =>
<option key={value} value={value}>
{transform ? transform(value) : value}
</option>
)}
</select>
;

const Select = ({
allowedValues,
checkboxes,
className,
disabled,
error,
fieldType,
id,
label,
name,
Expand All @@ -16,33 +71,17 @@ const Select = ({
value,
...props
}) =>
<section className={classnames({disabled, error, required}, 'field')}>
<section className={classnames({disabled, error, required}, className, 'field')} {...props}>
{label && (
<label htmlFor={id}>
{label}
</label>
)}

<select
disabled={disabled}
id={id}
name={name}
onChange={event => onChange(event.target.value)}
value={value}
{...props}
>
{placeholder && (
<option value="" disabled hidden>
{placeholder}
</option>
)}

{allowedValues.map(value =>
<option key={value} value={value}>
{transform ? transform(value) : value}
</option>
)}
</select>
{checkboxes || fieldType === Array
? renderCheckboxes({allowedValues, disabled, id, name, onChange, transform, value, fieldType})
: renderSelect ({allowedValues, disabled, id, name, onChange, transform, value, placeholder})
}
</section>
;

Expand Down
69 changes: 56 additions & 13 deletions packages/uniforms-unstyled/src/components/fields/SelectField.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,61 @@
import React from 'react';
import {connectField} from 'uniforms';

const xor = (item, array) => {
let index = array.indexOf(item);
if (index === -1) {
return array.concat([item]);
}

return array.slice(0, index).concat(array.slice(index + 1));
};

const renderCheckboxes = ({allowedValues, disabled, fieldType, id, name, onChange, transform, value}) =>
allowedValues.map(item =>
<section key={item}>
<input
checked={fieldType === Array ? value.includes(item) : value === item}
disabled={disabled}
id={`${id}-${item}`}
name={name}
onChange={() => onChange(fieldType === Array ? xor(item, value) : item)}
type="checkbox"
/>

<label htmlFor={`${id}-${item}`}>
{transform ? transform(item) : item}
</label>
</section>
)
;

const renderSelect = ({allowedValues, disabled, id, name, onChange, placeholder, transform, value}) =>
<select
disabled={disabled}
id={id}
name={name}
onChange={event => onChange(event.target.value)}
value={value}
>
{placeholder && (
<option value="" disabled hidden>
{placeholder}
</option>
)}

{allowedValues.map(value =>
<option key={value} value={value}>
{transform ? transform(value) : value}
</option>
)}
</select>
;

const Select = ({
allowedValues,
checkboxes,
disabled,
fieldType,
id,
label,
name,
Expand All @@ -20,19 +72,10 @@ const Select = ({
</label>
)}

<select disabled={disabled} id={id} name={name} value={value} onChange={event => onChange(event.target.value)}>
{placeholder && (
<option value="" disabled hidden>
{placeholder}
</option>
)}

{allowedValues.map(value =>
<option key={value} value={value}>
{transform ? transform(value) : value}
</option>
)}
</select>
{checkboxes || fieldType === Array
? renderCheckboxes({allowedValues, disabled, id, name, onChange, transform, value, fieldType})
: renderSelect ({allowedValues, disabled, id, name, onChange, transform, value, placeholder})
}
</section>
;

Expand Down

0 comments on commit 5b7fa60

Please sign in to comment.