|
| 1 | +import {Accessor, createMemo, createSignal, For, JSX, Show} from 'solid-js'; |
| 2 | +import CheckboxFieldTsx from '../../../../checkboxFieldTsx'; |
| 3 | +import {IconTsx} from '../../../../iconTsx'; |
| 4 | +import {HeightTransition} from '../heightTransition'; |
| 5 | +import styles from './expandableFilterGroup.module.scss'; |
| 6 | + |
| 7 | + |
| 8 | +type Item = { |
| 9 | + checked: Accessor<boolean>; |
| 10 | + label: JSX.Element; |
| 11 | + onClick: () => void; |
| 12 | +}; |
| 13 | + |
| 14 | +type ExpandableFilterGroupProps = { |
| 15 | + mainLabel: JSX.Element; |
| 16 | + onMainCheckboxClick: () => void; |
| 17 | + checkedCount: number; |
| 18 | + items: Item[]; |
| 19 | +}; |
| 20 | + |
| 21 | +export const ExpandableFilterGroup = (props: ExpandableFilterGroupProps) => { |
| 22 | + const [isExpanded, setIsExpanded] = createSignal(false); |
| 23 | + |
| 24 | + const isMainChecked = createMemo(() => props.items.every(item => item.checked())); |
| 25 | + |
| 26 | + const onMainCheckboxClick = (e: MouseEvent) => { |
| 27 | + e.stopPropagation(); |
| 28 | + props.onMainCheckboxClick(); |
| 29 | + }; |
| 30 | + |
| 31 | + return ( |
| 32 | + <> |
| 33 | + <div class={`${styles.Row} hover-effect`} onClick={() => setIsExpanded(!isExpanded())}> |
| 34 | + <div class={styles.RowCheckboxWrapper} onClick={onMainCheckboxClick}> |
| 35 | + <CheckboxFieldTsx class={styles.RowCheckbox} checked={isMainChecked()} /> |
| 36 | + </div> |
| 37 | + <div class={styles.RowSeparator} /> |
| 38 | + <div class={styles.RowLabel}> |
| 39 | + {props.mainLabel} |
| 40 | + <div class={styles.Count}> |
| 41 | + {props.checkedCount}/{props.items.length} |
| 42 | + <IconTsx class={styles.CountArrow} classList={{[styles.toggled]: isExpanded()}} icon='arrowhead' /> |
| 43 | + </div> |
| 44 | + </div> |
| 45 | + </div> |
| 46 | + |
| 47 | + <HeightTransition> |
| 48 | + <Show when={isExpanded()}> |
| 49 | + <div class={styles.ExpandedItems}> |
| 50 | + <For each={props.items}> |
| 51 | + {item => ( |
| 52 | + <div class={`${styles.Row} hover-effect`} onClick={item.onClick}> |
| 53 | + <div class={styles.RowOffset} /> |
| 54 | + <div class={styles.RowCheckboxWrapper}> |
| 55 | + <CheckboxFieldTsx class={styles.RowCheckbox} checked={item.checked()} /> |
| 56 | + </div> |
| 57 | + <div class={styles.RowLabel}> |
| 58 | + {item.label} |
| 59 | + </div> |
| 60 | + </div> |
| 61 | + )} |
| 62 | + </For> |
| 63 | + </div> |
| 64 | + </Show> |
| 65 | + </HeightTransition> |
| 66 | + </> |
| 67 | + ) |
| 68 | +}; |
0 commit comments