Skip to content

Commit 5830432

Browse files
authored
fix: support finding initial value from a list of values (#161)
1 parent 4037679 commit 5830432

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

src/npm-fastui/src/components/FormField.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ export const FormFieldSelectReactComp: FC<FormFieldSelectProps> = (props) => {
174174

175175
const className = useClassName(props)
176176
const classNameSelectReact = useClassName(props, { el: 'select-react' })
177+
let value
178+
if (Array.isArray(initial)) {
179+
value = findDefaultArray(options, initial)
180+
} else {
181+
value = findDefault(options, initial)
182+
}
177183

178184
const reactSelectOnChanged = () => {
179185
// TODO this is a hack to wait for the input to be updated, can we do better?
@@ -191,7 +197,7 @@ export const FormFieldSelectReactComp: FC<FormFieldSelectProps> = (props) => {
191197
className={classNameSelectReact}
192198
isMulti={multiple ?? false}
193199
isClearable
194-
defaultValue={findDefault(options, initial)}
200+
defaultValue={value}
195201
name={name}
196202
required={required}
197203
isDisabled={locked}
@@ -220,6 +226,11 @@ const SelectOptionComp: FC<{ option: SelectOption | SelectGroup }> = ({ option }
220226
}
221227
}
222228

229+
function findDefaultArray(options: SelectOptions, value: string[]): SelectOption[] {
230+
const foundValues = value.map((v) => findDefault(options, v))
231+
return foundValues.filter((v) => v) as SelectOption[]
232+
}
233+
223234
function findDefault(options: SelectOptions, value?: string): SelectOption | undefined {
224235
for (const option of options) {
225236
if ('options' in option) {

src/npm-fastui/src/models.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ export interface FormFieldSelect {
391391
className?: ClassName
392392
options: SelectOptions
393393
multiple?: boolean
394-
initial?: string
394+
initial?: string[] | string
395395
vanilla?: boolean
396396
placeholder?: string
397397
type: 'FormFieldSelect'

src/python-fastui/fastui/components/forms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class FormFieldFile(BaseFormField):
5757
class FormFieldSelect(BaseFormField):
5858
options: forms.SelectOptions
5959
multiple: _t.Union[bool, None] = None
60-
initial: _t.Union[str, None] = None
60+
initial: _t.Union[_t.List[str], str, None] = None
6161
vanilla: _t.Union[bool, None] = None
6262
placeholder: _t.Union[str, None] = None
6363
type: _t.Literal['FormFieldSelect'] = 'FormFieldSelect'

0 commit comments

Comments
 (0)