| 18 | } |
| 19 | |
| 20 | export function Select<T>(props: SelectProps<T> & ButtonProps) { |
| 21 | const [local, others] = splitProps(props, [ |
| 22 | "class", |
| 23 | "classList", |
| 24 | "placeholder", |
| 25 | "options", |
| 26 | "current", |
| 27 | "value", |
| 28 | "label", |
| 29 | "groupBy", |
| 30 | "onSelect", |
| 31 | "children", |
| 32 | ]) |
| 33 | const grouped = createMemo(() => { |
| 34 | const result = pipe( |
| 35 | local.options, |
| 36 | groupBy((x) => (local.groupBy ? local.groupBy(x) : "")), |
| 37 | // mapValues((x) => x.sort((a, b) => a.title.localeCompare(b.title))), |
| 38 | entries(), |
| 39 | map(([k, v]) => ({ category: k, options: v })), |
| 40 | ) |
| 41 | return result |
| 42 | }) |
| 43 | |
| 44 | return ( |
| 45 | // @ts-ignore |
| 46 | <Kobalte<T, { category: string; options: T[] }> |
| 47 | {...others} |
| 48 | data-component="select" |
| 49 | value={local.current} |
| 50 | options={grouped()} |
| 51 | optionValue={(x) => (local.value ? local.value(x) : (x as string))} |
| 52 | optionTextValue={(x) => (local.label ? local.label(x) : (x as string))} |
| 53 | optionGroupChildren="options" |
| 54 | placeholder={local.placeholder} |
| 55 | sectionComponent={(local) => ( |
| 56 | <Kobalte.Section data-slot="select-section">{local.section.rawValue.category}</Kobalte.Section> |
| 57 | )} |
| 58 | itemComponent={(itemProps) => ( |
| 59 | <Kobalte.Item |
| 60 | data-slot="select-select-item" |
| 61 | classList={{ |
| 62 | ...(local.classList ?? {}), |
| 63 | [local.class ?? ""]: !!local.class, |
| 64 | }} |
| 65 | {...itemProps} |
| 66 | > |
| 67 | <Kobalte.ItemLabel data-slot="select-select-item-label"> |
| 68 | {local.children |
| 69 | ? local.children(itemProps.item.rawValue) |
| 70 | : local.label |
| 71 | ? local.label(itemProps.item.rawValue) |
| 72 | : (itemProps.item.rawValue as string)} |
| 73 | </Kobalte.ItemLabel> |
| 74 | <Kobalte.ItemIndicator data-slot="select-select-item-indicator"> |
| 75 | <Icon name="check-small" size="small" /> |
| 76 | </Kobalte.ItemIndicator> |
| 77 | </Kobalte.Item> |