# All files / src/DateSelect DateSelect.tsx

73.17% Statements 60/82
53.65% Branches 22/41
58.33% Functions 7/12
70.42% Lines 50/71

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
    1978x
    8x

    8x
    8x
    8x

    8x
    8x

    8x

    4x
    4x

    8x

    8x

    8x

    8x

    31x
    31x
    31x
    31x
    31x
    31x
    31x
    31x
    31x

    31x

    31x

    31x
    31x

    31x

    31x

    31x

    31x

    31x

    31x
    16x
    16x

    31x

    31x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    4x
    4x
    4x
    4x

    4x

     import React from 'react'
    import { Calendar as CalendarBase } from '../Calendar/Calendar'
    import { CalendarProps } from '../Calendar/Calendar.types'
    import { Card } from '../Card/Card'
    import { DateInput, isValidYearRange } from '../DateInput/DateInput'
    import {
      OverlayTrigger,
      useOverlayTriggerContext,
    } from '../OverlayTrigger/OverlayTrigger'
    import { isEventSource } from '../_hooks/ClickOutside'
    import { useDateTime } from '../_hooks/DateTime'
    import { DateSelectProps } from './DateSelect.types'

    const enableLogs = process.env.TZ_LOG?.toLowerCase() === 'true'

    function consoleMessageChange({ date }: any) {
      Iif (enableLogs) {
        console.log(
          `core-react: DateInput onChange, relative start of day in destination time zone.\n`,
          date
        )
      }
    }

    const showKeys: string[] = []

    const hideKeys = {
      target: ['Esc', 'Escape', 'Enter'],
      overlay: ['Esc', 'Escape'],
    }

    function lifecycleDefault() {
      return true
    }

    function noop() {}

    const Calendar = React.forwardRef<HTMLDivElement, CalendarProps>(
      function Calendar({ onSelect, ...props }, ref) {
        const { hide } = useOverlayTriggerContext()

        return (
          <Card ref={ref} shadowStrength={2}>
            <CalendarBase
              {...props}
              onSelect={(date) => {
                onSelect?.(date)
                hide(true)
              }}
            />
          </Card>
        )
      }
    )

    /**

     We use date selects to allow users to enter a date into a form by either
     selecting it from a calendar or typing it into a date field.

     @since 10.19.0

     @see [Storybook](https://procore.github.io/core/core-react/latest/?path=/story/demos-dateselect--demo)

     @see [Design Guidelines](https://design.procore.com/date-select)

     */
    export const DateSelect = React.forwardRef<HTMLDivElement, DateSelectProps>(
      function DateSelect(
        {
          beforeHide = lifecycleDefault,
          beforeShow = lifecycleDefault,
          afterHide = noop,
          afterShow = noop,
          disabledDate,
          onChange = noop,
          onClear = noop,
          value,
          ...props
        },
        ref
      ) {
        const dateTime = useDateTime()

        const clearRef = React.useRef<HTMLButtonElement>(null)

        const dateInputRef = ref as React.RefObject<HTMLDivElement>
        React.useRef<HTMLDivElement>(null)

        const monthRef = React.useRef<HTMLDivElement>(null)

        const yearRef = React.useRef<HTMLDivElement>(null)

        const segmentRefs = {
          segmentThree: React.useRef<HTMLDivElement>(null),
        }

        const [selected, setSelected] = React.useState(value)

        const [displayed, setDisplayed] = React.useState(
          value || dateTime.newDate()
        )

        React.useEffect(() => {
          setSelected(value)
          setDisplayed(value || dateTime.newDate())
        }, [value])

        const overlay = (
          <Calendar
            data-qa="core-date-select-calendar"
            disabledDate={disabledDate}
            displayDate={displayed}
            monthRef={monthRef}
            onNavigate={setDisplayed}
            onSelect={(date) => {
              segmentRefs.segmentThree.current &&
                segmentRefs.segmentThree.current.focus()

              setDisplayed(date)
              setSelected(date)
              onChange(date)
            }}
            selectedStart={selected}
            selectedEnd={selected}
            yearRef={yearRef}
          />
        )

        return (
          <OverlayTrigger
            afterHide={afterHide}
            afterShow={afterShow}
            beforeHide={(e) => {
              Iif (e instanceof KeyboardEvent) {
                segmentRefs.segmentThree.current &&
                  segmentRefs.segmentThree.current.focus()

                return beforeHide(e)
              }

              Iif (isEventSource(dateInputRef, e) || isEventSource(clearRef, e)) {
                return false
              }
              return beforeHide(e)
            }}
            beforeShow={(e) => {
              Iif (isEventSource(clearRef, e)) {
                return false
              }
              return beforeShow(e)
            }}
            clickOutsideIgnoreRefs={[monthRef, yearRef]}
            hideKeys={hideKeys}
            overlay={overlay}
            placement="bottom-left"
            ref={dateInputRef}
            showKeys={showKeys}
          >
            <DateInput
              {...props}
              clearRef={clearRef}
              segmentRefs={segmentRefs}
              onChangeSegment={(type, value) => {
                Iif (value === -1) {
                  return
                }

                Iif (type === 'month') {
                  setDisplayed(dateTime.newDate(displayed.setMonth(value - 1)))
                } else Iif (type === 'year') {
                  // isValidYearRange prevents calendar from changing, the display date
                  Iif (isValidYearRange(value)) {
                    setDisplayed(dateTime.newDate(displayed.setFullYear(value)))
                  }
                }
              }}
              onChange={(date: Date | null) => {
                consoleMessageChange({ date })
                if (date) {
                  setSelected(date)
                  setDisplayed(date)
                } else E{
                  setDisplayed(dateTime.newDate())
                  setSelected(undefined)
                }

                onChange(date)
              }}
              onClear={onClear}
              value={selected}
            />
          </OverlayTrigger>
        )
      }
    )