|
| 1 | +import { type JSXNode, Component, PropsOf } from '@builder.io/qwik'; |
| 2 | +import { CheckboxRoot, type MixedStateCheckboxProps } from './checkbox'; |
| 3 | +import { |
| 4 | + ChecklistContextWrapper, |
| 5 | + getTriBool, |
| 6 | +} from '../checklist/checklist-context-wrapper'; |
| 7 | + |
| 8 | +type CheckListProps = PropsOf<'ul'> & { ariaLabeledBy: string }; |
| 9 | +// type CheckBoxes= |
| 10 | +/* |
| 11 | + This is an inline component. An example use case of an inline component to get the proper indexes with CSR. See issue #4757 |
| 12 | + for more information. |
| 13 | +*/ |
| 14 | +export const Checklist: Component<CheckListProps> = (props: CheckListProps) => { |
| 15 | + const checkArr: JSXNode[] = []; |
| 16 | + const hellSigs = []; |
| 17 | + let checklistCheckbox = undefined; |
| 18 | + const boolArr: boolean[] = []; |
| 19 | + const idArr: Array<false | string> = []; |
| 20 | + const checklistChilds: JSXNode[] = []; |
| 21 | + const { children: myChildren } = props; |
| 22 | + |
| 23 | + const childrenToProcess = ( |
| 24 | + Array.isArray(myChildren) ? [...myChildren] : [myChildren] |
| 25 | + ) as Array<JSXNode>; |
| 26 | + |
| 27 | + while (childrenToProcess.length) { |
| 28 | + const child = childrenToProcess.shift(); |
| 29 | + |
| 30 | + if (!child) { |
| 31 | + continue; |
| 32 | + } |
| 33 | + |
| 34 | + if (Array.isArray(child)) { |
| 35 | + childrenToProcess.unshift(...child); |
| 36 | + continue; |
| 37 | + } |
| 38 | + |
| 39 | + switch (child.type) { |
| 40 | + case CheckboxRoot: { |
| 41 | + const typedProps = child.props as MixedStateCheckboxProps; |
| 42 | + // FYI: Obj.assign mutates |
| 43 | + Object.assign(typedProps, { _useCheckListContext: true }); |
| 44 | + checkArr.push(child); |
| 45 | + // TODO: fix this if hell by making fn |
| 46 | + if (!typedProps.checkList) { |
| 47 | + checklistChilds.push(child); |
| 48 | + |
| 49 | + if (typedProps.id != undefined) { |
| 50 | + idArr.push(typedProps.id as string); |
| 51 | + } else { |
| 52 | + idArr.push(false); |
| 53 | + } |
| 54 | + if (typedProps.checkBoxSig && typedProps.checkBoxSig.value) { |
| 55 | + boolArr.push(typedProps.checkBoxSig.value); |
| 56 | + hellSigs.push(typedProps.checkBoxSig); |
| 57 | + } else { |
| 58 | + boolArr.push(false); |
| 59 | + } |
| 60 | + } else { |
| 61 | + checklistCheckbox = child; |
| 62 | + } |
| 63 | + |
| 64 | + break; |
| 65 | + } |
| 66 | + |
| 67 | + default: { |
| 68 | + if (child) { |
| 69 | + const anyChildren = Array.isArray(child.children) |
| 70 | + ? [...child.children] |
| 71 | + : [child.children]; |
| 72 | + childrenToProcess.unshift(...(anyChildren as JSXNode[])); |
| 73 | + } |
| 74 | + |
| 75 | + break; |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + if (checklistCheckbox === undefined) { |
| 80 | + throw Error( |
| 81 | + "QWIKUI: checklist doesn't have a checkbox. Did you give the atribute to *checklist* to any of the checkboxes inside the checklist?", |
| 82 | + ); |
| 83 | + } |
| 84 | + if (checklistCheckbox.props.checkBoxSig) { |
| 85 | + checklistChilds.forEach((checkbox) => { |
| 86 | + Object.assign(checkbox.props, { _overWriteCheckbox: true }); |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + return ( |
| 91 | + <> |
| 92 | + <ChecklistContextWrapper |
| 93 | + ariaLabeledBy={props.ariaLabeledBy} |
| 94 | + arrSize={boolArr.length} |
| 95 | + initialTriBool={getTriBool(boolArr)} |
| 96 | + idArr={idArr} |
| 97 | + > |
| 98 | + <ul class={props.class}> |
| 99 | + {checkArr.map((checkbox, i) => ( |
| 100 | + <li key={i}>{checkbox}</li> |
| 101 | + ))} |
| 102 | + </ul> |
| 103 | + </ChecklistContextWrapper> |
| 104 | + </> |
| 105 | + ); |
| 106 | +}; |
| 107 | +// TODO: deprecate ariaLabelledBy |
0 commit comments