@@ -17,23 +17,56 @@ import {
1717} from "../hid-usages" ;
1818import { useCallback , useMemo } from "react" ;
1919
20+ export interface HidUsagePage {
21+ id : number ;
22+ min ?: number ;
23+ max ?: number ;
24+ }
25+
2026export interface HidUsagePickerProps {
27+ label ?: string ;
2128 value ?: number ;
22- usagePages : number [ ] ;
29+ usagePages : HidUsagePage [ ] ;
2330 onValueChanged : ( value ?: number ) => void ;
2431}
2532
33+ type UsageSectionProps = HidUsagePage ;
34+
35+ const UsageSection = ( { id, min, max} : UsageSectionProps ) => {
36+ const info = useMemo ( ( ) => hid_usage_page_get_ids ( id ) , [ id ] ) ;
37+
38+ let usages = useMemo ( ( ) => {
39+ let usages = info ?. UsageIds || [ ] ;
40+ if ( max || min ) {
41+ usages = usages . filter ( i => i . Id <= ( max || Number . MAX_SAFE_INTEGER ) && i . Id >= ( min || 0 ) ) ;
42+ }
43+
44+ return usages ;
45+ } , [ id , min , max , info ] ) ;
46+
47+ return (
48+ < Section id = { id } >
49+ < Header className = "text-text-base/50" > { info ?. Name } </ Header >
50+ < Collection items = { usages } >
51+ { ( i ) => (
52+ < ListBoxItem
53+ className = "rac-hover:text-accent pl-3 relative rac-focus:text-accent cursur-default select-none rac-selected:before:content-['✔'] before:absolute before:left-[0] before:top-[0]"
54+ id = { hid_usage_from_page_and_id ( id , i . Id ) }
55+ >
56+ { i . Name }
57+ </ ListBoxItem >
58+ ) }
59+ </ Collection >
60+ </ Section >
61+ ) ;
62+ }
63+
2664export const HidUsagePicker = ( {
65+ label,
2766 value,
2867 usagePages,
2968 onValueChanged,
3069} : HidUsagePickerProps ) => {
31- let pageObjects = useMemo ( ( ) => {
32- return usagePages
33- . map ( ( p ) => ( { id : p , info : hid_usage_page_get_ids ( p ) } ) )
34- . filter ( ( i ) => ! ! i . info ) ;
35- } , [ usagePages ] ) ;
36-
3770 const selectionChanged = useCallback (
3871 ( e : Key | null ) => {
3972 let value = typeof e == "number" ? e : undefined ;
@@ -44,7 +77,7 @@ export const HidUsagePicker = ({
4477
4578 return (
4679 < ComboBox selectedKey = { value } onSelectionChange = { selectionChanged } >
47- < Label > HID Usage </ Label >
80+ { label && < Label > { label } </ Label > }
4881 < div >
4982 < Input className = "p-1 rounded" />
5083 < Button className = "ml-[-1.75em] px-[0.25em] py-0 rounded rounded-md bg-accent w-6 h-6" >
@@ -53,25 +86,11 @@ export const HidUsagePicker = ({
5386 </ div >
5487 < Popover className = "w-[var(--trigger-width)] max-h-4 border rounded border-text-base bg-bg-base" >
5588 < ListBox
56- items = { pageObjects }
89+ items = { usagePages }
5790 className = "block max-h-[30vh] min-h-[unset] overflow-auto m-2"
5891 selectionMode = "single"
5992 >
60- { ( p ) => (
61- < Section id = { p . id } >
62- < Header className = "text-text-base/50" > { p . info ?. Name } </ Header >
63- < Collection items = { p . info ?. UsageIds } >
64- { ( i ) => (
65- < ListBoxItem
66- className = "rac-hover:text-accent ml-2 rac-focus:text-accent cursur-default select-none"
67- id = { hid_usage_from_page_and_id ( p . id , i . Id ) }
68- >
69- { i . Name }
70- </ ListBoxItem >
71- ) }
72- </ Collection >
73- </ Section >
74- ) }
93+ { ( { id, min, max} ) => < UsageSection id = { id } min = { min } max = { max } /> }
7594 </ ListBox >
7695 </ Popover >
7796 </ ComboBox >
0 commit comments