# All files / src/Form Form.type.ts

100% Statements 3/3
100% Branches 0/0
100% Functions 0/0
100% Lines 3/3

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    1x

    1x

    1x

     import * as React from 'react'
    import * as PropTypes from 'prop-types'
    import Field from './components/Field'
    import { StandardProps } from '..'

    export type FormClassProps = 'root' | 'table'
    export type FormFieldClassProps = 'label' | 'labelRequired'

    export interface FormValue {
      [K: string]: any
    }
    export interface FormError {
      [K: string]: any
    }
    export interface FormState {
      values: FormValue
      errors: FormError
    }

    export interface FormProps
      extends Omit<
        StandardProps<React.FormHTMLAttributes<HTMLFormElement>, HTMLFormElement>,
        'prefix'
      > {
      children: React.ReactElement | React.ReactElement[]
      prefix?: React.ReactChild
      suffix?: React.ReactChild

      initialState?: FormValue
      onSubmit: (value: FormValue, errors?: FormError) => void
    }

    export interface FormType extends React.ForwardRefExoticComponent<FormProps> {
      Field: typeof Field
    }

    export interface ValidationRule {
      /** validation error message */
      message?: React.ReactNode
      /** built-in validation type, available options: https://github.com/yiminghe/async-validator#type */
      type?: string
      /** indicates whether field is required */
      required?: boolean
      /** treat required fields that only contain whitespace as errors */
      whitespace?: boolean
      /** validate the exact length of a field */
      len?: number
      /** validate the min length of a field */
      min?: number
      /** validate the max length of a field */
      max?: number
      /** validate the value from a list of possible values */
      enum?: string | string[]
      /** validate from a regular expression */
      pattern?: RegExp
      /** transform a value before validation */
      transform?: (value: any) => any
      /** custom validate function (Note: callback must be called) */
      validator?: (rule: any, value: any, callback: any, source?: any, options?: any) => any
    }

    export interface FieldProps
      extends StandardProps<React.HTMLAttributes<HTMLTableRowElement>, HTMLTableRowElement> {
      children: React.ReactElement
      onChange?: (value: any) => void
      label?: string

      name: string
      rules?: ValidationRule[]
    }

    export const FormPropTypes = {
      children: PropTypes.oneOfType([
        PropTypes.element,
        // @ts-ignore
        PropTypes.arrayOf<PropTypes.Requireable<PropTypes.ReactElementLike>>(PropTypes.element)
      ]).isRequired,
      prefix: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.element]),
      suffix: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.element]),
      initialState: PropTypes.object,
      onSubmit: PropTypes.func.isRequired
    }

    export const FieldPropTypes = {
      children: PropTypes.element.isRequired,
      onChange: PropTypes.func,
      label: PropTypes.string,
      name: PropTypes.string.isRequired,
      rules: PropTypes.array
    }