Skip to content

Commit a420cc6

Browse files
fix: refactor SelectionDropdown and simplify ModelSelect usage
Refactored SelectionDropdown to streamline value handling, remove unused props, and simplify portal/content rendering. Updated ModelSelect to match the new SelectionDropdown API by removing the unused 'value' prop.
1 parent f6dc115 commit a420cc6

File tree

2 files changed

+24
-31
lines changed

2 files changed

+24
-31
lines changed

src/componentsV2/base/SelectionDropdown/index.tsx

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,45 @@ import { Select } from 'heroui-native'
22
import type { FC } from 'react'
33
import React from 'react'
44

5+
interface SelectOption {
6+
value: string
7+
label: string
8+
}
9+
510
export interface SelectionDropdownItem {
611
id?: string
712
key?: string
813
label: React.ReactNode | string
914
onSelect?: () => void
10-
[x: string]: any
1115
}
1216

1317
export interface SelectionDropdownProps {
1418
items: SelectionDropdownItem[]
1519
children: React.ReactNode
1620
shouldDismissMenuOnSelect?: boolean
17-
value?: string
1821
onValueChange?: (value: string) => void
19-
usePortal?: boolean
2022
}
2123

2224
const SelectionDropdown: FC<SelectionDropdownProps> = ({
2325
items,
2426
children,
2527
shouldDismissMenuOnSelect = true,
26-
usePortal = true,
2728
onValueChange
2829
}) => {
29-
const handleValueChange = (selectedItem: any) => {
30-
const newValue = selectedItem?.value || selectedItem
31-
onValueChange?.(newValue)
30+
const handleValueChange = (selectedItem: SelectOption | undefined) => {
31+
if (!selectedItem) {
32+
return
33+
}
34+
35+
const newValue = selectedItem.value
36+
37+
if (onValueChange) {
38+
onValueChange(newValue)
39+
}
3240

3341
const foundItem = items.find(item => item.id === newValue || item.key === newValue)
34-
if (foundItem) {
35-
foundItem.onSelect?.()
42+
if (foundItem && foundItem.onSelect) {
43+
foundItem.onSelect()
3644
}
3745
}
3846

@@ -43,27 +51,15 @@ const SelectionDropdown: FC<SelectionDropdownProps> = ({
4351
return <Select.Item key={itemValue} value={itemValue} label={itemLabel} />
4452
})
4553

46-
const contentProps = {
47-
width: 'trigger' as const,
48-
placement: 'bottom' as const,
49-
align: 'center' as const
50-
}
51-
5254
return (
5355
<Select onValueChange={handleValueChange}>
5456
<Select.Trigger asChild>{children}</Select.Trigger>
55-
56-
{usePortal ? (
57-
<Select.Portal>
58-
<Select.Overlay closeOnPress={shouldDismissMenuOnSelect} />
59-
<Select.Content {...contentProps}>{renderItems()}</Select.Content>
60-
</Select.Portal>
61-
) : (
62-
<>
63-
<Select.Overlay closeOnPress={shouldDismissMenuOnSelect} />
64-
<Select.Content {...contentProps}>{renderItems()}</Select.Content>
65-
</>
66-
)}
57+
<Select.Portal>
58+
<Select.Overlay closeOnPress={shouldDismissMenuOnSelect} />
59+
<Select.Content width="trigger" placement="bottom" align="center">
60+
{renderItems()}
61+
</Select.Content>
62+
</Select.Portal>
6763
</Select>
6864
)
6965
}

src/componentsV2/features/SettingsScreen/providers/ModelSelect.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@ export function ModelSelect({ provider, onSelectModel }: ModelSelectProps) {
3636
}
3737

3838
return (
39-
<SelectionDropdown
40-
items={selectOptions}
41-
value={selectedModel ? getModelUniqId(selectedModel) : ''}
42-
onValueChange={handleValueChange}>
39+
<SelectionDropdown items={selectOptions} onValueChange={handleValueChange}>
4340
<Button className="rounded-xl" pressableFeedbackVariant="ripple" variant="tertiary">
4441
<Button.Label>{selectedModel ? selectedModel.id : t('settings.provider.api_check.tooltip')}</Button.Label>
4542
<ChevronDown />

0 commit comments

Comments
 (0)