1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import React from 'react';
6 import { WrappedFieldProps } from 'redux-form';
14 } from '@material-ui/core';
16 export const CheckboxField = (props: WrappedFieldProps & { label?: string }) =>
20 checked={props.input.value}
21 onChange={props.input.onChange}
22 disabled={props.meta.submitting}
28 type MultiCheckboxFieldProps = {
30 defaultValues?: string[];
32 minSelection?: number;
33 maxSelection?: number;
38 export const MultiCheckboxField = (props: WrappedFieldProps & MultiCheckboxFieldProps) => {
39 const isValid = (items: string[]) => (items.length >= (props.minSelection || 0)) &&
40 (items.length <= (props.maxSelection || items.length));
41 if (props.input.value.length === 0 && (props.defaultValues || []).length !== 0) {
42 props.input.value = props.defaultValues ? [...props.defaultValues] : [];
44 return <FormControl error={!isValid(props.input.value)}>
45 <FormLabel component='label'>{props.label}</FormLabel>
46 <FormGroup row={props.rowLayout}>
47 { props.items.map((item, idx) =>
52 data-cy={`checkbox-${item}`}
53 key={`control-${idx}`}
54 name={`${props.input.name}[${idx}]`}
57 props.input.value.indexOf(item) !== -1 ||
58 (props.input.value.length === 0 &&
59 (props.defaultValues || []).indexOf(item) !== -1)
62 const newValue = [...props.input.value];
63 if (e.target.checked) {
66 newValue.splice(newValue.indexOf(item), 1);
68 if (!isValid(newValue)) { return; }
69 return props.input.onChange(newValue);
71 disabled={props.meta.submitting}
76 <FormHelperText>{props.helperText}</FormHelperText>