-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathDateInput.tsx
More file actions
49 lines (42 loc) · 1.34 KB
/
DateInput.tsx
File metadata and controls
49 lines (42 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import * as React from "react";
import ReactDatePicker from "react-datepicker";
import * as moment from "moment";
import { FieldError, InputChangeHandler } from "../../types";
import FieldFeedbackPanel from "./FieldFeedbackPanel";
const DateInput = ({
object,
fieldError,
name,
label,
onChange,
onBlur
}: {
object: any;
fieldError: FieldError | null;
name: string;
label: string;
onChange: InputChangeHandler;
onBlur: (name: string) => void;
}) => {
const handleOnChange = (value: any) => {
const dateString = value ? value.format("YYYY-MM-DD") : null;
onChange(name, dateString);
};
const selectedValue = object[name] ? moment(object[name], "YYYY-MM-DD") : null;
const valid = !fieldError && selectedValue != null;
const cssGroup = `form-group ${fieldError ? "has-error" : ""}`;
return (
<div className={cssGroup}>
<label className="col-sm-2 control-label">
{label}
</label>
<div className="col-sm-10">
<ReactDatePicker selected={selectedValue} onChange={handleOnChange} className="form-control" dateFormat="YYYY-MM-DD"
onBlur={e => onBlur(e.currentTarget.name)}/>
<span className="glyphicon glyphicon-ok form-control-feedback" aria-hidden="true" />
<FieldFeedbackPanel valid={valid} fieldError={fieldError} />
</div>
</div>
);
};
export default DateInput;