|
1 | | -import { useRef } from 'react'; |
2 | | -import { useOption, mergeProps, useFocusRing } from 'react-aria'; |
3 | | -import Checkbox from '../checkbox/Checkbox'; |
| 1 | +import { useListBox } from "react-aria"; |
| 2 | +import EnhancedOption from "./EnhancedOption"; |
4 | 3 |
|
5 | | -const EnhancedOption = ({ |
6 | | - item, |
7 | | - state, |
8 | | - isVisibilityEnabled, |
9 | | - name, |
10 | | - visibility = {}, |
11 | | - onToggleVisibility, |
12 | | - isPending, |
13 | | - onSelectionChange, |
14 | | -}) => { |
15 | | - const ref = useRef(null); |
| 4 | +const EnhancedListBox = ({ listBoxRef, state, name, pendingKey, pendingKeys, onSelectionChange, ...props }) => { |
16 | 5 |
|
17 | | - const { |
18 | | - optionProps, |
19 | | - isSelected, |
20 | | - isFocused, |
21 | | - isDisabled, |
22 | | - } = useOption({ key: item.key }, state, ref); |
| 6 | + const { listBoxProps } = useListBox(props, state, listBoxRef); |
23 | 7 |
|
24 | | - const { isFocusVisible, focusProps } = useFocusRing(); |
25 | | - |
26 | | - const isVisible = visibility ? visibility[item.key] !== false : true; |
27 | | - const isSingle = state.selectionManager.selectionMode === 'single'; |
28 | | - |
29 | | - const handleEyeClick = (e) => { |
30 | | - e.preventDefault(); |
31 | | - onToggleVisibility?.(item.key); |
32 | | - }; |
33 | | - |
34 | | - const preventSelection = (e) => { |
35 | | - e.stopPropagation(); |
36 | | - }; |
37 | | - |
38 | | - const isMarked = isSingle ? (isSelected || isPending) : isPending; |
39 | | - |
40 | | - let classes = 'tf-enhanced-choice-option'; |
41 | | - if (isMarked) classes += ' is-selected'; |
42 | | - if (isFocused) classes += ' is-focused'; |
43 | | - if (isDisabled) classes += ' is-disabled'; |
44 | | - |
45 | | - const handleMultipleClick = (e) => { |
46 | | - e.preventDefault(); |
47 | | - if (!isDisabled) onSelectionChange?.(item.key); |
48 | | - }; |
49 | | - |
50 | | - const liProps = isSingle |
51 | | - ? mergeProps(optionProps, focusProps) |
52 | | - : mergeProps(optionProps, focusProps, { |
53 | | - onClick: handleMultipleClick, |
54 | | - onMouseDown: (e) => e.preventDefault(), |
55 | | - }); |
| 8 | + const isPendingKey = (key) => |
| 9 | + pendingKeys ? pendingKeys.includes(key) : pendingKey === key; |
56 | 10 |
|
57 | 11 | return ( |
58 | | - <li |
59 | | - {...liProps} |
60 | | - ref={ref} |
61 | | - className={classes} |
62 | | - data-focus-visible={isFocusVisible} |
| 12 | + <ul |
| 13 | + {...listBoxProps} |
| 14 | + ref={listBoxRef} |
| 15 | + className="tf-enhanced-choice-list" |
63 | 16 | > |
64 | | - <div className="tf-enhanced-choice-option-content"> |
65 | | - <div |
66 | | - className="tf-enhanced-choice-selection-indicator" |
67 | | - style={{ pointerEvents: 'none' }} |
68 | | - > |
69 | | - {isSingle ? ( |
70 | | - <span |
71 | | - aria-hidden="true" |
72 | | - className={`tf-enhanced-choice-radio${isMarked ? ' is-checked' : ''}`} |
73 | | - /> |
74 | | - ) : ( |
75 | | - <Checkbox |
76 | | - isSelected={isMarked} |
77 | | - isDisabled={isDisabled} |
78 | | - /> |
79 | | - )} |
80 | | - </div> |
81 | | - |
82 | | - <div className="tf-enhanced-choice-label"> |
83 | | - {item.rendered} |
84 | | - </div> |
85 | | - |
86 | | - {isVisibilityEnabled && ( |
87 | | - <button |
88 | | - type="button" |
89 | | - className="tf-enhanced-choice-visibility-toggle" |
90 | | - onClick={handleEyeClick} |
91 | | - onPointerDown={preventSelection} |
92 | | - onMouseDown={preventSelection} |
93 | | - aria-label={isVisible ? 'Hide item' : 'Show item'} |
94 | | - > |
95 | | - {isVisible ? ( |
96 | | - <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"> |
97 | | - <path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"></path> |
98 | | - <circle cx="12" cy="12" r="3"></circle> |
99 | | - </svg> |
100 | | - ) : ( |
101 | | - <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"> |
102 | | - <path d="M17.94 17.94A10.07 10.07 0 0 1 12 20c-7 0-11-8-11-8a18.45 18.45 0 0 1 5.06-5.94M9.9 4.24A9.12 9.12 0 0 1 12 4c7 0 11 8 11 8a18.5 18.5 0 0 1-2.16 3.19m-6.72-1.07a3 3 0 1 1-4.24-4.24"></path> |
103 | | - <line x1="1" y1="1" x2="23" y2="23"></line> |
104 | | - </svg> |
105 | | - )} |
106 | | - </button> |
107 | | - )} |
108 | | - </div> |
109 | | - </li> |
| 17 | + {[...state.collection].map((item) => |
| 18 | + item.type === "section" ? ( |
| 19 | + <li key={item.key}> |
| 20 | + <span>{item.rendered}</span> |
| 21 | + <ul style={{ listStyle: "none", padding: 0, margin: 0 }}> |
| 22 | + {[...item.childNodes].map((child) => ( |
| 23 | + <EnhancedOption |
| 24 | + key={child.key} |
| 25 | + item={child} |
| 26 | + state={state} |
| 27 | + name={name} |
| 28 | + isPending={isPendingKey(child.key)} |
| 29 | + onSelectionChange={onSelectionChange} |
| 30 | + isViewable={props.isViewable} |
| 31 | + /> |
| 32 | + ))} |
| 33 | + </ul> |
| 34 | + </li> |
| 35 | + ) : ( |
| 36 | + <EnhancedOption |
| 37 | + key={item.key} |
| 38 | + item={item} |
| 39 | + state={state} |
| 40 | + name={name} |
| 41 | + isPending={isPendingKey(item.key)} |
| 42 | + onSelectionChange={onSelectionChange} |
| 43 | + isViewable={props.isViewable} |
| 44 | + /> |
| 45 | + ) |
| 46 | + )} |
| 47 | + </ul> |
110 | 48 | ); |
111 | 49 | }; |
112 | 50 |
|
113 | | -export default EnhancedOption; |
| 51 | +export default EnhancedListBox; |
0 commit comments