@@ -3,23 +3,17 @@ import { ct0 } from "~ui/i18n/ct0"
33import { t4multiselect } from "~ui/input/select/t4multiselect"
44import { classArr } from "~ui/utils/classArr"
55import type { SignalObject } from "~ui/utils/createSignalObject"
6- import type { HasGetOptions } from "~ui/utils/HasGetOptions"
7- import type { MayHaveChildren } from "~ui/utils/MayHaveChildren"
86import type { MayHaveClass } from "~ui/utils/MayHaveClass"
9- import { type MayHaveDisabledAccessor , isDisabled } from "~ui/utils/MayHaveDisabledAccessor"
10- import type { SelectionItem } from "~ui/utils/SelectionItem"
11- import type { ValueOrAccessor } from "~ui/utils/ValueOrAccessor"
127
138/**
149 * https://github.com/radix-ui/primitives/blob/main/packages/react/radio-group/src/Radio.tsx
1510 */
16- export interface RadioSwitchProps extends MayHaveClass , MayHaveChildren , RadioSwitchStateProps , HasGetOptions , MayHaveDisabledAccessor {
11+ export interface RadioSwitchProps extends MayHaveClass {
1712 id ?: string
18- disabled ?: ValueOrAccessor < boolean >
19- }
20-
21- type RadioSwitchStateProps = {
22- valueSignal : SignalObject < SelectionItem | null >
13+ valueSignal : SignalObject < string >
14+ getOptions : ( ) => string [ ]
15+ valueText ?: ( value : string ) => string
16+ disabled ?: boolean
2317}
2418
2519export function RadioSwitch ( p : RadioSwitchProps ) {
@@ -28,7 +22,7 @@ export function RadioSwitch(p: RadioSwitchProps) {
2822 < div
2923 id = { p . id }
3024 role = "radiogroup"
31- data-disabled = { isDisabled ( p ) }
25+ data-disabled = { p . disabled }
3226 class = { classArr (
3327 "flex flex-wrap gap-2" ,
3428 // "grid gap-2 rounded-md p-2",
@@ -37,8 +31,16 @@ export function RadioSwitch(p: RadioSwitchProps) {
3731 p . class ,
3832 ) }
3933 >
40- < Key each = { p . getOptions ( ) } by = { ( item ) => item . value } fallback = { < NoItems /> } >
41- { ( item ) => < Option item = { item ( ) } valueSignal = { p . valueSignal } disabled = { p . disabled } filled = { filled } /> }
34+ < Key each = { p . getOptions ( ) } by = { ( item ) => item } fallback = { < NoItems /> } >
35+ { ( item ) => (
36+ < Option
37+ item = { item ( ) }
38+ valueSignal = { p . valueSignal }
39+ disabled = { p . disabled }
40+ filled = { filled }
41+ valueText = { p . valueText }
42+ />
43+ ) }
4244 </ Key >
4345 </ div >
4446 )
@@ -48,57 +50,49 @@ function NoItems(p: MayHaveClass) {
4850 return < div class = { p . class } > { ct0 ( t4multiselect . No_entries ) } </ div >
4951}
5052
51- interface Option2Props extends RadioSwitchStateProps , MayHaveDisabledAccessor , MayHaveClass {
52- item : SelectionItem
53+ interface Option2Props extends MayHaveClass {
54+ item : string
55+ valueSignal : SignalObject < string >
5356 filled : boolean
57+ valueText ?: ( value : string ) => string
58+ disabled ?: boolean
5459}
5560
5661function Option ( p : Option2Props ) {
57- // console.log("Option", p.item.value, "value:", p.valueSignal.get())
5862 return (
5963 < button
6064 type = "button"
6165 role = "radio"
62- // value={p.item.value}
63- disabled = { isDisabled ( p ) || isDisabled ( p . item ) }
66+ disabled = { p . disabled }
6467 aria-checked = { isChecked ( p ) }
6568 data-checked = { isChecked ( p ) }
6669 data-state = { isChecked ( p ) ? "checked" : "unchecked" }
67- data-disabled = { isDisabled ( p ) || isDisabled ( p . item ) }
70+ data-disabled = { p . disabled }
6871 value = { isChecked ( p ) ? "on" : "off" }
6972 class = { classArr (
70- // "block",
7173 "cursor-pointer select-none" ,
7274 "rounded-sm" ,
7375 "px-3 py-2 text-center" ,
7476 isChecked ( p ) ? "bg-blue-500 text-white" : "hover:bg-slate-50 dark:hover:bg-slate-900" , // bg hover
7577 "flex gap-2" ,
7678 p . class ,
7779 ) }
78- onClick = { ( e ) => optionToggle ( p ) }
80+ onClick = { ( ) => optionToggle ( p ) }
7981 >
8082 { getText ( p ) }
8183 </ button >
8284 )
8385}
8486
85- function optionToggle ( p : OptionProps ) {
86- let prev = p . valueSignal . get ( )
87- // console.log("optionToggle", p.item.value, "prev:", prev)
88- if ( prev === p . item ) return
87+ function optionToggle ( p : Option2Props ) {
88+ if ( p . valueSignal . get ( ) === p . item ) return
8989 p . valueSignal . set ( p . item )
9090}
9191
92- export interface OptionProps extends RadioSwitchStateProps {
93- item : SelectionItem
94- }
95-
96- function isChecked ( p : OptionProps ) {
97- return p . item . value === p . valueSignal . get ( ) ?. value
92+ function isChecked ( p : Option2Props ) {
93+ return p . item === p . valueSignal . get ( )
9894}
9995
100- function getText ( p : OptionProps ) : string {
101- const amount = p . item . amount ?.( )
102- if ( ! amount ) return p . item . label
103- return `${ p . item . label } (${ amount } )`
96+ function getText ( p : Option2Props ) : string {
97+ return p . valueText ? p . valueText ( p . item ) : p . item
10498}
0 commit comments