|
| 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(type, start, stop) { |
| 8 | + let options = [{value: -1, label: type}]; |
| 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 valid(state) { |
| 16 | + return Object.keys(state).every(key => state[key] !== -1); |
| 17 | +} |
| 18 | + |
| 19 | +function DateElement({type, range, value, select, rootId}) { |
| 20 | + const id = rootId + "_" + type; |
| 21 | + return ( |
| 22 | + <SelectWidget |
| 23 | + schema={{type: "integer"}} |
| 24 | + id={id} |
| 25 | + className="form-control" |
| 26 | + options={rangeOptions(type, range[0], range[1])} |
| 27 | + value={value} |
| 28 | + onChange={(value) => select(type, value)} /> |
| 29 | + ); |
| 30 | +} |
| 31 | + |
| 32 | +class AltDateWidget extends Component { |
| 33 | + static defaultProps = { |
| 34 | + time: false |
| 35 | + }; |
| 36 | + |
| 37 | + constructor(props) { |
| 38 | + super(props); |
| 39 | + this.state = parseDateString(props.value, props.time); |
| 40 | + } |
| 41 | + |
| 42 | + componentWillReceiveProps(nextProps) { |
| 43 | + this.setState(parseDateString(nextProps.value, nextProps.time)); |
| 44 | + } |
| 45 | + |
| 46 | + shouldComponentUpdate(nextProps, nextState) { |
| 47 | + return shouldRender(this, nextProps, nextState); |
| 48 | + } |
| 49 | + |
| 50 | + onChange = (property, value) => { |
| 51 | + this.setState({[property]: value}, () => { |
| 52 | + // Only propagate to parent state if we have a complete date{time} |
| 53 | + if (valid(this.state)) { |
| 54 | + this.props.onChange(toDateString(this.state)); |
| 55 | + } |
| 56 | + }); |
| 57 | + }; |
| 58 | + |
| 59 | + setNow = (event) => { |
| 60 | + event.preventDefault(); |
| 61 | + const {time, onChange} = this.props; |
| 62 | + const nowDateObj = parseDateString(new Date().toJSON(), time); |
| 63 | + this.setState(nowDateObj, () => onChange(toDateString(this.state))); |
| 64 | + }; |
| 65 | + |
| 66 | + clear = (event) => { |
| 67 | + event.preventDefault(); |
| 68 | + const {time, onChange} = this.props; |
| 69 | + this.setState(parseDateString("", time), () => onChange(undefined)); |
| 70 | + }; |
| 71 | + |
| 72 | + get dateElementProps() { |
| 73 | + const {time} = this.props; |
| 74 | + const {year, month, day, hour, minute, second} = this.state; |
| 75 | + const data = [ |
| 76 | + {type: "year", range: [1900, 2020], value: year}, |
| 77 | + {type: "month", range: [1, 12], value: month}, |
| 78 | + {type: "day", range: [1, 31], value: day}, |
| 79 | + ]; |
| 80 | + if (time) { |
| 81 | + data.push( |
| 82 | + {type: "hour", range: [0, 23], value: hour}, |
| 83 | + {type: "minute", range: [0, 59], value: minute}, |
| 84 | + {type: "second", range: [0, 59], value: second} |
| 85 | + ); |
| 86 | + } |
| 87 | + return data; |
| 88 | + } |
| 89 | + |
| 90 | + render() { |
| 91 | + const {id} = this.props; |
| 92 | + return ( |
| 93 | + <ul className="list-inline">{ |
| 94 | + this.dateElementProps.map((props, i) => ( |
| 95 | + <li key={i}> |
| 96 | + <DateElement rootId={id} select={this.onChange} {...props} /> |
| 97 | + </li> |
| 98 | + )) |
| 99 | + } |
| 100 | + <li> |
| 101 | + <a href="#" className="btn btn-info btn-now" |
| 102 | + onClick={this.setNow}>Now</a> |
| 103 | + </li> |
| 104 | + <li> |
| 105 | + <a href="#" className="btn btn-warning btn-clear" |
| 106 | + onClick={this.clear}>Clear</a> |
| 107 | + </li> |
| 108 | + </ul> |
| 109 | + ); |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +if (process.env.NODE_ENV !== "production") { |
| 114 | + AltDateWidget.propTypes = { |
| 115 | + schema: PropTypes.object.isRequired, |
| 116 | + id: PropTypes.string.isRequired, |
| 117 | + placeholder: PropTypes.string, |
| 118 | + value: React.PropTypes.string, |
| 119 | + required: PropTypes.bool, |
| 120 | + onChange: PropTypes.func, |
| 121 | + time: PropTypes.bool, |
| 122 | + }; |
| 123 | +} |
| 124 | + |
| 125 | +export default AltDateWidget; |
0 commit comments