# All files / elements/forms/DateField styled.js

97.3% Statements 36/37
83.33% Branches 15/18
100% Functions 7/7
97.3% Lines 36/37

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
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389

    8x
    8x
    1x

    7x

    8x
    8x
    1x

    7x

    8x
    8x

    8x

    8x

    8x

    8x

    8x

    8x
    8x

    8x

    8x

    8x

    9x
    9x
    1x
    1x

    8x

    8x

    8x

    9x
    9x
    9x
    9x
    9x

    9x

    9x

    8x

    8x

     import PropTypes from 'prop-types';
    import React from 'react';
    import styled, { css } from 'styled-components';
    import { borderRadius, fontWeight } from 'styled-system';
    import { removeSomeProps } from 'src/utils/componentHelpers';
    import {
      control,
      controlColor,
      controlStyles,
      defaultControlStyles,
      disabledStyle,
      focusStyles,
      fontFamilyCore,
      fontSizeCore,
      fontStyleCore,
      letterSpacingCore,
      lineHeightCore,
      plaintextPropsToTrim,
      refType,
      spaceProps,
      textAlignCore,
      textDecorationCore,
      typography,
    } from 'src/utils/styledHelpers';
    import { ControlGroup, getAssistiveText, PlainText } from 'src/elements/forms/utils';
    import { DateFieldComponent } from './component';
    import {
      DayPickerControlStyles,
      DEFAULT_DATE_FORMAT,
      DEFAULT_DATE_VALUE_FORMAT,
    } from './constants';
    import {
      defaultControlColorProps,
      defaultDayPickerProps,
      propsToTrim,
      styledWrapperPropsToTrim,
    } from './props';
    import { menuStyledHelper } from './styledHelpers';

    const controlStylesDateField = props => {
      if (props.validationError) {
        return controlStyles({ ...props, activeColor: 'brandDanger', borderColor: 'brandDanger' });
      }
      return controlStyles(props);
    };

    const disabledStyleDateField = props => {
      if (props.isDisabled) {
        return disabledStyle;
      }
      return '';
    };

    const getControlStyles = props => {
      Iif (props.calendarOnly) {
        return `
        background: transparent;
        margin-top: 1rem;
        + label {
          position: absolute;
          top: 0;
        }
      `;
      }
      return css`
        ${controlStylesDateField}
        ${disabledStyleDateField}
        &:focus-within { ${focusStyles} };
      `;
    };

    const StyledWrapper = styled('div')`
      ${controlColor}
      ${fontFamilyCore}
      ${fontSizeCore}
      ${fontWeight}
      ${letterSpacingCore}
      ${lineHeightCore}
      ${fontStyleCore}
      ${textAlignCore}
      ${textDecorationCore}
      ${DayPickerControlStyles}
      box-sizing: border-box;
      width: 100%;
      &&& {
        .DayPickerInput {
          box-sizing: border-box;
          width: 100%;
        }
      }
      ${getControlStyles}
      ${menuStyledHelper}
    `;

    StyledWrapper.propTypes = {
      bg: PropTypes.string,
      calendarOnly: PropTypes.bool,
      formStyle: PropTypes.string,
      isDisabled: PropTypes.bool,
      isFocused: PropTypes.bool,
      menuSelectedBg: PropTypes.string,
      menuSelectedColor: PropTypes.string,
      multiple: PropTypes.bool,
      validationError: PropTypes.oneOfType([
        PropTypes.bool,
        PropTypes.string,
      ]),
    };

    StyledWrapper.defaultProps = {
      bg: null,
      calendarOnly: false,
      formStyle: '',
      isDisabled: false,
      isFocused: false,
      multiple: false,
      validationError: '',
    };

    const renderDateFieldComponent = dateFieldProps => {
      const {
        bg,
        disabled,
        format,
        formStyle,
        inputProps,
        inputRef,
        labelText,
        placeholder,
        validationError,
      } = dateFieldProps;
      const controlProps = {
        ...inputProps,
        disabled,
        ref: inputRef,
      };
      const stylingProps = {
        bg,
        formStyle,
        labelText,
        validationError,
        ...removeSomeProps(dateFieldProps, styledWrapperPropsToTrim),
      };
      return (
        <StyledWrapper {...stylingProps}>
          <DateFieldComponent
            {...dateFieldProps}
            inputProps={controlProps}
            placeholder={placeholder || format}
          />
        </StyledWrapper>
      );
    };

    const renderValueOrComponent = propsFromComponent => {
      const {
        controlId,
        disabled,
        name,
        plainText,
      } = propsFromComponent;
      if (plainText) {
        const plainTextProps = removeSomeProps(propsFromComponent, plaintextPropsToTrim);
        return <PlainText {...plainTextProps} />;
      }
      const childProps = {
        id: controlId || name,
        isDisabled: disabled,
        ...removeSomeProps(propsFromComponent, propsToTrim),
      };
      return renderDateFieldComponent(childProps);
    };
    const DateField = props => {
      const {
        activeColor,
        assistiveText,
        assistiveTextProps,
        bg,
        controlColorProps,
        controlGroupProps,
        controlId,
        controlLabelProps,
        dayPickerProps,
        disabled,
        isRequired,
        labelText,
        locale,
        name,
        validationError,
      } = props;
      const assistiveProps = { assistiveText, isRequired };
      const newLabel = isRequired && labelText ? `${labelText}*` : labelText;
      const newControlColorProps = { ...defaultControlColorProps, ...controlColorProps };
      const newDayPickerProps = {
        ...defaultDayPickerProps,
        weekdaysShort: locale !== 'en' ? '' : defaultDayPickerProps.weekdaysShort,
        ...dayPickerProps,
      };
      const componentProps = {
        ...props,
        controlColorProps: newControlColorProps,
        dayPickerProps: newDayPickerProps,
      };
      return (
        <ControlGroup
          activeColor={activeColor}
          assistiveText={getAssistiveText(assistiveProps)}
          assistiveTextProps={assistiveTextProps}
          bg={bg}
          controlId={controlId}
          controlLabelProps={controlLabelProps}
          disabled={disabled}
          labelText={newLabel}
          name={name}
          validationError={validationError}
          {...controlGroupProps}
        >
          {renderValueOrComponent({ ...componentProps })}
        </ControlGroup>
      );
    };

    DateField.propTypes = {
      /** Defines the color for the label and border color when the focus is in the control. */
      activeColor: PropTypes.string,
      /** Provides helper text for the control.  When provided with no `assistiveText`, `isRequired` will add a default '*Required` helper text.  When provided with `validationError`, `assistiveText`'s value will not be displayed on the UI. */
      assistiveText: PropTypes.oneOfType([
        PropTypes.object,
        PropTypes.string,
      ]),
      /** Allows for custom props to be passed down to the `AssistiveText` component. */
      assistiveTextProps: PropTypes.object,
      /** Binds the DayPicker with an input field, displaying the calendar in an overlay by default. Selecting calendarOnly
       * shows the full calendar inline.
       * @see [react-day-picker/Input Field](https://react-day-picker.js.org/examples/input) */
      calendarOnly: PropTypes.bool,
      /** Define the colors for the control.  Uses grape-ui's color variables.  Important note, if a value is not provided explicitly for a specific color, it will fallback to the default color. */
      controlColorProps: PropTypes.object,
      /** Allows for custom props to be passed down to the `ControlGroup` component. */
      controlGroupProps: PropTypes.object,
      /** Define the height of the component.  Useful for definining the menu placement offset. */
      controlHeight: PropTypes.string,
      /** Will pass its value to the control's `id` as well as the label's `htmlFor`.
       * @deprecated Do not use! Use `name` instead!
       */
      controlId: PropTypes.string,
      /** Allows for custom props to be passed down to the `ControlLabel` component. */
      controlLabelProps: PropTypes.object,
      /** The DayPicker props to customize the calendar rendered in the overlay.
       * @see See [React-Day-Picker DayPicker API](https://react-day-picker.js.org/api/DayPicker/) for more */
      dayPickerProps: PropTypes.object,
      /** Basic HTML attribute, needed for styling. */
      disabled: PropTypes.bool,
      /** Date format that is acceptable for user to enter, as well as the format of the value that is returned upon selection. Defaults to M/D/YYYY.
       * @see See [Moment.js | Docs > Parsing > String + Format](https://momentjs.com/docs/#/parsing/string-format/) for more format options */
      format: PropTypes.string,
      /**
       * Use 'filled' or 'outlined'
       * @see See [Material Design](https://material.io/components/text-fields/#usage) for options */
      formStyle: PropTypes.string,
      /** Additional props to add to the input component. The value key is ignored: use the value prop instead.
       * @see See [React-Day-Picker DayPicker API](https://react-day-picker.js.org/api/DayPickerInput#inputProps) for more */
      inputProps: PropTypes.object,
      /** Allows for a ref to be defined to the DOM input */
      inputRef: refType,
      /** This will add an asterisk (*) to the `labelText` and provided `assistiveText` if none is provided. */
      isRequired: PropTypes.bool,
      /** The string value displayed on top of the control in the `ControlLabel` component. */
      labelText: PropTypes.string,
      /** Locale defaults to English (en) */
      locale: PropTypes.string,
      /** Define where to align the dropdown menu.
       * Please use either `left` or `right`.
       * Uses styled-system's responsive styling to specify via specic breakpoints. */
      menuAlignment: PropTypes.oneOfType([
        PropTypes.array,
        PropTypes.string,
      ]),
      /** Define the flex-direction of the menu.  Uses styled-system's responsive styling.  Should only be used for menus that have more than one month. */
      menuDirection: PropTypes.oneOfType([
        PropTypes.array,
        PropTypes.string,
      ]),
      /** Override `menuDirection`'s specific breakpoints for when to use column/row view.
       * Please use `column` or `row`.
       * Enter a value for `maxWidth` for `column`.
       * Enter a value for `minWidth` for `row`.
       * Should only be used for menus that have more than one month. */
      menuDirectionViewportBreakpoint: PropTypes.object,
      /** `'01'`, `'02'`, `'03'`, `'04'`, `'05'`, `'06'`.  Use these to define the boxShadow and zIndex styles. */
      menuElevation: PropTypes.string,
      /** Defines the bottom CSS property of the menu's overlay */
      menuOverlayBottom: PropTypes.oneOfType([
        PropTypes.array,
        PropTypes.number,
        PropTypes.string,
      ]),
      /** Defines the left CSS property of the menu's overlay */
      menuOverlayLeft: PropTypes.oneOfType([
        PropTypes.array,
        PropTypes.number,
        PropTypes.string,
      ]),
      /** Defines the right CSS property of the menu's overlay */
      menuOverlayRight: PropTypes.oneOfType([
        PropTypes.array,
        PropTypes.number,
        PropTypes.string,
      ]),
      /** Defines the top CSS property of the menu's overlay */
      menuOverlayTop: PropTypes.oneOfType([
        PropTypes.array,
        PropTypes.number,
        PropTypes.string,
      ]),
      /** Defines the placement of the dropdown menu */
      menuPlacement: PropTypes.oneOfType([
        PropTypes.array,
        PropTypes.string,
      ]),
      /** Should be used on each form control.  When no `id` or `controlId` are provided, `name` will populate both fields. */
      name: PropTypes.string,
      /** Used to render a dropdown control as a `PlainText` element. */
      plainText: PropTypes.bool,
      /** Error text that will appear below the control when validation fires. */
      validationError: PropTypes.oneOfType([
        PropTypes.bool,
        PropTypes.string,
      ]),
      /** The value of the input field.  It can either be a string or a Date object.
       * @see See [React-Day-Picker API](https://react-day-picker.js.org/api/DayPickerInput#value) for more about the prop.
       * @see See [MDN web docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) for more about Date objects.
       */
      value: PropTypes.oneOfType([
        PropTypes.string,
        PropTypes.instanceOf(Date),
      ]),
      /** Define the format for the value sent to the database.  Default value is "YYYY-MM-DD".
       * When user call OnChange, onChange function is expected to be called with 4 parameters: selectedDay, modifiers, input, formattedDay
       * selectedDay: the date object of the selected date
       * modifiers: matching day for the given modifiers
       * input: the inner input which you get the the input.value
       * formattedDay: string format output that using the valueFormat
       */
      valueFormat: PropTypes.any,
      ...borderRadius.propTypes,
      ...spaceProps.propTypes,
      ...control.propTypes,
      ...typography.propTypes,
    };

    DateField.defaultProps = {
      activeColor: defaultControlStyles.activeColor,
      assistiveText: '',
      assistiveTextProps: {},
      calendarOnly: false,
      controlColorProps: defaultControlColorProps,
      controlGroupProps: {},
      controlHeight: '58px',
      controlId: '',
      controlLabelProps: {},
      dayPickerProps: defaultDayPickerProps,
      disabled: false,
      format: DEFAULT_DATE_FORMAT,
      formStyle: '',
      inputProps: {},
      inputRef: () => {},
      isRequired: false,
      labelText: '',
      locale: 'en',
      menuAlignment: 'left',
      menuDirection: 'column',
      menuDirectionViewportBreakpoint: {},
      menuElevation: '01',
      menuOverlayBottom: '',
      menuOverlayLeft: '',
      menuOverlayRight: '',
      menuOverlayTop: '',
      menuPlacement: 'bottom',
      name: '',
      plainText: false,
      validationError: '',
      value: undefined,
      valueFormat: DEFAULT_DATE_VALUE_FORMAT,
    };

    /** @component */
    export { DateField };