|
| 1 | +import React, {useMemo} from 'react'; |
| 2 | +import {ChecklistProps, PersistedProps, PersistenceTypes} from '../types'; |
| 3 | +import './css/checklist.css'; |
| 4 | + |
| 5 | +import {sanitizeOptions} from '../utils/optionTypes'; |
| 6 | +import LoadingElement from '../utils/_LoadingElement'; |
| 7 | +import {OptionsList} from '../utils/optionRendering'; |
| 8 | + |
| 9 | +/** |
| 10 | + * Checklist is a component that encapsulates several checkboxes. |
| 11 | + * The values and labels of the checklist are specified in the `options` |
| 12 | + * property and the checked items are specified with the `value` property. |
| 13 | + * Each checkbox is rendered as an input with a surrounding label. |
| 14 | + */ |
| 15 | +export default function Checklist({ |
| 16 | + className, |
| 17 | + id, |
| 18 | + style, |
| 19 | + setProps, |
| 20 | + inputStyle = {}, |
| 21 | + inputClassName = '', |
| 22 | + labelStyle = {}, |
| 23 | + labelClassName = '', |
| 24 | + options = [], |
| 25 | + value = [], |
| 26 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 27 | + persisted_props = [PersistedProps.value], |
| 28 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 29 | + persistence_type = PersistenceTypes.local, |
| 30 | + inline = false, |
| 31 | +}: ChecklistProps) { |
| 32 | + const sanitizedOptions = useMemo(() => { |
| 33 | + return sanitizeOptions(options); |
| 34 | + }, [options]); |
| 35 | + |
| 36 | + const stylingProps = { |
| 37 | + id, |
| 38 | + className: 'dash-checklist ' + (className ?? ''), |
| 39 | + style, |
| 40 | + optionClassName: inline ? 'dash-checklist-inline' : undefined, |
| 41 | + inputStyle, |
| 42 | + inputClassName, |
| 43 | + labelStyle, |
| 44 | + labelClassName, |
| 45 | + }; |
| 46 | + |
| 47 | + return ( |
| 48 | + <LoadingElement> |
| 49 | + {loadingProps => ( |
| 50 | + <OptionsList |
| 51 | + {...loadingProps} |
| 52 | + options={sanitizedOptions} |
| 53 | + selected={value ?? []} |
| 54 | + onSelectionChange={selection => { |
| 55 | + setProps({value: selection}); |
| 56 | + }} |
| 57 | + {...stylingProps} |
| 58 | + /> |
| 59 | + )} |
| 60 | + </LoadingElement> |
| 61 | + ); |
| 62 | +} |
| 63 | + |
| 64 | +Checklist.dashPersistence = { |
| 65 | + persisted_props: [PersistedProps.value], |
| 66 | + persistence_type: PersistenceTypes.local, |
| 67 | +}; |
0 commit comments