forked from psaia/react-serial-forms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateTimeField.js
More file actions
85 lines (77 loc) · 2.26 KB
/
DateTimeField.js
File metadata and controls
85 lines (77 loc) · 2.26 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
* This is an example of a DateTime picker being extended from the React Widgets
* library.
*
* We're also using the Bootstrap Input to help with displaying a nice error
* message (if the field is required) and a label.
*
* { @see { @link http://jquense.github.io/react-widgets/docs/#/datetime-picker } }
* @example
* <DateTimeField value={mydate} name='expiration' />
*/
import React from 'react';
import { InputBase } from 'react-serial-forms';
import { Input as BootstrapInput } from 'react-bootstrap';
import { DateTimePicker } from 'react-widgets';
/**
* @constructor DateTimeField
* @extends InputBase
*/
export default class DateTimeField extends InputBase {
constructor(props) {
super(props);
}
componentWillMount() {
super.componentWillMount();
this.updateAttrs(this.props);
}
onChange(val) {
this.updateAttrs({
value: val && typeof val === 'object' ? val.getTime() : null
}, this.ogOnChange.bind(this, val));
}
render() {
let attrs = this.attrs();
let hasError = this.hasError();
let msg = hasError ? this.state.error.message : null;
// Setup an object to pass to bootstrap's Input class to use as our label
// and error message.
let labelAttrs = {};
if (msg) {
labelAttrs.help = msg;
}
if (attrs.label) {
labelAttrs.label = attrs.label;
}
// Apply classes based on state.
if (this.state.error === null) {
attrs.className += ' idle';
} else if (this.state.error === false) {
attrs.bsStyle = 'success';
attrs.className += 'success';
} else if (this.hasError()) {
attrs.className += 'error';
attrs.bsStyle = 'error';
}
// Convert the value into a Date object.
if (attrs.value) {
attrs.value = new Date(attrs.value);
}
// Don't apply these on the datepicker. Use only on a hidden field to be our
// host input.
let name = attrs.name;
let serial = attrs['data-serial'];
delete attrs.name;
delete attrs['data-serial'];
return (
<BootstrapInput {...labelAttrs} wrapperClassName='wrapper'>
<DateTimePicker {...attrs} />
<input
type='hidden'
name={name}
value={attrs.value}
data-serial={serial} />
</BootstrapInput>
);
}
}