Skip to content

Commit

Permalink
chore: fix type issues after updating dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammer5 committed Dec 5, 2023
1 parent 200875b commit f6043f6
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Validator } from './types'
export const composeAsyncValidators = <Value, FormValues>(
validators: Validator<Value, FormValues>[]
): Validator<Value, FormValues> => {
return async (value: Value, formValues?: FormValues) => {
return async (value?: Value, formValues?: FormValues) => {
for (let i = 0; i < validators.length; ++i) {
const validator = validators[i]
const result = await validator(value, formValues)
Expand Down
6 changes: 4 additions & 2 deletions src/lib/form/composeAsyncValidators/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
type ReturnValue = string | undefined

export type Validator<Value, FormValues = object> = (
value: Value,
value?: Value,
formValues?: FormValues
) => Promise<string | undefined>
) => ReturnValue | Promise<ReturnValue>
39 changes: 27 additions & 12 deletions src/pages/dataElements/fields/DomainField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ import { useField } from 'react-final-form'
import { DOMAIN_TYPE } from '../../../lib'
import classes from './DomainField.module.css'

function createHandler<T>(callback: (e: T) => void) {
return (_: object, e: T) => {
callback(e)
}
}

type FocusEvent = React.FocusEvent<HTMLInputElement>
type ChangeEvent = React.ChangeEvent<HTMLInputElement>

export function DomainField() {
const name = 'domainType'
const validate = (value: string) => (!value ? 'Required' : undefined)
Expand Down Expand Up @@ -42,24 +51,30 @@ export function DomainField() {
{...aggregateInput.input}
className={classes.domainTypeRadioButton}
label={DOMAIN_TYPE.AGGREGATE}
onChange={(
_: object,
e: React.ChangeEvent<HTMLInputElement>
) => {
aggregateInput.input.onChange(e)
}}
onChange={createHandler<ChangeEvent>(
aggregateInput.input.onChange
)}
onBlur={createHandler<FocusEvent>(
aggregateInput.input.onBlur
)}
onFocus={createHandler<FocusEvent>(
aggregateInput.input.onFocus
)}
/>

<Radio
{...trackerInput.input}
label={DOMAIN_TYPE.TRACKER}
className={classes.domainTypeRadioButton}
onChange={(
_: object,
e: React.ChangeEvent<HTMLInputElement>
) => {
trackerInput.input.onChange(e)
}}
onChange={createHandler<ChangeEvent>(
trackerInput.input.onChange
)}
onBlur={createHandler<FocusEvent>(
trackerInput.input.onBlur
)}
onFocus={createHandler<FocusEvent>(
trackerInput.input.onFocus
)}
/>
</div>
</Field>
Expand Down
8 changes: 3 additions & 5 deletions src/pages/dataElements/fields/NameField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function useValidator() {

return useMemo(
() =>
composeAsyncValidators([
composeAsyncValidators<string, FormValues>([
checkIsValueTaken as NameValidator,
checkMaxLength,
required,
Expand All @@ -47,7 +47,7 @@ export function NameField() {
})

return (
<FieldRFF
<FieldRFF<string | undefined>
loading={meta.validating}
component={InputFieldFF}
dataTest="dataelementsformfields-name"
Expand All @@ -60,9 +60,7 @@ export function NameField() {
helpText={i18n.t(
'A data element name should be concise and easy to recognize.'
)}
// Do not pass checkIsValueTaken directly, otherwise memoization
// won't work due to additional arguments being passed
validate={(name: string) => validator(name)}
validate={(name?: string) => validator(name)}
validateFields={[]}
/>
)
Expand Down
6 changes: 3 additions & 3 deletions src/pages/dataElements/fields/ShortNameField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function useValidator() {

return useMemo(
() =>
composeAsyncValidators([
composeAsyncValidators<string, FormValues>([
checkIsValueTaken as ShortNameValidator,
checkMaxLength,
required,
Expand All @@ -47,7 +47,7 @@ export function ShortNameField() {
})

return (
<FieldRFF
<FieldRFF<string | undefined>
loading={meta.validating}
component={InputFieldFF}
dataTest="dataelementsformfields-shortname"
Expand All @@ -58,7 +58,7 @@ export function ShortNameField() {
})}
name="shortName"
helpText={i18n.t('Often used in reports where space is limited')}
validate={validator}
validate={(name?: string) => validator(name)}
validateFields={[]}
/>
)
Expand Down

0 comments on commit f6043f6

Please sign in to comment.