arqex / react-datetime

A lightweight but complete datetime picker react component.
2k stars 869 forks source link

Caret snaps to the end of input on first keyboard edit of a controlled datepicker #831

Closed aidando73 closed 2 years ago

aidando73 commented 2 years ago

I'm Submitting a ...

[ x ] Bug report
[ ] Feature request
[ ] Support request

Steps to Reproduce

For a controlled date picker:

  1. Select a date
  2. Select the text-based input
  3. Move the caret to the middle of the input.
  4. Hit backspace

Expected Results

Caret should remain in position

Actual Results

Caret moves to the back of the input

Minimal Reproduction of the Problem

This bug occurs with the following code snippet, attempted on version 3.1.1:

import React, { useState } from "react";
import "react-datetime/css/react-datetime.css";
import Datetime from "react-datetime";

export default function App() {
  const [datePickerValue, setDatePickerValue] = useState(undefined);
  return (
    <>
      <Datetime
        value={datePickerValue}
        onChange={(dateValue) => setDatePickerValue(dateValue)}
        input
      />
    </>
  );
}

Sandbox example

Other Information (e.g. stacktraces, related issues, suggestions how to fix)

- Does not occur when component is uncontrolled - Does not occur on subsequent edits. - Also occurs when adding a character - Is also repeatable. If you undo the edit and make another edit, bug occurs.
aidando73 commented 2 years ago

Cleaning up my issues - going to close this one. Let me know if you want it reopened