From ad956f33b83e55aadca7189d5352940aaeadd659 Mon Sep 17 00:00:00 2001 From: Lucas Di Pentima Date: Wed, 14 Jul 2021 17:55:15 -0300 Subject: [PATCH] 17573: Adds redux-form controlled multi-checkbox selection component. Arvados-DCO-1.1-Signed-off-by: Lucas Di Pentima --- .../checkbox-field/checkbox-field.tsx | 54 +++++++++++++++++-- 1 file changed, 51 insertions(+), 3 deletions(-) diff --git a/src/components/checkbox-field/checkbox-field.tsx b/src/components/checkbox-field/checkbox-field.tsx index 2b2a8a03..bfa3714a 100644 --- a/src/components/checkbox-field/checkbox-field.tsx +++ b/src/components/checkbox-field/checkbox-field.tsx @@ -4,7 +4,14 @@ import React from 'react'; import { WrappedFieldProps } from 'redux-form'; -import { FormControlLabel, Checkbox } from '@material-ui/core'; +import { + FormControlLabel, + Checkbox, + FormControl, + FormGroup, + FormLabel, + FormHelperText +} from '@material-ui/core'; export const CheckboxField = (props: WrappedFieldProps & { label?: string }) => disabled={props.meta.submitting} color="primary" /> } - label={props.label} - />; \ No newline at end of file + label={props.label} + />; + +type MultiCheckboxFieldProps = { + items: string[]; + label?: string; + minSelection?: number; + maxSelection?: number; + helperText?: string; + rowLayout?: boolean; +} + +export const MultiCheckboxField = (props: WrappedFieldProps & MultiCheckboxFieldProps) => { + const isValid = (items: string[]) => (items.length >= (props.minSelection || 0)) && + (items.length <= (props.maxSelection || items.length)); + return + {props.label} + + { props.items.map((item, idx) => + { + const newValue = [...props.input.value]; + if (e.target.checked) { + newValue.push(item); + } else { + newValue.splice(newValue.indexOf(item), 1); + } + if (!isValid(newValue)) { return; } + return props.input.onChange(newValue); + }} + disabled={props.meta.submitting} + color="primary" /> + } + label={item} />) } + + {props.helperText} + }; \ No newline at end of file -- 2.30.2