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 '@mui/material';
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] : [];
45 <FormControl variant="standard" error={!isValid(props.input.value)}>
46 <FormLabel component='label'>{props.label}</FormLabel>
47 <FormGroup row={props.rowLayout}>
48 { props.items.map((item, idx) =>
53 data-cy={`checkbox-${item}`}
54 key={`control-${idx}`}
55 name={`${props.input.name}[${idx}]`}
58 props.input.value.indexOf(item) !== -1 ||
59 (props.input.value.length === 0 &&
60 (props.defaultValues || []).indexOf(item) !== -1)
63 const newValue = [...props.input.value];
64 if (e.target.checked) {
67 newValue.splice(newValue.indexOf(item), 1);
69 if (!isValid(newValue)) { return; }
70 return props.input.onChange(newValue);
72 disabled={props.meta.submitting}
77 <FormHelperText>{props.helperText}</FormHelperText>