Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support finding initial value from a list of values #161

Merged
merged 2 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/npm-fastui/src/components/FormField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ export const FormFieldSelectReactComp: FC<FormFieldSelectProps> = (props) => {

const className = useClassName(props)
const classNameSelectReact = useClassName(props, { el: 'select-react' })
let value
if (Array.isArray(initial)) {
value = findDefaultArray(options, initial)
} else {
value = findDefault(options, initial)
}

const reactSelectOnChanged = () => {
// TODO this is a hack to wait for the input to be updated, can we do better?
Expand All @@ -164,7 +170,7 @@ export const FormFieldSelectReactComp: FC<FormFieldSelectProps> = (props) => {
className={classNameSelectReact}
isMulti={multiple ?? false}
isClearable
defaultValue={findDefault(options, initial)}
defaultValue={value}
name={name}
required={required}
isDisabled={locked}
Expand Down Expand Up @@ -193,6 +199,11 @@ const SelectOptionComp: FC<{ option: SelectOption | SelectGroup }> = ({ option }
}
}

function findDefaultArray(options: SelectOptions, value: string[]): SelectOption[] {
const foundValues = value.map((v) => findDefault(options, v))
return foundValues.filter((v) => v) as SelectOption[]
}

function findDefault(options: SelectOptions, value?: string): SelectOption | undefined {
for (const option of options) {
if ('options' in option) {
Expand Down
2 changes: 1 addition & 1 deletion src/npm-fastui/src/models.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ export interface FormFieldSelect {
className?: ClassName
options: SelectOptions
multiple?: boolean
initial?: string
initial?: string[] | string
vanilla?: boolean
placeholder?: string
type: 'FormFieldSelect'
Expand Down
2 changes: 1 addition & 1 deletion src/python-fastui/fastui/components/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class FormFieldFile(BaseFormField):
class FormFieldSelect(BaseFormField):
options: forms.SelectOptions
multiple: _t.Union[bool, None] = None
initial: _t.Union[str, None] = None
initial: _t.Union[_t.List[str], str, None] = None
vanilla: _t.Union[bool, None] = None
placeholder: _t.Union[str, None] = None
type: _t.Literal['FormFieldSelect'] = 'FormFieldSelect'
Expand Down
Loading