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

Not triggering onchange event #31

Open
w35l3y opened this issue Jul 28, 2017 · 2 comments
Open

Not triggering onchange event #31

w35l3y opened this issue Jul 28, 2017 · 2 comments

Comments

@w35l3y
Copy link

w35l3y commented Jul 28, 2017

When choosing a date through datepicker, the onchange event of the input is not triggered.
So it can not be used with React.

@w35l3y
Copy link
Author

w35l3y commented Jul 28, 2017

Actually, it does dispatch change event!

React works with SyntheticEvents. Native events doesn't call React Event Handlers.
https://stackoverflow.com/a/39066491/157873

I really liked the simplicity of this polyfill, but now I don't know how to use it with React.

@melvey
Copy link

melvey commented Dec 13, 2017

You can work around it by triggering the react event yourself.

// If we have a date input and it has a picker attribute bind the event
componentDidUpdate() {
    if(this.dateInput && this.dateInput.hasAttribute('data-has-picker')) {
        this.dateInput.addEventListener('change', this.startDateChanged);
    } 
} 

// Cleanup before re-render
componentWillUpdate() {
    if(this.dateInput && this.dateInput.hasAttribute('data-has-picker')) {
        this.dateInput.removeEventListener('change', this.startDateChanged);
    } 
} 

// In your render function store a reference to the polyfilled input
render() {
    return (
        <input
            type="date"
            value={startDate}
            onChange={this.startDateChanged}
            ref={(input) => { this.dateInput = input; }}
         />);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants