-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #150: Using native date widgets by default.
- Loading branch information
Showing
10 changed files
with
279 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
module.exports = { | ||
schema: { | ||
title: "Date and time widgets", | ||
type: "object", | ||
properties: { | ||
native: { | ||
title: "Native", | ||
description: "Note that a few browsers support these.", | ||
type: "object", | ||
properties: { | ||
"datetime": { | ||
type: "string", | ||
format: "date-time" | ||
}, | ||
"date": { | ||
type: "string", | ||
format: "date-time" | ||
} | ||
} | ||
}, | ||
alternative: { | ||
title: "Alternative", | ||
description: "These work on every platform.", | ||
type: "object", | ||
properties: { | ||
"alt-datetime": { | ||
type: "string", | ||
format: "date-time" | ||
}, | ||
"alt-date": { | ||
type: "string", | ||
format: "date-time" | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
uiSchema: { | ||
native: { | ||
date: { | ||
"ui:widget": "date" | ||
} | ||
}, | ||
alternative: { | ||
"alt-datetime": { | ||
"ui:widget": "alt-datetime" | ||
}, | ||
"alt-date": { | ||
"ui:widget": "alt-date" | ||
} | ||
} | ||
}, | ||
formData: {} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import React, { PropTypes } from "react"; | ||
|
||
import AltDateWidget from "./AltDateWidget"; | ||
|
||
|
||
function AltDateTimeWidget(props) { | ||
return <AltDateWidget time {...props} />; | ||
} | ||
|
||
if (process.env.NODE_ENV !== "production") { | ||
AltDateTimeWidget.propTypes = { | ||
schema: PropTypes.object.isRequired, | ||
id: PropTypes.string.isRequired, | ||
placeholder: PropTypes.string, | ||
value: React.PropTypes.string, | ||
required: PropTypes.bool, | ||
onChange: PropTypes.func, | ||
}; | ||
} | ||
|
||
export default AltDateTimeWidget; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import React, { Component, PropTypes } from "react"; | ||
|
||
import { shouldRender, parseDateString, toDateString, pad } from "../../utils"; | ||
import SelectWidget from "../widgets/SelectWidget"; | ||
|
||
|
||
function rangeOptions(type, start, stop) { | ||
let options = [{value: -1, label: type}]; | ||
for (let i=start; i<= stop; i++) { | ||
options.push({value: i, label: pad(i, 2)}); | ||
} | ||
return options; | ||
} | ||
|
||
function valid(state) { | ||
return Object.keys(state).every(key => state[key] !== -1); | ||
} | ||
|
||
function DateElement({type, range, value, select, rootId}) { | ||
const id = rootId + "_" + type; | ||
return ( | ||
<SelectWidget | ||
schema={{type: "integer"}} | ||
id={id} | ||
className="form-control" | ||
options={rangeOptions(type, range[0], range[1])} | ||
value={value} | ||
onChange={(value) => select(type, value)} /> | ||
); | ||
} | ||
|
||
class AltDateWidget extends Component { | ||
static defaultProps = { | ||
time: false | ||
}; | ||
|
||
constructor(props) { | ||
super(props); | ||
this.state = parseDateString(props.value, props.time); | ||
} | ||
|
||
componentWillReceiveProps(nextProps) { | ||
this.setState(parseDateString(nextProps.value, nextProps.time)); | ||
} | ||
|
||
shouldComponentUpdate(nextProps, nextState) { | ||
return shouldRender(this, nextProps, nextState); | ||
} | ||
|
||
onChange = (property, value) => { | ||
this.setState({[property]: value}, () => { | ||
// Only propagate to parent state if we have a complete date{time} | ||
if (valid(this.state)) { | ||
this.props.onChange(toDateString(this.state)); | ||
} | ||
}); | ||
}; | ||
|
||
setNow = (event) => { | ||
event.preventDefault(); | ||
const {time, onChange} = this.props; | ||
const nowDateObj = parseDateString(new Date().toJSON(), time); | ||
this.setState(nowDateObj, () => onChange(toDateString(this.state))); | ||
}; | ||
|
||
clear = (event) => { | ||
event.preventDefault(); | ||
const {time, onChange} = this.props; | ||
this.setState(parseDateString("", time), () => onChange(undefined)); | ||
}; | ||
|
||
get dateElementProps() { | ||
const {time} = this.props; | ||
const {year, month, day, hour, minute, second} = this.state; | ||
const data = [ | ||
{type: "year", range: [1900, 2020], value: year}, | ||
{type: "month", range: [1, 12], value: month}, | ||
{type: "day", range: [1, 31], value: day}, | ||
]; | ||
if (time) { | ||
data.push( | ||
{type: "hour", range: [0, 23], value: hour}, | ||
{type: "minute", range: [0, 59], value: minute}, | ||
{type: "second", range: [0, 59], value: second} | ||
); | ||
} | ||
return data; | ||
} | ||
|
||
render() { | ||
const {id} = this.props; | ||
return ( | ||
<ul className="list-inline">{ | ||
this.dateElementProps.map((props, i) => ( | ||
<li key={i}> | ||
<DateElement rootId={id} select={this.onChange} {...props} /> | ||
</li> | ||
)) | ||
} | ||
<li> | ||
<a href="#" className="btn btn-info btn-now" | ||
onClick={this.setNow}>Now</a> | ||
</li> | ||
<li> | ||
<a href="#" className="btn btn-warning btn-clear" | ||
onClick={this.clear}>Clear</a> | ||
</li> | ||
</ul> | ||
); | ||
} | ||
} | ||
|
||
if (process.env.NODE_ENV !== "production") { | ||
AltDateWidget.propTypes = { | ||
schema: PropTypes.object.isRequired, | ||
id: PropTypes.string.isRequired, | ||
placeholder: PropTypes.string, | ||
value: React.PropTypes.string, | ||
required: PropTypes.bool, | ||
onChange: PropTypes.func, | ||
time: PropTypes.bool, | ||
}; | ||
} | ||
|
||
export default AltDateWidget; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.