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

feat(types): add types for forms collection #1415

Merged
merged 9 commits into from
Oct 19, 2023
Merged
16 changes: 16 additions & 0 deletions collections/forms/types/CheckboxFieldFF/CheckboxFieldFF.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { FieldRenderProps } from 'react-final-form'
import type { CheckboxFieldProps } from '@dhis2-ui/checkbox'
import React from 'react'

type CheckBoxValue = CheckboxFieldProps['value']
type CheckboxRestProps = Omit<
CheckboxFieldProps,
'onChange' | 'value' | 'checked' | 'name'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should onBlur be omitted too

Copy link
Member Author

@Birkbjo Birkbjo Oct 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onChange is the only handler that won't be called. onBlur and onFocus will call both the callback on input.onBlur and the passed onBlur in root-props. onChange just calls the one on input. This seems consistent for all FF components. See here https://github.com/dhis2/ui/blob/feat/types-fieldff/collections/forms/src/CheckboxFieldFF/CheckboxFieldFF.js#L33-L35 .

>

export type CheckboxFieldFFProps = FieldRenderProps<CheckBoxValue> &
CheckboxRestProps & {
showValidStatus?: boolean
}

export const CheckboxFieldFF: React.FC<CheckboxFieldFFProps>
9 changes: 9 additions & 0 deletions collections/forms/types/FieldGroupFF/FieldGroupFF.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react'
import type { FieldGroupProps } from '@dhis2-ui/field'

export type FieldGroupFFProps = Omit<
FieldGroupProps,
'children' | 'label' | 'name' | 'required'
>

export const FieldGroupFF: React.FC<FieldGroupFFProps>
18 changes: 18 additions & 0 deletions collections/forms/types/FileInputFieldFF/FileInputFieldFF.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from 'react'
import type { FileInputFieldProps } from '@dhis2-ui/file-input'
import type { FieldRenderProps } from 'react-final-form'

export type FilesValue = File[] | undefined | null | ''

type FileInputRestProps = Omit<
FileInputFieldProps,
'onChange' | 'multiple' | 'name'
>

export type FileInputFieldFFProps = FieldRenderProps<FilesValue> &
FileInputRestProps & {
showValidStatus?: boolean
multifile: FileInputFieldProps['multiple']
}

export const FileInputFieldFF: React.FC<FileInputFieldFFProps>
15 changes: 15 additions & 0 deletions collections/forms/types/InputFieldFF/InputFieldFF.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react'
import type { InputFieldProps } from '@dhis2-ui/input'
import type { FieldRenderProps } from 'react-final-form'

export type InputValue = InputFieldProps['value']

type InputFieldRestProps = Omit<InputFieldProps, 'onChange' | 'value' | 'name'>

export type InputFieldFFProps = FieldRenderProps<InputValue> &
InputFieldRestProps & {
showLoadingStatus?: boolean
showValidStatus?: boolean
}

export const InputFieldFF: React.FC<InputFieldFFProps>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React from 'react'
import type { FieldRenderProps } from 'react-final-form'
import type {
MultiSelectOptionProps,
MultiSelectFieldProps,
} from '@dhis2-ui/select'

type InputValue = MultiSelectFieldProps['selected'] | ''

type MultiSelectFieldRestProps = Omit<
MultiSelectFieldProps,
'onChange' | 'value'
>

type MultiSelectOptions = Array<Pick<MultiSelectOptionProps, 'value' | 'label'>>

export type MultiSelectFieldFFProps = FieldRenderProps<InputValue> &
MultiSelectFieldRestProps & {
showLoadingStatus?: boolean
showValidStatus?: boolean
options: MultiSelectOptions
}

export const MultiSelectFieldFF: React.FC<MultiSelectFieldFFProps>
17 changes: 17 additions & 0 deletions collections/forms/types/RadioFieldFF/RadioFieldFF.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from 'react'
import type { FieldRenderProps } from 'react-final-form'
import type { RadioProps } from '@dhis2-ui/radio'

type InputValue = RadioProps['value']

type RadioRestProps = Omit<
RadioProps,
'onChange' | 'value' | 'checked' | 'name'
>

export type RadioFieldFFProps = FieldRenderProps<InputValue> &
RadioRestProps & {
showValidStatus?: boolean
}

export const RadioFieldFF: React.FC<RadioFieldFFProps>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React from 'react'
import type { FieldRenderProps } from 'react-final-form'
import type {
SingleSelectOptionProps,
SingleSelectFieldProps,
} from '@dhis2-ui/select'

type InputValue = SingleSelectFieldProps['selected']

type SingleSelectOptions = Array<
Pick<SingleSelectOptionProps, 'value' | 'label'>
>

type SingleSelectRestProps = Omit<
SingleSelectFieldProps,
'onChange' | 'value' | 'name'
>

export type SingleSelectFieldFFProps = FieldRenderProps<InputValue> &
SingleSelectRestProps & {
showLoadingStatus?: boolean
showValidStatus?: boolean
options: SingleSelectOptions
}

export const SingleSelectFieldFF: React.FC<SingleSelectFieldFFProps>
15 changes: 15 additions & 0 deletions collections/forms/types/SwitchFieldFF/SwitchFieldFF.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react'
import type { FieldRenderProps } from 'react-final-form'
import type { SwitchFieldProps } from '@dhis2-ui/switch'

type InputValue = SwitchFieldProps['value']

type SwitchFieldRestProps = Omit<
SwitchFieldProps,
'onChange' | 'checked' | 'value' | 'name'
>

export type SwitchFieldFFProps = FieldRenderProps<InputValue> &
SwitchFieldRestProps

export const SwitchFieldFF: React.FC<SwitchFieldFFProps>
15 changes: 15 additions & 0 deletions collections/forms/types/TextAreaFieldFF/TextAreaFieldFF.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react'
import type { FieldRenderProps } from 'react-final-form'
import type { TextAreaFieldProps } from '@dhis2-ui/text-area'

type InputValue = TextAreaFieldProps['value']

type TextAreaFieldRestProps = Omit<
TextAreaFieldProps,
'onChange' | 'value' | 'name'
>

export type TextAreaFieldFFProps = FieldRenderProps<InputValue> &
TextAreaFieldRestProps

export const TextAreaFieldFF: React.FC<TextAreaFieldFFProps>
28 changes: 28 additions & 0 deletions collections/forms/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as FinalForm from 'final-form'
import * as ReactFinalForm from 'react-final-form'

export { CheckboxFieldFF } from './CheckboxFieldFF/CheckboxFieldFF'
export { FileInputFieldFF } from './FileInputFieldFF/FileInputFieldFF'
export { InputFieldFF } from './InputFieldFF/InputFieldFF'
export { MultiSelectFieldFF } from './MultiSelectFieldFF/MutliSelectFieldFF'
export { SingleSelectFieldFF } from './SingleSelectFieldFF/SingleSelectFieldFF'
export { RadioFieldFF } from './RadioFieldFF/RadioFieldFF'
export { SwitchFieldFF } from './SwitchFieldFF/SwitchFieldFF'
export { TextAreaFieldFF } from './TextAreaFieldFF/TextAreaFieldFF'
export { FieldGroupFF } from './FieldGroupFF/FieldGroupFF'

export * from './transformers'
export * from './validators'

/**
* Allows direct access to the FinalForm library. Please note that this is considered advanced
* usage and that you need to stay up to date with breaking changes in the FinalForm library.
*/
export { FinalForm }

/**
* Allows direct access to the ReactFinalForm library. Please note that this is considered
* advanced usage and that you need to stay up to date with breaking changes in the FinalForm
* library.
*/
export { ReactFinalForm }
6 changes: 6 additions & 0 deletions collections/forms/types/transformers/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export type ValuesWithId = Array<{ id: string }> | undefined

export const arrayWithIdObjects: {
format: (value: ValuesWithId) => string
pars: (value: string) => ValuesWithId
}
31 changes: 31 additions & 0 deletions collections/forms/types/validators/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export type Validator = (value: unknown) => string | undefined

export const alphaNumeric: Validator
export const boolean: Validator
export const composeValidators: (...validators: Validator[]) => Validator

export const createCharacterLengthRange: (
lowerBound: number,
upperBound: number,
customMessage?: string
) => Validator
export const createEqualTo: (key: string, description?: string) => Validator
export const createMaxCharacterLength: (upperBound: number) => Validator
export const createMaxNumber: (upperBound: number) => Validator
export const createMinCharacterLength: (lowerBound: number) => Validator
export const createMinNumber: (lowerBound: number) => Validator
export const createNumberRange: (
lowerBound: number,
upperBound: number,
customMessage?: string
) => Validator
export const createPattern: (pattern: RegExp, message?: string) => Validator
export const dhis2Password: Validator
export const dhis2Username: Validator
export const email: Validator
export const hasValue: Validator
export const integer: Validator
export const internationalPhoneNumber: Validator
export const number: Validator
export const string: Validator
export const url: Validator
6 changes: 3 additions & 3 deletions components/select/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { LayerBackdropClickEvent } from '@dhis2-ui/layer'
import { CheckboxProps } from '@dhis2-ui/checkbox'

interface BaseEventPayload {
selected?: string | string[]
selected: string | string[]
}
interface SingleSelectEventPayload extends BaseEventPayload {
selected?: string
selected: string
}
interface MultiSelectEventPayload extends BaseEventPayload {
selected?: string[]
selected: string[]
}

type SelectEventHandler<
Expand Down
Loading