Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support singleDate #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 75 additions & 25 deletions Calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ export default class Calendar extends Component {
format: PropTypes.string,
customI18n: PropTypes.object,
color: PropTypes.object,
singleDate: PropTypes.bool,
minDate: PropTypes.oneOfType([PropTypes.string, PropTypes.instanceOf(Date)]),
maxDate: PropTypes.oneOfType([PropTypes.string, PropTypes.instanceOf(Date)])
}
static defaultProps = {
format: 'YYYY-MM-DD',
i18n: 'en',
customI18n: {},
color: {}
color: {},
singleDate:false
}
static I18N_MAP = {
'zh': {
Expand All @@ -43,7 +45,8 @@ export default class Calendar extends Component {
'end': '结 束',
'date': '日 期',
'save': '保 存',
'clear': '清除'
'clear': '清除',
'selected': '选'
},
'date': 'M月D日'
},
Expand All @@ -55,7 +58,8 @@ export default class Calendar extends Component {
'end': 'End',
'date': 'Date',
'save': 'Save',
'clear': 'Reset'
'clear': 'Reset',
'selected': 'Selected'
},
'date': 'DD / MM'
},
Expand All @@ -67,7 +71,8 @@ export default class Calendar extends Component {
'end': 'エンド',
'date': '時 間',
'save': '確 認',
'clear': 'クリア'
'clear': 'クリア',
'selected': '選択'
},
'date': 'M月D日'
}
Expand Down Expand Up @@ -153,6 +158,16 @@ export default class Calendar extends Component {
startDate,
endDate
} = this.state;
if(this.props.singleDate==true){
this.setState({
startDate: day,
startDateText: this._i18n(day, 'date'),
startWeekdayText: this._i18n(day.isoWeekday(), 'weekday'),
endDate: day,
endDateText: this._i18n(day, 'date'),
endWeekdayText: this._i18n(day.isoWeekday(), 'weekday')
});
}else{
if ((!startDate && !endDate) || day < startDate || (startDate && endDate)) {
this.setState({
startDate: day,
Expand All @@ -169,6 +184,7 @@ export default class Calendar extends Component {
endWeekdayText: this._i18n(day.isoWeekday(), 'weekday')
});
}
}
}
cancel () {
this.close();
Expand All @@ -194,6 +210,7 @@ export default class Calendar extends Component {
endWeekdayText: ''
});
}

confirm () {
const {
startDate,
Expand All @@ -209,6 +226,57 @@ export default class Calendar extends Component {
});
this.close();
}
_renderReturn(){
const {
startDate,
endDate,
startDateText,
startWeekdayText,
endDateText,
endWeekdayText
} = this.state;
const {
mainColor = '#15aaaa',
subColor = '#fff',
borderColor = 'rgba(255, 255, 255, 0.50)'
} = this.props.color;
let color = {mainColor, subColor, borderColor};
let subFontColor = {color: subColor};
let subBack = {backgroundColor: subColor};
if(this.props.singleDate==true){
return ( <View style={styles.result}>

<View style={styles.resultPart}>
<Text style={[styles.resultText, styles.endText, subFontColor]}>
{endDateText || this._i18n('selected', 'text')}
</Text>
<Text style={[styles.resultText, styles.endText, subFontColor]}>
{endWeekdayText || this._i18n('date', 'text')}
</Text>
</View>
</View>)
}else{
return ( <View style={styles.result}>
<View style={styles.resultPart}>
<Text style={[styles.resultText, styles.startText, subFontColor]}>
{startDateText || this._i18n('start', 'text')}
</Text>
<Text style={[styles.resultText, styles.startText, subFontColor]}>
{startWeekdayText || this._i18n('date', 'text')}
</Text>
</View>
<View style={[styles.resultSlash, subBack]}/>
<View style={styles.resultPart}>
<Text style={[styles.resultText, styles.endText, subFontColor]}>
{endDateText || this._i18n('end', 'text')}
</Text>
<Text style={[styles.resultText, styles.endText, subFontColor]}>
{endWeekdayText || this._i18n('date', 'text')}
</Text>
</View>
</View>)
}
}
render () {
const {
startDate,
Expand All @@ -228,11 +296,11 @@ export default class Calendar extends Component {
let subBack = {backgroundColor: subColor};
let mainFontColor = {color: mainColor};
let subFontColor = {color: subColor};
let isValid = !startDate || endDate;
let isValid = (this.props.singleDate==true)? startDate!=null : !startDate || endDate;
let isClearVisible = startDate || endDate;
return (
<Modal
animationType={'slide'}
animationType={'none'}
visible={this.state.isModalVisible}
onRequestClose={this.close}>
<View style={[styles.container, mainBack]}>
Expand All @@ -253,25 +321,7 @@ export default class Calendar extends Component {
<Text style={[styles.clearText, subFontColor]}>{this._i18n('clear', 'text')}</Text>
</TouchableHighlight>}
</View>
<View style={styles.result}>
<View style={styles.resultPart}>
<Text style={[styles.resultText, styles.startText, subFontColor]}>
{startDateText || this._i18n('start', 'text')}
</Text>
<Text style={[styles.resultText, styles.startText, subFontColor]}>
{startWeekdayText || this._i18n('date', 'text')}
</Text>
</View>
<View style={[styles.resultSlash, subBack]}/>
<View style={styles.resultPart}>
<Text style={[styles.resultText, styles.endText, subFontColor]}>
{endDateText || this._i18n('end', 'text')}
</Text>
<Text style={[styles.resultText, styles.endText, subFontColor]}>
{endWeekdayText || this._i18n('date', 'text')}
</Text>
</View>
</View>
{this._renderReturn()}
<View style={styles.week}>
{[7, 1, 2, 3, 4, 5, 6].map(item =>
<Text style={[styles.weekText, subFontColor]} key={item}>{this._i18n(item, 'w')}</Text>
Expand Down
64 changes: 64 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,69 @@ render() {
}
```


```js
constructor (props) {
super(props);
this.state = {
startDate: new Date(2017, 6, 12),
endDate: new Date(2017, 8, 2)
};
this.confirmDate = this.confirmDate.bind(this);
this.openCalendar = this.openCalendar.bind(this);
}
// when confirm button is clicked, an object is conveyed to outer component
// contains following property:
// startDate [Date Object], endDate [Date Object]
// startMoment [Moment Object], endMoment [Moment Object]
confirmDate({startDate, endDate, startMoment, endMoment}) {
this.setState({
startDate,
endDate
});
}
openCalendar() {
this.calendar && this.calendar.open();
}
// in render function
render() {
// It's an optional property, I use this to show the structure of customI18n object.
let customI18n = {
'w': ['', 'Mon', 'Tues', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun'],
'weekday': ['', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'],
'text': {
'start': 'Check in',
'end': 'Check out',
'date': 'Date',
'save': 'Confirm',
'clear': 'Reset'
},
'date': 'DD / MM' // date format
};
// optional property, too.
let color = {
subColor: '#f0f0f0'
};
return (
<View>
<Button title="Open Calendar" onPress={this.openCalendar}>
<Calendar
i18n="en"
ref={(calendar) => {this.calendar = calendar;}}
customI18n={customI18n}
color={color}
format="YYYYMMDD"
minDate="20170510"
maxDate="20180312"
singleDate={true}
onConfirm={this.confirmDate}
/>
</View>
);
}
```


### Properties


Expand All @@ -118,6 +181,7 @@ render() {
| startDate | String / Object | null | Start date of selection |
| endDate | String / Object | null | End date of selection |
| onConfirm | Function | - | Callback function when the period is confirmed, receives an object as only parameter, contains `startDate` / `endDate` / `startMoment` / `endMoment` four property. |
| singleDate | Boolean | false | Single Date selection |

### Instance methods

Expand Down