Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React Carbon - Controlled DatePicker type='range', first input shows both values #4803

Closed
1 task done
agmcleod opened this issue Dec 2, 2019 · 2 comments · Fixed by #4814
Closed
1 task done

React Carbon - Controlled DatePicker type='range', first input shows both values #4803

agmcleod opened this issue Dec 2, 2019 · 2 comments · Fixed by #4814

Comments

@agmcleod
Copy link

agmcleod commented Dec 2, 2019

What package(s) are you using?

  • carbon-components-react

Summary

I'm working on setting up a date range for a table filter. I seem to get the input data okay, but when I update the form state to have the array of the two dates, and that passes back to the DatePicker component, the first DatePickerInput shows both values.

Screen Recording 2019-12-02 at 11 04 13 AM 2019-12-02 11_05_15

Relevant information

Our component for integrating formik with the date picker

export const FormikDatePicker = connectFormik(
  ({
    formik,
    labelText,
    tooltipKey,
    field,
    datePickerType,
    rangeLabels,
    ...otherInputProps
  }) => {
    const isRange = datePickerType === 'range'
    const handleChange = (selectedDates, dateStr) => {
      const { setFieldTouched, setFieldValue } = formik
      setFieldTouched(field.name, true)

      if (isRange) {
        setFieldValue(field.name, selectedDates)
      } else {
        const date = moment(`${dateStr}T00:00:00`)
        setFieldValue(field.name, date.toISOString())
      }
    }

    let label = ''
    if (isRange) {
      label = rangeLabels[0]
    }

    return (
      <>
        {datePickerType === 'single' ? (
          <FieldLabel
            labelText={labelText}
            name={field.name}
            tooltipKey={tooltipKey}
          />
        ) : null}
        <DatePicker
          onChange={(selectedDates, dateStr) => {
            handleChange(selectedDates, dateStr)
          }}
          value={field.value}
          id={`datePicker-${field.name}`}
          datePickerType={datePickerType}
          dateFormat='Y-m-d'
        >
          <DatePickerInput
            id={field.name}
            placeholder='yyyy-mm-dd'
            pattern={DATE_PATTERN}
            labelText={label}
            {...otherInputProps}
          />
          {isRange ? (
            <DatePickerInput
              id={`${field.name}-end`}
              placeholder='yyyy-mm-dd'
              pattern={DATE_PATTERN}
              labelText={rangeLabels[1]}
              {...otherInputProps}
            />
          ) : null}
        </DatePicker>
      </>
    )
  }
)
@asudoh
Copy link
Contributor

asudoh commented Dec 2, 2019

Hi 👋 we used to have a similar issue, but it's been fixed. That said, what version of Carbon are you using? If you think you are using latest Carbon, would you want to create a reduced case based on https://codesandbox.io/s/github/carbon-design-system/carbon/tree/master/packages/react/examples/codesandbox? Thanks!

@agmcleod
Copy link
Author

agmcleod commented Dec 3, 2019

@asudoh sure, here you go: https://codesandbox.io/s/codesandbox-oliyt. Using the latest on my own code base as well: 7.7.4

asudoh added a commit to asudoh/carbon-components that referenced this issue Dec 4, 2019
asudoh added a commit to asudoh/carbon-components that referenced this issue Dec 4, 2019
This change adds a logic to re-format dates in `<input>` when
Flatpickr's `setDate()` API is called. Flatpickr's range plugin has a
similar logic to do that, but runs only if the second argument is
`true`.

Fixes carbon-design-system#4803.
asudoh added a commit to asudoh/carbon-components that referenced this issue Dec 4, 2019
This change adds a logic to re-format dates in `<input>` when
Flatpickr's `setDate()` API is called. Flatpickr's range plugin has a
similar logic to do that, but runs only if the second argument is
`true`.

Fixes carbon-design-system#4803.
@dakahn dakahn added severity: 2 https://ibm.biz/carbon-severity priority: high labels Dec 6, 2019
asudoh added a commit that referenced this issue Jan 10, 2020
This change adds a logic to re-format dates in `<input>` when
Flatpickr's `setDate()` API is called. Flatpickr's range plugin has a
similar logic to do that, but runs only if the second argument is
`true`.

Fixes #4803.
joshblack pushed a commit to joshblack/carbon that referenced this issue Jan 13, 2020
…esign-system#4814)

This change adds a logic to re-format dates in `<input>` when
Flatpickr's `setDate()` API is called. Flatpickr's range plugin has a
similar logic to do that, but runs only if the second argument is
`true`.

Fixes carbon-design-system#4803.
joshblack pushed a commit to joshblack/carbon that referenced this issue Jan 14, 2020
…esign-system#4814)

This change adds a logic to re-format dates in `<input>` when
Flatpickr's `setDate()` API is called. Flatpickr's range plugin has a
similar logic to do that, but runs only if the second argument is
`true`.

Fixes carbon-design-system#4803.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants