question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DatePicker onBlur event issue with forms

See original GitHub issue
  • I have searched the issues of this repository and believe that this is not a duplicate.

Reproduction link

Edit on CodeSandbox

Steps to reproduce

Hi,

I am trying to implement onBlur and onKeyDown functionalities for DatePicker that would use moment to format the user input to correct format for DatePicker. However, the event passed to onBlur callback of DatePicker seems to be getting the previous DatePicker value within its target value rather than the value that is inputted to the input field when the onBlur is called. The issue arises with Antd version greater than 4.9.4. With version 4.9.4 or lower, the provided implementation works fine for the onBlur functionality. This applies to TypeScript environments. When reproduced in codesandbox in JavaScript, version 4.12.2 and lower seem to be working and version 4.12.3 seems to be the first version affected.

The problem is that I would like to have also similar implementation for the onKeyDown callback, where user could confirm their input by pressing ‘Enter’ button and DatePicker would accept the date as long as moment is able to form a valid date from the input. The provided implementation works fine with Antd version >= 4.10.0 but with version greater than 4.9.4, the onBlur callback won’t receive the correct target value on the focus event on TypeScript environments. I can’t use version 4.9.4 as the onKeyDown was introduced on version 4.10.0 and on the other hand, I can’t use version greater than 4.9.4 as the onBlur won’t work with the newer versions.

DatePicker with onBlur and onKeyDown implementations:

import React from 'react';

import { DatePicker } from 'antd';
import { PickerProps } from 'antd/lib/date-picker/generatePicker';
import { Moment } from 'moment';
import moment from 'moment-timezone';

export type DatePickerProps = PickerProps<Moment> & {
  moment: typeof moment;
};

export class DatePickerAutoaccept extends React.Component<DatePickerProps> {
  private inputRef: any;
  constructor(props: DatePickerProps) {
    super(props);
    this.inputRef = React.createRef();
  }
  render() {
    const onBlur = (elem: React.FocusEvent<HTMLInputElement>) => {
      if (elem.target.value === '' && this.props.onChange) {
        this.props.onChange(null, elem.target.value);
        return;
      }
      const value = this.props.moment(elem.target.value, this.props.format as string);
      if (value && value.isValid() && this.props.onChange) {
        this.props.onChange(value, elem.target.value);
      }
    };
    const onKeyDown = (elem: React.KeyboardEvent<HTMLInputElement>) => {
      if (elem.code === 'Enter') {
        if (this.inputRef && this.inputRef.current) {
          this.inputRef.current.blur();
        }
      }
    };
    return <DatePicker {...this.props} ref={this.inputRef} onBlur={onBlur} onKeyDown={onKeyDown} />;
  }
}

How the component is being used:

<Form
  initialValues={initialValues}
  onFinish={onFinish}
  onFinishFailed={onFinishFailed}
  form={form}
  layout="vertical"
>
  <Form.Item
    name={['birthdate']}
    label="Syntymäaika"
    validateTrigger={['onSubmit', 'onBlur', 'onChange']}
    rules={[dateRequireRule]}
  >
    <DatePickerAutoaccept
      placeholder="pp.kk.vvvv"
      className="datePicker"
      size="large"
      format={dateFormat}
      defaultPickerValue={moment(new Date()).subtract(60, 'years')}
      disabledDate={disabledDate}
      moment={moment}
    />
  </Form.Item>
</Form>

What is expected?

Expecting onBlur to receive the current input value of the DatePicker input rather than the previous selected DatePicker value. Functionality has changed since version 4.9.4. Version 4.9.4 and lower are working as expected when working in TypeScript environment. In JavaScript environment (codesandbox), the first affected version seems to be 4.12.3.

Expected behaviour would be as follows. User inputs a date as 1.1.1991 or 1 1 1991 to the DatePicker input field and clicks away from the field. 01.01.1991 would be selected as DatePicker value.

What is actually happening?

DatePicker onBlur event receives older target value than is expected

Environment Info
antd 4.14.0
React 17.0.1
System macOS 11.2.2
Browser Google Chrome Version 89.0.4389.82

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
zombieJcommented, Mar 16, 2021

Do not rely on event.target.value. This is not what real value by DatePicker. You should always get correct value by onChange event.

3reactions
yimitycommented, May 12, 2021

Perhaps you guys need this solution for the input date by manually in the input box. https://codesandbox.io/s/jolly-hawking-m31ht?file=/src/App.tsx

Read more comments on GitHub >

github_iconTop Results From Across the Web

DateTimePicker: onBlur to include the current value ? #254
The problem is that redux-form updates its form values on onChange , but also on onBlur , i.e.field value is updated to ""...
Read more >
Blur event happens before datepicker fills the field
onSelect: function() { date_input.blur(); },. to the datepicker, although this is still not working for me. Any idea why?
Read more >
datePicker acting odd when passed onBlur and onFocus
The issue occurs because when the second DatePicker is opened its Calendar is also focused. This caused the first DatePicker to blur and ......
Read more >
Reactive Datepicker empties the input on form validation
Reactive Datepicker empties the input on form validation. ... The OnBlur event executes a Server Action and then sets the input.Valid and input....
Read more >
DatePicker Issue - javascript - Salesforce Stack Exchange
Scenario: Enter wrong value in number field textbox - which is field1. Check validation on blur event .Error Message popup ones. But datePicker...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found