# All files / layouts/Flow/fields flowField.js

0% Statements 0/64
0% Branches 0/8
0% Functions 0/7
0% Lines 0/19

    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

     import React from 'react';
    import PropTypes from 'prop-types';
    import styled from 'styled-components';
    import Checkbox from '../../../components/Checkbox';
    import Toggle from '../../../components/Toggle';
    import TextArea from '../../../components/TextArea';
    import RadioGroup from '../../../components/RadioGroup';
    import Label from '../../../components/Label';
    import Item from '../FlowItem';

    /**
     * Creates a wrapper which renders a redux-form Field component as a FlowItem
     */
    export default FieldComponent =>
      class FlowField extends React.Component {
        static propTypes = {
          ...Item.propTypes,
          required: PropTypes.bool,
          disabled: PropTypes.bool,
          options: PropTypes.array,
          choices: PropTypes.array,
          description: PropTypes.string,
          type: PropTypes.string,
          input: PropTypes.shape({
            checked: PropTypes.bool,
            name: PropTypes.string,
            onBlur: PropTypes.func,
            onChange: PropTypes.func,
            onDragStart: PropTypes.func,
            onDrop: PropTypes.func,
            onFocus: PropTypes.func,
            value: PropTypes.any,
          }).isRequired,
          meta: PropTypes.shape({
            error: PropTypes.string,
            active: PropTypes.bool,
            autofilled: PropTypes.bool,
            asyncValidating: PropTypes.bool,
            dirty: PropTypes.bool,
            dispatch: PropTypes.func,
            form: PropTypes.string,
            initial: PropTypes.any,
            invalid: PropTypes.bool,
            pristine: PropTypes.bool,
            submitting: PropTypes.bool,
            submitFailed: PropTypes.bool,
            touched: PropTypes.bool,
            valid: PropTypes.bool,
            visited: PropTypes.bool,
            warning: PropTypes.string,
          }),
          extraProps: PropTypes.object,
        };

        static defaultProps = {
          ...Item.defaultProps,
          meta: {},
          required: false,
          disabled: false,
          options: null,
          choices: null,
          description: null,
          type: null,
          extraProps: {},
        };

        hasFlexibleContent = FieldComponent === TextArea;
        alignment = [RadioGroup, Toggle, Checkbox].includes(FieldComponent)
          ? 'left'
          : 'stretch';

        /**
         * Renders the label with required state.
         */
        renderLabel = () => {
          const { label, required } = this.props;
          return (
            <Label className="FlowItem-label" required={required}>
              {label}
            </Label>
          );
        };

        /**
         * Renders the input component with desired passed props.
         */
        renderInput = () => {
          const {
            input,
            meta,
            required,
            disabled,
            id,
            className,
            options,
            choices,
            description,
            type,
            extraProps,
          } = this.props;

          const passedProps = {
            ...extraProps,
            ...input,
            ...meta,
            error: !!meta.error,
            required,
            disabled,
            id,
            className,
            options,
            choices,
            description,
            type,
          };

          return <FieldComponent {...passedProps} />;
        };

        render() {
          const { moreContent, meta, helpText } = this.props;

          return (
            <Item
              label={this.renderLabel()}
              helpText={helpText}
              moreContent={moreContent}
              flexibleContent={this.hasFlexibleContent}
              error={meta.error}
              alignment={this.alignment}
            >
              {this.renderInput()}
            </Item>
          );
        }
      };