Skip to content

Commit eff7e98

Browse files
author
Stephy
committed
Successfully add header controls, rough implementation
0 parents  commit eff7e98

15 files changed

+738
-0
lines changed

.babelrc

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"presets": ["babel-preset-expo"],
3+
"env": {
4+
"development": {
5+
"plugins": ["transform-react-jsx-source"]
6+
}
7+
}
8+
}

.flowconfig

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
[ignore]
2+
; We fork some components by platform
3+
.*/*[.]android.js
4+
5+
; Ignore "BUCK" generated dirs
6+
<PROJECT_ROOT>/\.buckd/
7+
8+
; Ignore unexpected extra "@providesModule"
9+
.*/node_modules/.*/node_modules/fbjs/.*
10+
11+
; Ignore duplicate module providers
12+
; For RN Apps installed via npm, "Libraries" folder is inside
13+
; "node_modules/react-native" but in the source repo it is in the root
14+
.*/Libraries/react-native/React.js
15+
.*/Libraries/react-native/ReactNative.js
16+
17+
; Additional create-react-native-app ignores
18+
19+
; Ignore duplicate module providers
20+
.*/node_modules/fbemitter/lib/*
21+
22+
; Ignore misbehaving dev-dependencies
23+
.*/node_modules/xdl/build/*
24+
.*/node_modules/reqwest/tests/*
25+
26+
; Ignore missing expo-sdk dependencies (temporarily)
27+
; https://github.com/exponent/exponent-sdk/issues/36
28+
.*/node_modules/expo/src/*
29+
30+
; Ignore react-native-fbads dependency of the expo sdk
31+
.*/node_modules/react-native-fbads/*
32+
33+
[include]
34+
35+
[libs]
36+
node_modules/react-native/Libraries/react-native/react-native-interface.js
37+
node_modules/react-native/flow
38+
flow/
39+
40+
[options]
41+
module.system=haste
42+
43+
emoji=true
44+
45+
experimental.strict_type_args=true
46+
47+
munge_underscores=true
48+
49+
module.name_mapper='^[./a-zA-Z0-9$_-]+\.\(bmp\|gif\|jpg\|jpeg\|png\|psd\|svg\|webp\|m4v\|mov\|mp4\|mpeg\|mpg\|webm\|aac\|aiff\|caf\|m4a\|mp3\|wav\|html\|pdf\)$' -> 'RelativeImageStub'
50+
51+
suppress_type=$FlowIssue
52+
suppress_type=$FlowFixMe
53+
suppress_type=$FixMe
54+
55+
suppress_comment=\\(.\\|\n\\)*\\$FlowFixMe\\($\\|[^(]\\|(\\(>=0\\.\\(3[0-8]\\|[1-2][0-9]\\|[0-9]\\).[0-9]\\)? *\\(site=[a-z,_]*react_native_oss[a-z,_]*\\)?)\\)
56+
suppress_comment=\\(.\\|\n\\)*\\$FlowIssue\\((\\(>=0\\.\\(3[0-8]\\|1[0-9]\\|[1-2][0-9]\\).[0-9]\\)? *\\(site=[a-z,_]*react_native_oss[a-z,_]*\\)?)\\)?:? #[0-9]+
57+
suppress_comment=\\(.\\|\n\\)*\\$FlowFixedInNextDeploy
58+
59+
unsafe.enable_getters_and_setters=true
60+
61+
[version]
62+
^0.38.0

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules/
2+
.expo/
3+
npm-debug.*

.watchmanconfig

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

App.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import React from 'react';
2+
import { StyleSheet, Text, View } from 'react-native';
3+
import CalendarPicker from './CalendarPicker';
4+
5+
export default class App extends React.Component {
6+
render() {
7+
return (
8+
<View style={styles.container}>
9+
<CalendarPicker />
10+
<Text>Open up App.js to start working on your app!</Text>
11+
<Text>Changes you make will automatically reload.</Text>
12+
<Text>Shake your phone to open the developer menu.</Text>
13+
</View>
14+
);
15+
}
16+
}
17+
18+
const styles = StyleSheet.create({
19+
container: {
20+
flex: 1,
21+
backgroundColor: '#fff',
22+
alignItems: 'center',
23+
justifyContent: 'center',
24+
},
25+
});

App.test.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import React from 'react';
2+
import App from './App';
3+
4+
import renderer from 'react-test-renderer';
5+
6+
it('renders without crashing', () => {
7+
const rendered = renderer.create(<App />).toJSON();
8+
expect(rendered).toBeTruthy();
9+
});

CalendarPicker/Controls.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import React, { PropTypes } from 'react';
2+
import {
3+
TouchableHighlight,
4+
Text,
5+
} from 'react-native';
6+
7+
export default function Controls({ styles, label, onPressControl }) {
8+
return (
9+
<TouchableHighlight onPress={() => onPressControl()}>
10+
<Text style={styles}>
11+
{ label }
12+
</Text>
13+
</TouchableHighlight>
14+
);
15+
}
16+
17+
Controls.propTypes = {
18+
styles: PropTypes.shape().isRequired,
19+
label: PropTypes.string.isRequired,
20+
onPressControl: PropTypes.func.isRequired,
21+
};

CalendarPicker/HeaderControls.js

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import React, { PropTypes, Component } from 'react';
2+
import {
3+
View,
4+
Text,
5+
} from 'react-native';
6+
import { Utils } from './Utils';
7+
import Controls from './Controls';
8+
9+
export default class HeaderControls extends Component {
10+
constructor(props) {
11+
super(props);
12+
this.state = {
13+
currentMonth: null,
14+
currentYear: null,
15+
}
16+
this.handleOnPressPrevious = this.handleOnPressPrevious.bind(this);
17+
this.handleOnPressNext = this.handleOnPressNext.bind(this);
18+
}
19+
20+
componentWillMount() {
21+
const { initialDate } = this.props;
22+
const date = initialDate ? initialDate : new Date();
23+
24+
this.setState({
25+
currentMonth: parseInt(date.getMonth()),
26+
currentYear: parseInt(date.getFullYear()),
27+
});
28+
}
29+
30+
handleOnPressPrevious() {
31+
const { currentMonth, currentYear } = this.state;
32+
const previousMonth = currentMonth - 1;
33+
// if previousMonth is negative it means the current month is January,
34+
// so we have to go to previous year, and set the current month to December
35+
if (previousMonth < 0) {
36+
this.setState({
37+
currentMonth: parseInt(11), // setting month to December
38+
currentYear: parseInt(currentYear) - 1, // decrement year
39+
});
40+
} else {
41+
this.setState({
42+
currentMonth: parseInt(previousMonth),
43+
currentYear: parseInt(currentYear),
44+
});
45+
}
46+
}
47+
48+
handleOnPressNext() {
49+
const { currentMonth, currentYear } = this.state;
50+
const nextMonth = currentMonth + 1;
51+
// if nextMonth is greater than 11 it means the current month is December,
52+
// so we have to go to next year, and set the current month to January
53+
if (nextMonth > 11) {
54+
this.setState({
55+
currentMonth: parseInt(0), // setting month to December
56+
currentYear: parseInt(currentYear) + 1, // decrement year
57+
});
58+
} else {
59+
this.setState({
60+
currentMonth: parseInt(nextMonth),
61+
currentYear: parseInt(currentYear),
62+
});
63+
}
64+
}
65+
66+
render() {
67+
const { styles, initialDate } = this.props;
68+
const { currentMonth, currentYear } = this.state;
69+
const MONTHS = Utils.MONTHS; // English Month Array
70+
const date = initialDate ? initialDate : new Date();
71+
// getMonth() call below will return the month number, we will use it as the
72+
// index for month array in english
73+
const month = MONTHS[currentMonth];
74+
const year = currentYear;
75+
76+
return (
77+
<View style={styles.headerWrapper}>
78+
<View style={styles.monthSelector}>
79+
<Controls
80+
label="Previous"
81+
onPressControl={this.handleOnPressPrevious}
82+
styles={styles.prev}
83+
/>
84+
</View>
85+
<View>
86+
<Text style={[styles.monthLabel]}>
87+
{ month } { year }
88+
</Text>
89+
</View>
90+
<View style={styles.monthSelector}>
91+
<Controls
92+
label="Next"
93+
onPressControl={this.handleOnPressNext}
94+
styles={styles.next}
95+
/>
96+
</View>
97+
98+
</View>
99+
);
100+
}
101+
}
102+
103+
HeaderControls.propTypes = {
104+
initialDate: PropTypes.instanceOf(Date),
105+
};

CalendarPicker/Styles.js

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* Calendar Picker Component
3+
*
4+
* Copyright 2016 Yahoo Inc.
5+
* Licensed under the terms of the MIT license. See LICENSE file in the project root for terms.
6+
*/
7+
import { StyleSheet } from 'react-native';
8+
9+
export const styles = StyleSheet.create({
10+
calendar: {
11+
marginTop: 10
12+
},
13+
dayWrapper: {
14+
backgroundColor: 'rgba(0,0,0,0.0)',
15+
justifyContent: 'center',
16+
alignItems: 'center'
17+
},
18+
dayButton: {
19+
flex: 1,
20+
justifyContent: 'center',
21+
alignSelf: 'center'
22+
},
23+
dayLabel: {
24+
fontSize: 14,
25+
color: '#000',
26+
alignSelf: 'center'
27+
},
28+
dayLabelsWrapper: {
29+
flexDirection: 'row',
30+
marginBottom: 10,
31+
borderBottomWidth: 1,
32+
borderTopWidth: 1,
33+
paddingTop: 10,
34+
paddingBottom: 10,
35+
alignSelf: 'center',
36+
backgroundColor: 'rgba(0,0,0,0.0)',
37+
borderColor: 'rgba(0,0,0,0.2)'
38+
},
39+
daysWrapper: {
40+
alignSelf: 'center'
41+
},
42+
dayLabels: {
43+
textAlign: 'center'
44+
},
45+
monthLabel: {
46+
fontSize: 16,
47+
color: '#000',
48+
textAlign: 'center'
49+
},
50+
headerWrapper: {
51+
alignItems: 'center',
52+
flexDirection: 'row',
53+
marginBottom: 10,
54+
paddingTop: 5,
55+
paddingBottom: 3,
56+
backgroundColor: 'rgba(0,0,0,0.0)'
57+
},
58+
prev: {
59+
textAlign: 'left'
60+
},
61+
next: {
62+
textAlign: 'right'
63+
},
64+
weeks: {
65+
flexDirection: 'column'
66+
},
67+
weekRow: {
68+
flexDirection: 'row'
69+
}
70+
});

CalendarPicker/Utils.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Calendar Picker Component
3+
*
4+
* Copyright 2016 Yahoo Inc.
5+
* Licensed under the terms of the MIT license. See LICENSE file in the project root for terms.
6+
*/
7+
8+
export const Utils = {
9+
WEEKDAYS: [
10+
'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'
11+
],
12+
MONTHS: [
13+
'January', 'February', 'March', 'April', 'May', 'June', 'July',
14+
'August', 'September', 'October', 'November', 'December'
15+
],
16+
MAX_ROWS: 7,
17+
MAX_COLUMNS: 7,
18+
getDaysInMonth: function(month, year) {
19+
const lastDayOfMonth = new Date(year, month + 1, 0);
20+
return lastDayOfMonth.getDate();
21+
}
22+
};

CalendarPicker/index.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import React, { Component } from 'react';
2+
import {
3+
View,
4+
Text,
5+
Dimensions,
6+
StyleSheet,
7+
} from 'react-native';
8+
import { makeStyles } from './makeStyles';
9+
import HeaderControls from './HeaderControls';
10+
11+
// The styles in makeStyles are intially scaled to this width
12+
const IPHONE6_WIDTH = 375;
13+
const initialScale = Dimensions.get('window').width / IPHONE6_WIDTH ;
14+
const styles = StyleSheet.create(makeStyles(initialScale));
15+
16+
export default class CalendarPicker extends Component {
17+
constructor(props) {
18+
super(props);
19+
this.state = {
20+
initialDate: new Date(),
21+
}
22+
}
23+
render() {
24+
const { initialDate } = this.state;
25+
return (
26+
<View>
27+
<HeaderControls styles={styles} initialDate={initialDate}/>
28+
<Text>Hello Calendar </Text>
29+
</View>
30+
);
31+
}
32+
}

0 commit comments

Comments
 (0)