|
1 | | -import React, { PropTypes } from "react"; |
2 | | - |
3 | | - |
4 | | -function DateTimeWidget({ |
5 | | - schema, |
6 | | - id, |
7 | | - placeholder, |
8 | | - value, |
9 | | - defaultValue, |
10 | | - required, |
11 | | - onChange |
12 | | -}) { |
| 1 | +import React, { Component, PropTypes } from "react"; |
| 2 | + |
| 3 | +import { shouldRender, parseDateString, toDateString, pad } from "../../utils"; |
| 4 | +import SelectWidget from "../widgets/SelectWidget"; |
| 5 | + |
| 6 | + |
| 7 | +function rangeOptions(start, stop) { |
| 8 | + let options = []; |
| 9 | + for (let i=start; i<= stop; i++) { |
| 10 | + options.push({value: i, label: pad(i, 2)}); |
| 11 | + } |
| 12 | + return options; |
| 13 | +} |
| 14 | + |
| 15 | +function DateElement({type, range, value, select, rootId}) { |
| 16 | + const id = rootId + "_" + type; |
13 | 17 | return ( |
14 | | - <input type="datetime-local" |
15 | | - id={id} |
16 | | - className="form-control" |
17 | | - value={value} |
18 | | - defaultValue={defaultValue} |
19 | | - placeholder={placeholder} |
20 | | - required={required} |
21 | | - onChange={(event) => onChange(event.target.value)} /> |
| 18 | + <li> |
| 19 | + <SelectWidget |
| 20 | + schema={{type: "integer"}} |
| 21 | + id={id} |
| 22 | + className="form-control" |
| 23 | + options={rangeOptions(range[0], range[1])} |
| 24 | + value={value} |
| 25 | + onChange={select} /> |
| 26 | + <p className="text-center help-block"> |
| 27 | + <label htmlFor={id}><small>{type}</small></label> |
| 28 | + </p> |
| 29 | + </li> |
22 | 30 | ); |
23 | 31 | } |
24 | 32 |
|
| 33 | +class DateTimeWidget extends Component { |
| 34 | + constructor(props) { |
| 35 | + super(props); |
| 36 | + this.state = parseDateString(props.value || props.defaultValue); |
| 37 | + } |
| 38 | + |
| 39 | + componentWillReceiveProps(nextProps) { |
| 40 | + this.setState(parseDateString(nextProps.value || nextProps.defaultValue)); |
| 41 | + } |
| 42 | + |
| 43 | + shouldComponentUpdate(nextProps, nextState) { |
| 44 | + return shouldRender(this, nextProps, nextState); |
| 45 | + } |
| 46 | + |
| 47 | + onChange = (property, value) => { |
| 48 | + this.setState({[property]: value}, () => { |
| 49 | + this.props.onChange(toDateString(this.state)); |
| 50 | + }); |
| 51 | + }; |
| 52 | + |
| 53 | + onYearChange = (value) => this.onChange("year", value); |
| 54 | + onMonthChange = (value) => this.onChange("month", value); |
| 55 | + onDayChange = (value) => this.onChange("day", value); |
| 56 | + onHourChange = (value) => this.onChange("hour", value); |
| 57 | + onMinuteChange = (value) => this.onChange("minute", value); |
| 58 | + onSecondChange = (value) => this.onChange("second", value); |
| 59 | + |
| 60 | + render() { |
| 61 | + const {id} = this.props; |
| 62 | + const {year, month, day, hour, minute, second} = this.state; |
| 63 | + return ( |
| 64 | + <ul className="list-inline"> |
| 65 | + <DateElement type="year" rootId={id} range={[1900, 2020]} |
| 66 | + value={year} select={this.onYearChange} /> |
| 67 | + <DateElement type="month" rootId={id} range={[1, 12]} |
| 68 | + value={month} select={this.onMonthChange} /> |
| 69 | + <DateElement type="day" rootId={id} range={[1, 31]} |
| 70 | + value={day} select={this.onDayChange} /> |
| 71 | + <DateElement type="hour" rootId={id} range={[0, 23]} |
| 72 | + value={hour} select={this.onHourChange} /> |
| 73 | + <DateElement type="minute" rootId={id} range={[0, 59]} |
| 74 | + value={minute} select={this.onMinuteChange} /> |
| 75 | + <DateElement type="second" rootId={id} range={[0, 59]} |
| 76 | + value={second} select={this.onSecondChange} /> |
| 77 | + </ul> |
| 78 | + ); |
| 79 | + } |
| 80 | +} |
| 81 | + |
25 | 82 | if (process.env.NODE_ENV !== "production") { |
26 | 83 | DateTimeWidget.propTypes = { |
27 | 84 | schema: PropTypes.object.isRequired, |
|
0 commit comments