# All files / layouts/Flow FlowItem.js

0% Statements 0/79
0% Branches 0/28
0% Functions 0/8
0% Lines 0/36

    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
    174
    175

     import React from 'react';
    import PropTypes from 'prop-types';
    import _ from 'lodash';
    import { withProps } from 'recompose';
    import DefaultLabel from 'components/Label';
    import DefaultHelpText from 'components/HelpText';
    import FlowItemContainer from './styles/FlowItemContainer';
    import FlowItemContent from './styles/FlowItemContent';
    import FlowItemFlexibleContent from './styles/FlowItemFlexibleContent';
    import FlowItemMoreContent from './styles/FlowItemMoreContent';

    /**
     * @deprecated
     * See /layouts/Fields for new layout mechanisms for form fields
     *
     * Flow.Item is an individual element in the Flow system. It annotates the provided
     * content with a label, help text, and ensures correct layout with sibling elements when used properly inside
     * a Flow.Row. Flow.Item includes some advanced props like `moreContent` and `flexibleContent` to further
     * customize the way it looks and behaves.
     *
     * @class FlowItem
     * @extends {React.Component}
     */
    class FlowItem extends React.Component {
      static propTypes = {
        /**
         * A label for this item. May be a Label instance or a string.
         */
        label: PropTypes.node,
        /**
         * Help text for this item. May be a HelpText instance or a string.
         */
        helpText: PropTypes.node,
        /**
         * Contents of this item. This goes between the label and the help text.
         */
        children: PropTypes.node,
        /**
         * More contents for this item. This goes below the help text and extends the size
         * of the component.
         */
        moreContent: PropTypes.node,
        /**
         * Allows the main content to grow larger than the default fixed size.
         */
        flexibleContent: PropTypes.bool,
        /**
         * Aligns the various elements within the item to a particular side.
         */
        alignment: PropTypes.oneOf(['stretch', 'left', 'right']),
        /**
         * Whether this item should render a visual error state.
         */
        error: PropTypes.bool,
        /**
         * An id to add to the item container element.
         */
        id: PropTypes.string,
        /**
         * A class name to add to the item container element.
         */
        className: PropTypes.string,
        /**
         * A component for rendering the containing element
         */
        Container: PropTypes.func,
        /**
         * A component for rendering the content area
         */
        Content: PropTypes.func,
        /**
         * A component for rendering content that is flexible in size
         */
        FlexibleContent: PropTypes.func,
        /**
         * A component for rendering the 'more content' area
         */
        MoreContent: PropTypes.func,
        /**
         * Allows overriding the default Label component
         */
        Label: PropTypes.func,
        /**
         * Allows overriding the default HelpText component
         */
        HelpText: PropTypes.func,
      };

      static defaultProps = {
        label: null,
        helpText: null,
        children: null,
        moreContent: null,
        flexibleContent: false,
        error: false,
        id: null,
        className: null,
        alignment: 'stretch',
        Container: FlowItemContainer,
        Content: FlowItemContent,
        FlexibleContent: FlowItemFlexibleContent,
        MoreContent: FlowItemMoreContent,
        Label: DefaultLabel,
        HelpText: DefaultHelpText,
      };

      renderLabel = () => {
        const { label, Label } = this.props;

        if (!!label && _.isString(label)) {
          return <Label className="FlowItem-label">{label}</Label>;
        }

        // always render some label, even if blank, to keep layout consistent
        return label || <Label className="FlowItem-label" />;
      };

      renderChildren = () => {
        const { flexibleContent, FlexibleContent, Content, children } = this.props;

        if (flexibleContent) {
          return <FlexibleContent>{children}</FlexibleContent>;
        }
        return <Content>{children}</Content>;
      };

      renderHelpText = () => {
        const { helpText, error, HelpText } = this.props;

        if (_.isString(helpText)) {
          return (
            <HelpText className="FlowItem-helpText" error={error}>
              {helpText}
            </HelpText>
          );
        } else if (helpText && helpText.props) {
          return React.cloneElement(helpText, { error: error });
        } else if (!helpText && error) {
          return (
            <HelpText className="FlowItem-helpText" error>
              {error}
            </HelpText>
          );
        }

        return helpText || null;
      };

      renderMoreContent = () => {
        const { moreContent, MoreContent } = this.props;
        if (moreContent) {
          return <MoreContent>{moreContent}</MoreContent>;
        }
        return null;
      };

      render() {
        const { id, className, Container } = this.props;
        return (
          <Container alignment={this.props.alignment} id={id} className={className}>
            {this.renderLabel()}
            {this.renderChildren()}
            {this.renderHelpText()}
            {this.renderMoreContent()}
          </Container>
        );
      }
    }

    FlowItem.Small = withProps({
      Container: FlowItemContainer.Small,
    })(FlowItem);

    export default FlowItem;