Skip to content

Commit f274179

Browse files
committed
feat(datepicker): add supoort of showIcon property
Controller whether or not show the icon #11
1 parent 7bd2b7c commit f274179

File tree

4 files changed

+43
-33
lines changed

4 files changed

+43
-33
lines changed

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# react-native-datepicker [![Build Status](https://travis-ci.org/xgfe/react-native-datepicker.svg)](https://travis-ci.org/xgfe/react-native-datepicker) [![Coverage Status](https://coveralls.io/repos/github/xgfe/react-native-datepicker/badge.svg?branch=master)](https://coveralls.io/github/xgfe/react-native-datepicker?branch=master)
2-
React Native datePicker component for both Android and iOS, using DatePickerAndroid, TimePickerAndroid and DatePickeriOS.
1+
# react-native-datepicker
2+
react native datePicker component for both Android and IOS, useing DatePikcerAndroid, TimePickerAndroid and DatePickerIOS
33

44
## Install
55

@@ -8,7 +8,7 @@ npm install react-native-datepicker --save
88
```
99

1010
## Example
11-
Check [index.android.js](example/index.android.js) in the Example folder.
11+
Check [index.js](https://github.com/xgfe/react-native-datepicker/blob/master/example/index.android.js) in the Example folder.
1212

1313
![android](http://7xtixz.com2.z0.glb.clouddn.com/react-native-datepicker-android.gif)
1414
![android](http://7xtixz.com2.z0.glb.clouddn.com/react-native-datepicker-ios.gif)
@@ -29,7 +29,7 @@ Check [index.android.js](example/index.android.js) in the Example folder.
2929
/>
3030
```
3131

32-
You can check [index.js](example/index.android.js) in the Example folder for detail.
32+
You can check [index.js](https://github.com/xgfe/react-native-datepicker/blob/master/example/index.android.js) in the Example folder for detail.
3333

3434
## Properties
3535

@@ -46,10 +46,11 @@ You can check [index.js](example/index.android.js) in the Example folder for det
4646
| maxDate | - | `string | date` | Restricts the range of possible date values. |
4747
| duration | 300 | `number` | Specify the animation duration of datepicker.|
4848
| customStyles | - | `number` | The hook of customize datepicker style, same as the native style. `dateTouchBody`, `dateInput`...|
49-
| onDateChange | - | `function(date:string)` | This is called when the user confirm the picked date or time in the UI. The first and only argument is a date or time string representing the new date and time formatted by [moment.js](http://momentjs.com/) with the given format property. |
49+
| showIcon | true | `boolean` | Controller whether or not show the icon |
5050

5151
## Methods
5252

53+
5354
| Method | Params | Description |
5455
| :------------ |:---------------:| :---------------:|
55-
| onPressDate | - | Manual call this method could open the DatePicker with js. |
56+
| onDateChange | date:string | This is called when the user confirm the picked date or time in the UI. The first and only argument is a date or time string representing the new date and time formatted by [moment.js](http://momentjs.com/) with the given format property. |

example/index.android.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class example extends Component {
6060
format="YYYY-MM-DD HH:mm"
6161
confirmBtnText="Confirm"
6262
cancelBtnText="Cancel"
63+
showIcon={false}
6364
onDateChange={(datetime) => {this.setState({datetime: datetime});}}
6465
/>
6566
<Text style={styles.instructions}>datetime: {this.state.datetime}</Text>

index.js

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,34 @@ class DatePicker extends Component {
2424
constructor(props) {
2525
super(props);
2626

27+
this.mode = this.props.mode || 'date';
28+
29+
this.format = this.props.format || FORMATS[this.mode];
30+
// component height: 216(DatePickerIOS) + 1(borderTop) + 42(marginTop), IOS only
31+
this.height = 259;
32+
// slide animation duration time, default to 300ms, IOS only
33+
this.duration = this.props.duration || 300;
34+
35+
this.confirmBtnText = this.props.confirmBtnText || '确定';
36+
this.cancelBtnText = this.props.cancelBtnText || '取消';
37+
38+
this.iconSource = this.props.iconSource || require('./date_icon.png');
39+
this.customStyles = this.props.customStyles || {};
40+
41+
// whether or not show the icon
42+
if (typeof this.props.showIcon === 'boolean') {
43+
this.showIcon = this.props.showIcon;
44+
} else {
45+
this.showIcon = true;
46+
}
47+
48+
this.state = {
49+
date: this.getDate(),
50+
modalVisible: false,
51+
disabled: this.props.disabled,
52+
animatedHeight: new Animated.Value(0)
53+
};
54+
2755
this.datePicked = this.datePicked.bind(this);
2856
this.onPressDate = this.onPressDate.bind(this);
2957
this.onPressCancel = this.onPressCancel.bind(this);
@@ -35,37 +63,12 @@ class DatePicker extends Component {
3563
this.setModalVisible = this.setModalVisible.bind(this);
3664
}
3765

38-
format = this.props.format;
39-
mode = this.props.mode || 'date';
40-
// component height: 216(DatePickerIOS) + 1(borderTop) + 42(marginTop), IOS only
41-
height = 259;
42-
// slide animation duration time, default to 300ms, IOS only
43-
duration = this.props.duration || 300;
44-
45-
confirmBtnText = this.props.confirmBtnText || '确定';
46-
cancelBtnText = this.props.cancelBtnText || '取消';
47-
48-
iconSource = this.props.iconSource || require('./date_icon.png');
49-
customStyles = this.props.customStyles || {};
50-
51-
state = {
52-
date: this.getDate(),
53-
modalVisible: false,
54-
disabled: this.props.disabled,
55-
animatedHeight: new Animated.Value(0)
56-
};
57-
5866
componentWillMount() {
5967
// ignore the warning of Failed propType for date of DatePickerIOS, will remove after being fixed by official
6068
console.ignoredYellowBox = [
6169
'Warning: Failed propType'
6270
// Other warnings you don't want like 'jsSchedulingOverhead',
6371
];
64-
65-
// init format according to mode
66-
if (!this.format) {
67-
this.format = FORMATS[this.mode];
68-
}
6972
}
7073

7174
setModalVisible(visible) {
@@ -214,10 +217,10 @@ class DatePicker extends Component {
214217
<View style={[Style.dateInput, this.customStyles.dateInput, this.state.disabled && Style.disabled]}>
215218
<Text style={[Style.dateText, this.customStyles.dateText]}>{this.getDateStr()}</Text>
216219
</View>
217-
<Image
220+
{this.showIcon && <Image
218221
style={[Style.dateIcon, this.customStyles.dateIcon]}
219222
source={this.iconSource}
220-
/>
223+
/>}
221224
{Platform.OS === 'ios' && <Modal
222225
transparent={true}
223226
visible={this.state.modalVisible}

test/index.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ describe('DatePicker:', () => {
3535
expect(datePicker.cancelBtnText).to.equal('取消');
3636
expect(datePicker.iconSource).to.deep.equal(require('../date_icon.png'));
3737
expect(datePicker.customStyles).to.deep.equal({});
38+
expect(datePicker.showIcon).to.equal(true);
39+
expect(wrapper.find('Image').length).to.equal(1);
3840

3941
expect(wrapper.state('date')).to.be.a('date');
4042
expect(wrapper.state('disabled')).to.equal(undefined);
@@ -52,6 +54,7 @@ describe('DatePicker:', () => {
5254
iconSource={{}}
5355
customStyles={{testStyle: 123}}
5456
disabled={true}
57+
showIcon={false}
5558
/>
5659
);
5760
const datePicker1 = wrapper1.instance();
@@ -64,6 +67,8 @@ describe('DatePicker:', () => {
6467
expect(datePicker1.cancelBtnText).to.equal('Cancel');
6568
expect(datePicker1.iconSource).to.deep.equal({});
6669
expect(datePicker1.customStyles).to.deep.equal({testStyle: 123});
70+
expect(datePicker1.showIcon).to.equal(false);
71+
expect(wrapper1.find('Image').length).to.equal(0);
6772

6873
expect(wrapper1.state('date')).to.deep.equal(Moment('2016-05-11', 'YYYY-MM-DD').toDate());
6974
expect(wrapper1.state('disabled')).to.equal(true);

0 commit comments

Comments
 (0)