1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-18 07:52:23 +01:00

(quality) rename check-list to checklist and added an uncheck all button

This commit is contained in:
Sylvain 2022-07-27 15:35:57 +02:00 committed by Du Peng
parent 57ef555943
commit bd96622d37
6 changed files with 61 additions and 39 deletions

View File

@ -1,4 +1,4 @@
import React, { BaseSyntheticEvent } from 'react'; import React from 'react';
import { Controller, Path, FieldPathValue } from 'react-hook-form'; import { Controller, Path, FieldPathValue } from 'react-hook-form';
import { FieldValues } from 'react-hook-form/dist/types/fields'; import { FieldValues } from 'react-hook-form/dist/types/fields';
import { FieldPath } from 'react-hook-form/dist/types/path'; import { FieldPath } from 'react-hook-form/dist/types/path';
@ -13,16 +13,16 @@ import { FabButton } from '../base/fab-button';
*/ */
export type ChecklistOption<TOptionValue> = { value: TOptionValue, label: string }; export type ChecklistOption<TOptionValue> = { value: TOptionValue, label: string };
interface FormCheckListProps<TFieldValues, TOptionValue, TContext extends object> extends FormControlledComponent<TFieldValues, TContext>, AbstractFormItemProps<TFieldValues> { interface FormChecklistProps<TFieldValues, TOptionValue, TContext extends object> extends FormControlledComponent<TFieldValues, TContext>, AbstractFormItemProps<TFieldValues> {
defaultValue?: Array<TOptionValue>, defaultValue?: Array<TOptionValue>,
options: Array<ChecklistOption<TOptionValue>>, options: Array<ChecklistOption<TOptionValue>>,
onChange?: (values: Array<TOptionValue>) => void, onChange?: (values: Array<TOptionValue>) => void,
} }
/** /**
* This component is a template for an check list component to use within React Hook Form * This component is a template for a checklist component to use within React Hook Form
*/ */
export const FormCheckList = <TFieldValues extends FieldValues, TOptionValue, TContext extends object>({ id, control, label, tooltip, defaultValue, className, rules, disabled, error, warning, formState, onChange, options }: FormCheckListProps<TFieldValues, TOptionValue, TContext>) => { export const FormChecklist = <TFieldValues extends FieldValues, TOptionValue, TContext extends object>({ id, control, label, tooltip, defaultValue, className, rules, disabled, error, warning, formState, onChange, options }: FormChecklistProps<TFieldValues, TOptionValue, TContext>) => {
const { t } = useTranslation('shared'); const { t } = useTranslation('shared');
/** /**
@ -35,15 +35,15 @@ export const FormCheckList = <TFieldValues extends FieldValues, TOptionValue, TC
/** /**
* Callback triggered when a checkbox is ticked or unticked. * Callback triggered when a checkbox is ticked or unticked.
*/ */
const toggleCheckbox = (option: ChecklistOption<TOptionValue>, values: Array<TOptionValue> = [], cb: (value: Array<TOptionValue>) => void) => { const toggleCheckbox = (option: ChecklistOption<TOptionValue>, rhfValues: Array<TOptionValue> = [], rhfCallback: (value: Array<TOptionValue>) => void) => {
return (event: BaseSyntheticEvent) => { return (event: React.ChangeEvent<HTMLInputElement>) => {
let newValues: Array<TOptionValue> = []; let newValues: Array<TOptionValue> = [];
if (event.target.checked) { if (event.target.checked) {
newValues = values.concat(option.value); newValues = rhfValues.concat(option.value);
} else { } else {
newValues = values.filter(v => v !== option.value); newValues = rhfValues.filter(v => v !== option.value);
} }
cb(newValues); rhfCallback(newValues);
if (typeof onChange === 'function') { if (typeof onChange === 'function') {
onChange(newValues); onChange(newValues);
} }
@ -51,26 +51,33 @@ export const FormCheckList = <TFieldValues extends FieldValues, TOptionValue, TC
}; };
/** /**
* Callback triggered to select all options * Mark all options as selected
*/ */
const allSelect = (cb: (value: Array<TOptionValue>) => void) => { const selectAll = (rhfCallback: (value: Array<TOptionValue>) => void) => {
return () => { return () => {
const newValues: Array<TOptionValue> = options.map(o => o.value); const newValues: Array<TOptionValue> = options.map(o => o.value);
cb(newValues); rhfCallback(newValues);
if (typeof onChange === 'function') { if (typeof onChange === 'function') {
onChange(newValues); onChange(newValues);
} }
}; };
}; };
// Compose classnames from props /**
const classNames = [ * Mark all options as non-selected
`${className || ''}` */
].join(' '); const unselectAll = (rhfCallback: (value: Array<TOptionValue>) => void) => {
return () => {
rhfCallback([]);
if (typeof onChange === 'function') {
onChange([]);
}
};
};
return ( return (
<AbstractFormItem id={id} formState={formState} label={label} <AbstractFormItem id={id} formState={formState} label={label}
className={`form-check-list form-input ${classNames}`} tooltip={tooltip} className={`form-checklist form-input ${className || ''}`} tooltip={tooltip}
disabled={disabled} disabled={disabled}
rules={rules} error={error} warning={warning}> rules={rules} error={error} warning={warning}>
<Controller name={id as FieldPath<TFieldValues>} <Controller name={id as FieldPath<TFieldValues>}
@ -90,7 +97,10 @@ export const FormCheckList = <TFieldValues extends FieldValues, TOptionValue, TC
); );
})} })}
</div> </div>
<FabButton type="button" onClick={allSelect(onChange)} className="checklist-all-button">{t('app.shared.form_check_list.select_all')}</FabButton> <div className="actions">
<FabButton type="button" onClick={selectAll(onChange)} className="checklist-all-button">{t('app.shared.form_checklist.select_all')}</FabButton>
<FabButton type="button" onClick={unselectAll(onChange)} className="checklist-none-button">{t('app.shared.form_checklist.unselect_all')}</FabButton>
</div>
</> </>
); );
}} /> }} />

View File

@ -8,7 +8,7 @@ import { Product } from '../../models/product';
import { FormInput } from '../form/form-input'; import { FormInput } from '../form/form-input';
import { FormSwitch } from '../form/form-switch'; import { FormSwitch } from '../form/form-switch';
import { FormSelect } from '../form/form-select'; import { FormSelect } from '../form/form-select';
import { FormCheckList } from '../form/form-check-list'; import { FormChecklist } from '../form/form-checklist';
import { FormRichText } from '../form/form-rich-text'; import { FormRichText } from '../form/form-rich-text';
import { FabButton } from '../base/fab-button'; import { FabButton } from '../base/fab-button';
import { FabAlert } from '../base/fab-alert'; import { FabAlert } from '../base/fab-alert';
@ -177,7 +177,7 @@ export const ProductForm: React.FC<ProductFormProps> = ({ product, title, onSucc
<FabAlert level="warning"> <FabAlert level="warning">
<HtmlTranslate trKey="app.admin.store.product_form.assigning_machines_info" /> <HtmlTranslate trKey="app.admin.store.product_form.assigning_machines_info" />
</FabAlert> </FabAlert>
<FormCheckList options={machines} <FormChecklist options={machines}
control={control} control={control}
id="machine_ids" id="machine_ids"
formState={formState} /> formState={formState} />

View File

@ -38,7 +38,7 @@
@import "modules/form/form-input"; @import "modules/form/form-input";
@import "modules/form/form-rich-text"; @import "modules/form/form-rich-text";
@import "modules/form/form-switch"; @import "modules/form/form-switch";
@import "modules/form/form-check-list"; @import "modules/form/form-checklist";
@import "modules/group/change-group"; @import "modules/group/change-group";
@import "modules/machines/machine-card"; @import "modules/machines/machine-card";
@import "modules/machines/machines-filters"; @import "modules/machines/machines-filters";

View File

@ -1,17 +0,0 @@
.form-check-list {
position: relative;
.form-item-field {
display: block !important;
}
.checklist {
display: flex;
padding: 16px;
flex-wrap: wrap;
}
.checklist-item {
flex: 0 0 33.333333%;
}
}

View File

@ -0,0 +1,28 @@
.form-checklist {
position: relative;
.form-item-field {
display: block !important;
}
.checklist {
display: flex;
padding: 16px;
flex-wrap: wrap;
.checklist-item {
flex: 0 0 33.333333%;
& > input {
margin-right: 1em;
}
}
}
.actions {
display: flex;
justify-content: space-evenly;
width: 50%;
margin: auto auto 1.2em;
}
}

View File

@ -550,5 +550,6 @@ en:
validate_button: "Validate the new card" validate_button: "Validate the new card"
form_multi_select: form_multi_select:
create_label: "Add {VALUE}" create_label: "Add {VALUE}"
form_check_list: form_checklist:
select_all: "Select all" select_all: "Select all"
unselect_all: "Unselect all"