-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #150 from mapbox/react-datepicker
Replace native date input with react-datepicker
- Loading branch information
Showing
5 changed files
with
138 additions
and
178 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
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,61 @@ | ||
// @flow | ||
import React from 'react'; | ||
import { List, fromJS } from 'immutable'; | ||
import moment from 'moment'; | ||
import type { InputType } from './'; | ||
import type Moment from 'moment'; | ||
|
||
import DatePicker from 'react-datepicker'; | ||
import 'react-datepicker/dist/react-datepicker.css'; | ||
|
||
export class Date extends React.Component { | ||
props: { | ||
name: string, | ||
display: string, | ||
type: string, | ||
placeholder: string, | ||
value: List<InputType>, | ||
className: string, | ||
disabled: ?boolean, | ||
onChange: (string, ?List<InputType>) => any, | ||
min: ?string, | ||
max: ?string | ||
}; | ||
static defaultProps = { | ||
className: '' | ||
}; | ||
handleDateChange = (momentObj: ?Moment) => { | ||
const value = momentObj ? momentObj.format('YYYY-MM-DD') : null; | ||
const name = this.props.name; | ||
if (value) { | ||
this.props.onChange( | ||
name, | ||
fromJS([ | ||
// allways sends 1 size array to keep things consistent | ||
{ | ||
label: value, | ||
value | ||
} | ||
]) | ||
); | ||
} else { | ||
this.props.onChange(name, null); | ||
} | ||
}; | ||
render() { | ||
const { placeholder, display, value, className, min, max } = this.props; | ||
const hasValue = value && value.getIn([0, 'value']); | ||
return ( | ||
<DatePicker | ||
className={`input ${className}`} | ||
dateFormat="DD/MM/YYYY" | ||
isClearable={true} | ||
selected={hasValue ? moment(value.getIn([0, 'value'])) : null} | ||
placeholderText={placeholder || display} | ||
onChange={this.handleDateChange} | ||
minDate={min && moment(min)} | ||
maxDate={max && moment(max)} | ||
/> | ||
); | ||
} | ||
} |
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
Oops, something went wrong.