Skip to content

Commit

Permalink
fix: rename select to transform
Browse files Browse the repository at this point in the history
  • Loading branch information
Birkbjo committed Dec 10, 2024
1 parent af49454 commit 210a1e9
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type OwnProps<TModel> = {
showNoValueOption?: boolean
}

export type BaseModelSingleSelectProps<TModel extends DisplayableModel> = Omit<
export type BaseModelSingleSelectProps<TModel extends DisplayableModel = DisplayableModel> = Omit<
SearchableSingleSelectPropTypes,
keyof OwnProps<TModel> | 'options' | 'selected'
> &
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type RelevantUseFieldProps<TModel extends DisplayableModel> = Pick<
'validate' | 'validateFields' | 'initialValue' | 'format' | 'parse' | 'data'
>

type ModelSingleSelectFieldProps<TModel extends DisplayableModel> = Omit<
export type ModelSingleSelectFieldProps<TModel extends DisplayableModel = DisplayableModel> = Omit<
ModelSingleSelectProps<TModel>,
'selected' | 'onChange'
> &
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const defaultQuery = {
},
} satisfies Omit<PlainResourceQuery, 'resource'>

export type ModelSingleSelectProps<TModel extends DisplayableModel> = Omit<
export type ModelSingleSelectProps<TModel extends DisplayableModel = DisplayableModel> = Omit<
BaseModelSingleSelectProps<TModel>,
| 'available'
| 'onFilterChange'
Expand All @@ -32,18 +32,16 @@ export type ModelSingleSelectProps<TModel extends DisplayableModel> = Omit<
> & {
query: Omit<PlainResourceQuery, 'id'>
onFilterChange?: (value: string) => void
select?: (value: TModel[]) => TModel[]
transform?: (value: TModel[]) => TModel[]
}

export const ModelSingleSelect = <TModel extends DisplayableModel>({
selected,
query,
select,
transform,
...baseModelSingleSelectProps
}: ModelSingleSelectProps<TModel>) => {
const queryFn = useBoundResourceQueryFn()
// keep select in ref, so we dont recompute for inline selects
const selectRef = useRef(select)
const [searchTerm, setSearchTerm] = useState('')

const searchFilter = `identifiable:token:${searchTerm}`
Expand Down Expand Up @@ -73,12 +71,13 @@ export const ModelSingleSelect = <TModel extends DisplayableModel>({
const allDataMap = useMemo(() => {
const flatData =
queryResult.data?.pages.flatMap((page) => page[modelName]) ?? []
if (selectRef.current) {
return selectRef.current(flatData)
}
return flatData
}, [queryResult.data, modelName])

const resolvedAvailable = useMemo(() => {
return transform ? transform(allDataMap) : allDataMap
}, [allDataMap, transform])

const handleFilterChange = useDebouncedCallback(({ value }) => {
if (value != undefined) {
setSearchTerm(value)
Expand All @@ -90,7 +89,7 @@ export const ModelSingleSelect = <TModel extends DisplayableModel>({
<BaseModelSingleSelect
{...baseModelSingleSelectProps}
selected={selected}
available={allDataMap}
available={resolvedAvailable}
onFilterChange={handleFilterChange}
onRetryClick={queryResult.refetch}
showEndLoader={!!queryResult.hasNextPage}
Expand Down
6 changes: 4 additions & 2 deletions src/pages/dataElements/fields/CategoryComboField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React, { useEffect } from 'react'
import { useField, useForm } from 'react-final-form'
import { useHref } from 'react-router'
import { EditableFieldWrapper } from '../../../components'
import { ModelSingleSelectField } from '../../../components/metadataFormControls/ModelSingleSelect/ModelSingleSelectField'
import { ModelSingleSelectField, ModelSingleSelectFieldProps } from '../../../components/metadataFormControls/ModelSingleSelect/ModelSingleSelectField'
import { useRefreshModelSingleSelect } from '../../../components/metadataFormControls/ModelSingleSelect/useRefreshSingleSelect'
import { DEFAULT_CATEGORY_COMBO } from '../../../lib'
import { PlainResourceQuery } from '../../../types'
Expand All @@ -17,6 +17,8 @@ const query = {
},
} as const satisfies PlainResourceQuery

// stable reference for transform function
const withDefaultCategoryCombo: ModelSingleSelectFieldProps['transform'] = (value) => [DEFAULT_CATEGORY_COMBO, ...value]
/*
* @TODO: Verify that the api ignores the category combo when it's disabled.
* If it does not, file a jira issue and "escalate" this so it will be
Expand Down Expand Up @@ -55,7 +57,7 @@ export function CategoryComboField() {
name="categoryCombo"
label={i18n.t('Category combo')}
required
select={(value) => [DEFAULT_CATEGORY_COMBO, ...value]}
transform={withDefaultCategoryCombo}
disabled={disabled}
/>
</div>
Expand Down

0 comments on commit 210a1e9

Please sign in to comment.