Skip to content

Commit

Permalink
fix(datepicker): fix default date bug without selecting
Browse files Browse the repository at this point in the history
  • Loading branch information
feyy committed Aug 2, 2016
1 parent 170537c commit b08fb7e
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 3 deletions.
30 changes: 27 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,33 @@ class DatePicker extends Component {
}

getDate(date = this.props.date) {
// date默认值
if (!date) {
let now = new Date();
if (this.props.minDate) {
let minDate = this.getDate(this.props.minDate);

if (now < minDate) {
return minDate;
}
}

if (this.props.maxDate) {
let maxDate = this.getDate(this.props.maxDate);

if (now > maxDate) {
return maxDate;
}
}

return now;
}

if (date instanceof Date) {
return date;
} else {
return Moment(date, this.format).toDate();
}

return Moment(date, this.format).toDate();
}

getDateStr(date = this.props.date) {
Expand Down Expand Up @@ -201,7 +223,7 @@ class DatePicker extends Component {
Style.dateInput, customStyles.dateInput,
this.state.disabled && Style.disabled,
this.state.disabled && customStyles.disabled
]
];

return (
<TouchableHighlight
Expand Down Expand Up @@ -293,6 +315,8 @@ DatePicker.defaultProps = {
DatePicker.propTypes = {
mode: React.PropTypes.oneOf(['date', 'datetime', 'time']),
date: React.PropTypes.oneOfType([React.PropTypes.string, React.PropTypes.instanceOf(Date)]),
minDate: React.PropTypes.oneOfType([React.PropTypes.string, React.PropTypes.instanceOf(Date)]),
maxDate: React.PropTypes.oneOfType([React.PropTypes.string, React.PropTypes.instanceOf(Date)]),
height: React.PropTypes.number,
duration: React.PropTypes.number,
confirmBtnText: React.PropTypes.string,
Expand Down
47 changes: 47 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,53 @@ describe('DatePicker:', () => {
expect(wrapper3.instance().getDateStr()).to.equal('Invalid date');
});

it('default selected Date', () => {
var dateStr = null;
const wrapper = shallow(<DatePicker date="" onDateChange={(date) => {
dateStr = date
}}/>);
const datePicker = wrapper.instance();

datePicker.onPressConfirm();

expect(dateStr).to.equal(Moment().format('YYYY-MM-DD'));
});

it('default selected Date with minDate and maxDate', () => {
var dateStr = null;
var dateStrMax = null;
var dateStrNormal = null

const wrapper = shallow(<DatePicker date="" minDate="3000-09-09" onDateChange={(date) => {
dateStr = date
}}/>);
const datePicker = wrapper.instance();

datePicker.onPressConfirm();

expect(dateStr).to.equal('3000-09-09');


const wrapperMax = shallow(<DatePicker date="" maxDate="2016-07-07" onDateChange={(date) => {
dateStrMax = date
}}/>);
const datePickerMax = wrapperMax.instance();

datePickerMax.onPressConfirm();

expect(dateStrMax).to.equal('2016-07-07');


const wrapperNormal = shallow(<DatePicker date="" minDate="2016-07-07" maxDate="3000-09-09" onDateChange={(date) => {
dateStrNormal = date
}}/>);
const datePickerNormal = wrapperNormal.instance();

datePickerNormal.onPressConfirm();

expect(dateStrNormal).to.equal(Moment().format('YYYY-MM-DD'));
});

it('setModalVisible', () => {
const wrapper = shallow(<DatePicker />);
const datePicker = wrapper.instance();
Expand Down

0 comments on commit b08fb7e

Please sign in to comment.