Skip to content

Commit

Permalink
fix: pass usefieldff to modelsingleselectfield
Browse files Browse the repository at this point in the history
  • Loading branch information
Birkbjo committed Dec 10, 2024
1 parent 6365b6b commit af49454
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const toDisplayOption = (model: DisplayableModel) => ({
})

type OwnProps<TModel> = {
selected: TModel | undefined
selected?: TModel
available: TModel[]
onChange: (selected: TModel | undefined) => void
showNoValueOption?: boolean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Field } from '@dhis2/ui'
import React from 'react'
import { useField } from 'react-final-form'
import { PlainResourceQuery } from '../../../types'
import { useField, UseFieldConfig } from 'react-final-form'
import { DisplayableModel } from '../../../types/models'
import {
ModelSingleSelectProps,
Expand All @@ -10,31 +9,46 @@ import {

type OwnProps<TModel extends DisplayableModel> = {
name: string
query: PlainResourceQuery
label?: string
placeholder?: string
helpText?: string
required?: boolean
onChange?: ModelSingleSelectProps<TModel>['onChange']
}

type RelevantUseFieldProps<TModel extends DisplayableModel> = Pick<
UseFieldConfig<TModel | undefined>,
'validate' | 'validateFields' | 'initialValue' | 'format' | 'parse' | 'data'
>

type ModelSingleSelectFieldProps<TModel extends DisplayableModel> = Omit<
ModelSingleSelectProps<TModel>,
'selected' | 'onChange'
> &
OwnProps<TModel>
OwnProps<TModel> & RelevantUseFieldProps<TModel>

export function ModelSingleSelectField<TModel extends DisplayableModel>({
name,
query,
label,
helpText,
required,
onChange,
// react-final-form props
validate,
validateFields,
initialValue,
format,
parse,
data,
...modelSingleSelectProps
}: ModelSingleSelectFieldProps<TModel>) {
const { input, meta } = useField<TModel | undefined>(name, {
validateFields: [],
validateFields: validateFields ?? [],
validate,
initialValue,
format,
parse,
data
})

return (
Expand All @@ -55,7 +69,6 @@ export function ModelSingleSelectField<TModel extends DisplayableModel>({
input.onBlur()
onChange?.(selected)
}}
query={query}
/>
</Field>
)
Expand Down

0 comments on commit af49454

Please sign in to comment.