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.

onChange event not firing when typing

See original GitHub issue

The onChange event only seems to fire when selecting a date from the picker but not when the user types into the input field. This makes it impossible to do any sort of validation on user input.

Current workaround is to set readOnly to true so that it forces users to only use the picker, but it would be ideal to also let them type into the box and be able to run my own validation regex in a change handler before updating the input value. Am I missing something here or is this not supported? It seems like you would either need to expose the input field’s actual onChange property or tie it into the parent component’s property (I would have assumed it already was).

React-datepicker v0.25.0 React v0.14.7

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:11
  • Comments:11

github_iconTop GitHub Comments

2reactions
alex-shamshurincommented, Mar 4, 2017

I noticed that onChange event never happens if using custom input component with null value. When we select date through DatePicker the input field first get onBlur event and then if it’s value is null and onBlur handler of input element is defined then onChange of DatePicker is not fired.

0reactions
YuriAntishevcommented, Sep 30, 2021

I met the same issues in my practice so I resolve problem with this approach - just pass all props(value, onClick, onChange) in the custom input(child) component:

const [startDate, setStartDate] = useState(new Date());
        const ExampleCustomInput = ({ value, onClick, onChange }: any) => (
            <S.StyledFloatingLabel controlId="floatingInput" label={placeholder} className="mb-3"
            >
                <Form.Control
                    placeholder={placeholder}
                    onChange={onChange}
                    onClick={onClick}
                    value={value}
                    autoComplete="off"
                />
            </S.StyledFloatingLabel>
        );
return (
                <>
                    <DatePicker
                        className="form-control"
                        dateFormat="dd/MM/yyyy"
                        selected={startDate}
                        onChange={(date: any) => setStartDate(date)}
                        customInput={<ExampleCustomInput />}
                    />
                </>
            );

Read more comments on GitHub >

github_iconTop Results From Across the Web

html <input type="text" /> onchange event not working
I am trying to do some experiment. What I want to happen is that everytime the user types in something in the textbox,...
Read more >
Text edit onchange not firing when typing
This might be happening because the OnChange property of a text input, gets triggered when you click outside of the control, somewhere else...
Read more >
The attribute for input tag"onchange" will not fire, but ...
onchange is only triggered when the control is out of focus. That means you have to click outside of the input field after...
Read more >
HTMLElement: change event - Web APIs | MDN
Unlike the input event, the change event is not necessarily fired for each ... where the user's interaction is typing rather than selection, ......
Read more >
HTML onchange Event Attribute - W3Schools
The onchange attribute fires the moment when the value of the element is changed. Tip: This event is similar to the oninput event....
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