Skip to content

Commit 5ec9aeb

Browse files
committed
feat(inputs): Handling onChange and onBlur events on the input
1 parent 2c6765c commit 5ec9aeb

File tree

1 file changed

+50
-5
lines changed

1 file changed

+50
-5
lines changed

src/inputs/simple.js

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import React, { Component } from 'react';
22
import PropTypes from 'prop-types';
33
import { Form } from 'semantic-ui-react';
44
import isEqual from 'date-fns/is_equal';
5+
import isValid from 'date-fns/is_valid';
6+
import parse from 'date-fns/parse';
57
import { formatDate } from '../utils';
68
import Calendar from '../components/calendar';
79
import Portal from '../components/portal';
@@ -23,14 +25,15 @@ class SimpleInput extends Component {
2325
state = {
2426
isCalendarVisible: false,
2527
selectedDate: null,
28+
selectedDateFormatted: '',
2629
};
2730

2831
onDateSelected = ({ selectable, date }) => {
2932
if (!selectable) {
3033
return;
3134
}
3235

33-
const { onDateSelected } = this.props;
36+
const { format, onDateSelected } = this.props;
3437

3538
this.setState(({ selectedDate }) => {
3639
let newDate = date;
@@ -39,15 +42,52 @@ class SimpleInput extends Component {
3942
newDate = null;
4043
}
4144

42-
onDateSelected(date);
45+
onDateSelected(newDate);
4346

4447
return {
4548
isCalendarVisible: false,
4649
selectedDate: newDate,
50+
selectedDateFormatted: formatDate(newDate, format),
4751
};
4852
});
4953
};
5054

55+
handleBlur = () => {
56+
const { format } = this.props;
57+
const { selectedDateFormatted } = this.state;
58+
const newDate = parse(selectedDateFormatted);
59+
60+
if (format.length !== selectedDateFormatted.length || !isValid(newDate)) {
61+
this.setState({
62+
selectedDateFormatted: '',
63+
});
64+
}
65+
};
66+
67+
handleDateChange = (evt, { value }) => {
68+
const { format, onDateSelected } = this.props;
69+
70+
this.setState({
71+
selectedDate: null,
72+
selectedDateFormatted: value,
73+
});
74+
75+
onDateSelected(null);
76+
77+
if (value.length === format.length) {
78+
const newDate = parse(value);
79+
80+
if (isValid(newDate)) {
81+
this.setState({
82+
selectedDate: newDate,
83+
selectedDateFormatted: formatDate(newDate, format),
84+
});
85+
86+
onDateSelected(newDate);
87+
}
88+
}
89+
};
90+
5191
showCalendar = () => {
5292
this.setState(({ isCalendarVisible }) => ({
5393
isCalendarVisible: !isCalendarVisible,
@@ -56,16 +96,21 @@ class SimpleInput extends Component {
5696

5797
render() {
5898
const { date, format, inputProps } = this.props;
59-
const { isCalendarVisible, selectedDate } = this.state;
99+
const {
100+
isCalendarVisible,
101+
selectedDate,
102+
selectedDateFormatted,
103+
} = this.state;
60104

61105
return (
62106
<div id="test">
63107
<Form.Input
64108
icon="calendar"
65-
onChange={() => {}}
109+
onBlur={this.handleBlur}
110+
onChange={this.handleDateChange}
66111
onClick={this.showCalendar}
67112
placeholder={format}
68-
value={formatDate(selectedDate, format)}
113+
value={selectedDateFormatted}
69114
{...inputProps}
70115
/>
71116
{isCalendarVisible && (

0 commit comments

Comments
 (0)