# All files / lib/form index.tsx

100% Statements 40/40
100% Branches 14/14
100% Functions 8/8
100% Lines 34/34

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
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    1741x
    1x
    1x
    1x

    1x

    1x
    17x

    17x
    8x
    8x
    8x

    17x
    17x
    17x

    17x
    17x
    17x

    17x
    12x
    12x

    17x
    11x

    2x

    9x

    17x
    11x

    10x

    1x

    17x
    1x
    1x

    17x

    1x

    1x
     import * as React from "react";
    import validator from './validator';
    import {Row, Col} from '../grid';
    import Input from '../input';
    import {ChangeEventHandler, CSSProperties, FormEvent, ReactElement} from "react";
    import './index.scss';

    export interface FormValue {
      [K: string]: any
    }

    export interface FormRule {
      [K: string]: any;
      message: string
    }

    export interface FormErrors {
      [K: string]: string[];
    }

    interface FormItemCol {
      span?: number,
      style?: CSSProperties
    }

    interface FormLayout {
      labelCol?: FormItemCol,
      wrapperCol?: FormItemCol,
      style?: CSSProperties
    }

    export interface FormField {
      key: string;
      label?: string;
      formItem: { type: string, item?: ReactElement<any> };
      rules?: Array<FormRule>;
    }

    interface IProps {
      values: FormValue;
      fields: Array<FormField>;
      onSubmit?: (values: FormValue, e:FormEvent) => void;
      onChange?: (values: FormValue, errors: FormErrors) => void;
      validateErrors?: () => void;
      formLayout?: FormLayout,
      formItemStyle?: 'google' | 'grid'
    }

    interface FormItem {
      type: string
      item: ReactElement<any>,
      value: any,
      onChange: ChangeEventHandler<HTMLElement>
    }

    const Form: React.FunctionComponent<IProps> = props => {
      const { fields, values, onSubmit, onChange } = props;

      const onFormItemChange = (key: string, e: React.ChangeEvent<HTMLInputElement>) => {
        const newValues = { ...values, [key]: e.target.value };
        const errors = validator(fields, values);
        onChange && onChange(newValues, errors);
      };

      let labelCol: FormItemCol | undefined = {};
      let wrapperCol: FormItemCol | undefined = {};
      let rowStyle: CSSProperties | undefined = {};

      if (props.formLayout) {
        labelCol = props.formLayout.labelCol;
        wrapperCol = props.formLayout.wrapperCol;
        rowStyle = props.formLayout.style;
      }

      const renderFormItem = (itemProps: FormItem) => {
        const {item, ...restItemProps} = itemProps;
        return itemProps.type !== 'none' ?
          <Input
            className="w-form-item"
            {...restItemProps}
          /> :
          React.cloneElement(item, {
            type: item.props.type || 'text',
            value: itemProps.value,
            onChange: itemProps.onChange
          });
      };

      const renderGridStyleItems = () => fields.map(f => {
        const item = f.formItem.item as ReactElement<any>;
        const itemProps = {
          type: f.formItem.type,
          item,
          value: values[f.key] || '',
          onChange: onFormItemChange.bind(null, f.key)
        };

        if (f.formItem.type === 'none') {
          return (
            <Row key={f.key} style={rowStyle}>
              <Col className="w-form-wrapper" {...wrapperCol}>
                {renderFormItem({ ...itemProps, item })}
              </Col>
            </Row>
          );
        }
        return (
          <Row key={f.key} style={rowStyle}>
            <Col className="w-form-label" {...labelCol}>{`${f.label}：`}</Col>
            <Col className="w-form-wrapper" {...wrapperCol}>
              {renderFormItem(itemProps)}
            </Col>
          </Row>
        );
      });

      const renderGoogleStyleItems = () => fields.map(f => {
        const item = f.formItem.item as ReactElement<any>;
        const itemProps = {
          type: f.formItem.type,
          item,
          value: values[f.key] || '',
          onChange: onFormItemChange.bind(null, f.key)
        };

        if (f.formItem.type !== 'none') {
          return (
            <div key={f.key} className="w-google-style-input-wrapper w-form-item" style={rowStyle}>
              <Input
                className="w-google-style-input"
                type={f.formItem.type}
                value={values[f.key]}
                onChange={onFormItemChange.bind(null, f.key)}
                placeholder={f.label}
              />
              <label className="w-google-style-label">{f.label}</label>
            </div>
          )
        }
        return (
          <div key={f.key} className="w-google-style-input-wrapper w-form-item" style={rowStyle}>
            <Row key={f.key} style={rowStyle}>
              <Col className="w-form-wrapper" {...wrapperCol}>
                {renderFormItem({ ...itemProps, item })}
              </Col>
            </Row>
          </div>
        )
      });

      const onSubmitClick = (e:FormEvent) => {
        e.preventDefault();
        onSubmit && onSubmit(values, e)
      };

      return (
        <form className="w-form" onSubmit={onSubmitClick}>
          {props.formItemStyle !== 'google' ? renderGridStyleItems() : renderGoogleStyleItems()}
        </form>);
    };

    Form.defaultProps = {
      formLayout: {
        labelCol: {
          span: 4
        },
        wrapperCol: {
          span: 12
        }
      }
    };

    export default Form;