UNPKG

17.3 kBMarkdownView Raw
1# react-native-calendar-strip
2
3Easy to use and visually stunning calendar component for React Native.
4
5`<CalendarStrip>` is a React Native component designed to replace the standard date picker component. It supports `iOS` & `Android` and responsively sizes its height based on its container width. It gracefully resizes for `portrait` and `landscape` orientations and virtually all display resolutions.
6
7![alt text](https://raw.githubusercontent.com/BugiDev/react-native-calendar-strip/master/example/gifs/Initial.gif "react-native-calendar-strip demo")
8
9#### Installation
10Just use the standard npm installation
11```sh
12npm install react-native-calendar-strip --save
13```
14
15#### Example
16
17You can see the examples in [\example](https://github.com/BugiDev/react-native-calendar-strip/tree/master/example/ReactNativeCalendarStripExample) folder of this repo.
18
19##### Simple "out of the box" usage
20You can use this component without any styling or customization.
21Just import it in your project and render it:
22```javascript
23import React, {Component} from 'react';
24import {
25 AppRegistry,
26 View
27} from 'react-native';
28
29import CalendarStrip from 'react-native-calendar-strip';
30
31class Example extends Component {
32 render() {
33 return (
34 <View>
35 <CalendarStrip/>
36 </View>
37 );
38 }
39}
40
41AppRegistry.registerComponent('Example', () => Example);
42```
43![alt text](https://raw.githubusercontent.com/BugiDev/react-native-calendar-strip/master/example/gifs/blank.gif "react-native-calendar-strip simple demo")
44
45##### Styling and animations
46Even though this component works withouth any customization, it is possible to customize almost everything, so you can make it as beautiful as you want:
47```javascript
48import React, {Component} from 'react';
49import {
50 AppRegistry,
51 View
52} from 'react-native';
53import moment from 'moment';
54
55import CalendarStrip from 'react-native-calendar-strip';
56
57class Example extends Component {
58 let datesWhitelist = [{
59 start: moment(),
60 end: moment.add(3, 'days') // total 4 days enabled
61 }];
62 let datesBlacklist = [ moment.add(1, 'days') ]; // 1 day disabled
63
64 render() {
65 return (
66 <View>
67 <CalendarStrip
68 calendarAnimation={{type: 'sequence', duration: 30}}
69 daySelectionAnimation={{type: 'border', duration: 200, borderWidth: 1, borderHighlightColor: 'white'}}
70 style={{height: 100, paddingTop: 20, paddingBottom: 10}}
71 calendarHeaderStyle={{color: 'white'}}
72 calendarColor={'#7743CE'}
73 dateNumberStyle={{color: 'white'}}
74 dateNameStyle={{color: 'white'}}
75 highlightDateNumberStyle={{color: 'yellow'}}
76 highlightDateNameStyle={{color: 'yellow'}}
77 disabledDateNameStyle={{color: 'grey'}}
78 disabledDateNumberStyle={{color: 'grey'}}
79 datesWhitelist={datesWhitelist}
80 datesBlacklist={datesBlacklist}
81 iconLeft={require('./img/left-arrow.png')}
82 iconRight={require('./img/right-arrow.png')}
83 iconContainer={{flex: 0.1}}
84 />
85 </View>
86 );
87 }
88}
89
90AppRegistry.registerComponent('Example', () => Example);
91```
92![alt text](https://raw.githubusercontent.com/BugiDev/react-native-calendar-strip/master/example/gifs/border.gif "react-native-calendar-strip border demo")
93
94#### Props / API
95This is the list of all the props you can pass to the component so that you can customize it:
96###### Initial data and onDateSelected handler
97 * startingDate: PropTypes.any - Date to be used for centering the calendar/showing the week based on that date. It is internaly wrapped by `moment` so it accepts both `Date` and `moment Date`.
98 * selectedDate: PropTypes.any - Date to be used as pre selected Date. It is internaly wrapped by `moment` so it accepts both `Date` and `moment Date`.
99 * onDateSelected: PropTypes.func - Function to be used as a callback when a date is selected. It returns `moment Date`
100 * onWeekChanged: PropTypes.func - Function to be used as a callback when a week is changed. It returns `moment Date`
101 * useIsoWeekday: PropTypes.bool - start week on ISO day of week (default true). If false, starts week on _startingDate_ parameter.
102 * minDate: PropTypes.any - minimum date that the calendar may navigate to. A week is allowed if minDate falls within the current week.
103 * maxDate: PropTypes.any - maximum date that the calendar may navigate to. A week is allowed if maxDate falls within the current week.
104 * datesWhitelist: PropTypes.array - Dates that are enabled (accepts both `Date` and `moment Date`). Ranges may be specified with an object entry in the array:
105 ```
106 // Date range format
107 {
108 start: (Date or moment Date)
109 end: (Date or moment Date)
110 }
111 ```
112 This may be overridden by _datesBlacklist_.
113 * datesBlacklist: PropTypes.array - Dates that are disabled. Same format as _datesWhitelist_. This overrides dates in _datesWhitelist_.
114
115###### Show or hide components
116 * showMonth: PropTypes.bool - Show (default true) or hide (false) the month label.
117 * showDate: PropTypes.bool - Show (default true) or hide (false) all the dates.
118 * showDayName: PropTypes.bool - Show (default true) or hide (false) the day name label.
119 * showDayNumber: PropTypes.bool - Show (default true) or hide (false) the day number label.
120
121###### Top level style
122 * style: PropTypes.any - Style for the top level CalendarStrip component
123 * innerStyle: PropTypes.any - Style for the responsively sized inner view. This is necessary to account for padding/margin from the top level view. The inner view has style `flex:1` by default. If this component is nested within another dynamically sized container, remove the flex style by passing in `[]`.
124
125###### Responsive sizing
126 * maxDayComponentSize: PropTypes.number - (default 80) Maximum size that CalendarDay will responsively size up to.
127 * minDayComponentSize: PropTypes.number - (default 10) Minimum size that CalendarDay will responsively size down to.
128 * responsiveSizingOffset: PropTypes.number - (default 0) Adjust the responsive sizing. May be positive (increase size) or negative (decrease size). This value is added to the calculated day component width.
129
130###### Icon
131 * iconLeft: PropTypes.any - Icon to be used for the left icon. It accepts require statement with url to the image (`require('./img/icon.png')`), or object with remote uri `{uri: 'http://example.com/image.png'}`
132 * iconRight: PropTypes.any - Icon to be used for the right icon. It accepts require statement with url to the image (`require('./img/icon.png')`), or object with remote uri `{uri: 'http://example.com/image.png'}`
133 * iconStyle: PropTypes.any - Style that is applied to both left and right icons. It is applied before *iconLeftStyle* or *iconLeftStyle*.
134 * iconLeftStyle: PropTypes.any - Style for left icon. It will override all of the other styles applied to icons.
135 * iconRightStyle: PropTypes.any - Style for right icon. It will override all of the other styles applied to icons.
136 * iconContainer: PropTypes.any - Style for the container of icons. (Example usage is to add `flex` property to it so in the portrait mode, it will shrink the dates strip)
137 * leftSelector: PropTypes.any - Component for the left selector control. May be an instance of any React component. This overrides the icon* props above. Passing in an empty array `[]` hides this control.
138 * rightSelector: PropTypes.any - same as above but for the right selector control.
139
140###### Header style and formatting
141 * calendarHeaderStyle: PropTypes.any - Style for the header text of the calendar.
142 * calendarHeaderFormat: PropTypes.string - Format for the header text of the calendar. For options, refere to [moments documentation](http://momentjs.com/docs/#/displaying/format/)
143
144###### Date name and number styling
145 * dateNameStyle: PropTypes.any - Style for the name of the day on work days in dates strip.
146 * dateNumberStyle: PropTypes.any - Style for the number of the day on work days in dates strip.
147 * weekendDateNameStyle: PropTypes.any - Style for the name of the day on weekend days in dates strip.
148 * weekendDateNumberStyle: PropTypes.any - Style for the number of the day on weekend days in dates strip.
149 * styleWeekend: PropTypes.bool - (default true) Whether to style weekend dates separately.
150 * highlightDateNameStyle: PropTypes.any - Style for the selected name of the day in dates strip.
151 * highlightDateNumberStyle: PropTypes.any - Style for the selected number of the day in dates strip.
152
153 * disabledDateNameStyle: PropTypes.any - Style for disabled name of the day in dates strip (controlled by datesWhitelist & datesBlacklist).
154 * disabledDateNumberStyle: PropTypes.any - Style for disabled number of the day in dates strip (controlled by datesWhitelist & datesBlacklist).
155 * disabledDateOpacity: PropTypes.number - (default 0.3) Opacity of disabled dates strip.
156 * customDatesStyles: PropTypes.array - Custom per-date styling, overriding the styles above. Object format:
157 * startDate: anything parseable by Moment.
158 * endDate: (optional) specify a range. If no endDate is supplied, startDate is treated as a single date.
159 * dateNameStyle: (optional, see above)
160 * dateNumberStyle: (optional, see above)
161 * dateContainerStyle: (optional highlight style)
162
163 ![Custom date styling example](https://cloud.githubusercontent.com/assets/6295083/25105759/a3335fc8-238b-11e7-9a92-3174498a0d89.png)
164
165 Example:
166 ```
167 let customDatesStyles = [];
168 let startDate = moment();
169 for (let i=0; i<6; i++) {
170 customDatesStyles.push({
171 startDate: startDate.clone().add(i, 'days'), // Single date since no endDate provided
172 dateNameStyle: {styles.someDateNameStyle},
173 dateNumberStyle: {styles.someDateNumberStyle},
174 // Random color...
175 dateContainerStyle: {{backgroundColor: '#'+('#00000'+(Math.random()*(1<<24)|0).toString(16)).slice(-6)}},
176 });
177 }
178
179 render() {
180 return (
181 <CalendarStrip
182 customDatesStyles={customDatesStyles}
183 ...
184 />
185 );
186 }
187 ```
188
189###### Animations
190 The week strip and day selection have configurable opacity animations. If they are not specified in the props, by default the animations are disabled.
191
192**Week strip animation**
193 * calendarAnimation: PropTypes.object - options for animating the week.
194 * type: `sequence` or `parallel`
195 * duration: duration of animation in milliseconds.
196
197 Sequence example (dates shown one by one):
198
199 ![alt text](https://raw.githubusercontent.com/BugiDev/react-native-calendar-strip/master/example/gifs/squential.gif "react-native-calendar-strip sequence animation demo")
200
201 Parallel example (dates shown all at once):
202
203 ![alt text](https://raw.githubusercontent.com/BugiDev/react-native-calendar-strip/master/example/gifs/parallel.gif "react-native-calendar-strip parallel animation demo")
204
205**Day selection animation**
206 * daySelectionAnimation: PropTypes.object -
207 * type: `border` or `background`. 'border' animates a circular border around a date. 'background' animates the background of the date.
208 * duration: duration of animation in milliseconds.
209 * borderWidth: PropTypes.number - Selected day's border width. Required if the type is set to _border_.
210 * borderHighlightColor: PropTypes.string - Selected day's border color. Required if the type is set to _border_.
211 * highlightColor: PropTypes.string - Highlighted color of selected date. Required if the type is set to _background_.
212 * animType, animUpdateType, animProperty, and animSpringDamping are optional config options passed to [LayoutAnimation](https://facebook.github.io/react-native/docs/layoutanimation.html)
213
214 Border example:
215
216 ![alt text](https://raw.githubusercontent.com/BugiDev/react-native-calendar-strip/master/example/gifs/border-small.gif "react-native-calendar-strip border animation demo")
217
218 If you chose the `background` type of animation, the selected date will animate the circular background color from `calendarColor` to `daySelectionAnimation.highlightColor`. That is why, when this type is chosen, you have to define both of these colors.
219 * calendarColor: PropTypes.string - Background color of the whole calendar strip. `!important` This is also the color of the circular background of the date so that highlighting the date will appear 'from background'. The top level style of this component can override this property.
220
221 Background example:
222
223 ![alt text](https://raw.githubusercontent.com/BugiDev/react-native-calendar-strip/master/example/gifs/background-small.gif "react-native-calendar-strip simple demo")
224
225###### Localization
226 * locale: PropTypes.object - Locale for dates
227
228 This prop is used for adding localization to react-native-calendar-strip component. The localization rules are the same as moments and can be found in [moments documentation](http://momentjs.com/docs/#/i18n/)
229
230 The locale object has two properties:
231 * `name` - String - The name of the locale (ex. 'fr')
232 * `config` - Object - The config object holding all of the localization strings.
233
234 Example of one locale object is:
235 ```
236 const locale = {
237 name: 'fr',
238 config: {
239 months : "Janvier_Février_Mars_Avril_Mai_Juin_Juillet_Août_Septembre_Octobre_Novembre_Décembre".split("_"),
240 monthsShort : "Janv_Févr_Mars_Avr_Mai_Juin_Juil_Août_Sept_Oct_Nov_Déc".split("_"),
241 weekdays : "Dimanche_Lundi_Mardi_Mercredi_Jeudi_Vendredi_Samedi".split("_"),
242 weekdaysShort : "Dim_Lun_Mar_Mer_Jeu_Ven_Sam".split("_"),
243 weekdaysMin : "Di_Lu_Ma_Me_Je_Ve_Sa".split("_"),
244 longDateFormat : {
245 LT : "HH:mm",
246 LTS : "HH:mm:ss",
247 L : "DD/MM/YYYY",
248 LL : "D MMMM YYYY",
249 LLL : "D MMMM YYYY LT",
250 LLLL : "dddd D MMMM YYYY LT"
251 },
252 calendar : {
253 sameDay: "[Aujourd'hui à] LT",
254 nextDay: '[Demain à] LT',
255 nextWeek: 'dddd [à] LT',
256 lastDay: '[Hier à] LT',
257 lastWeek: 'dddd [dernier à] LT',
258 sameElse: 'L'
259 },
260 relativeTime : {
261 future : "dans %s",
262 past : "il y a %s",
263 s : "quelques secondes",
264 m : "une minute",
265 mm : "%d minutes",
266 h : "une heure",
267 hh : "%d heures",
268 d : "un jour",
269 dd : "%d jours",
270 M : "un mois",
271 MM : "%d mois",
272 y : "une année",
273 yy : "%d années"
274 },
275 ordinalParse : /\d{1,2}(er|ème)/,
276 ordinal : function (number) {
277 return number + (number === 1 ? 'er' : 'ème');
278 },
279 meridiemParse: /PD|MD/,
280 isPM: function (input) {
281 return input.charAt(0) === 'M';
282 },
283 // in case the meridiem units are not separated around 12, then implement
284 // this function (look at locale/id.js for an example)
285 // meridiemHour : function (hour, meridiem) {
286 // return /* 0-23 hour, given meridiem token and hour 1-12 */
287 // },
288 meridiem : function (hours, minutes, isLower) {
289 return hours < 12 ? 'PD' : 'MD';
290 },
291 week : {
292 dow : 1, // Monday is the first day of the week.
293 doy : 4 // The week that contains Jan 4th is the first week of the year.
294 }
295 }
296 };
297 ```
298
299###### Methods
300Methods may be accessed through the instantiated component's [ref](http://reactjs.cn/react/docs/more-about-refs.html).
301 * `getSelectedDate()` : Returns the currently selected date. If no date is selected, returns undefined.
302 * `setSelectedDate(date)` : Sets the selected date. `date` may be a Moment object, ISO8601 date string, or any format that Moment is able to parse. It is the responsibility of the caller to select a date that makes sense (e.g. within the current week view). Passing in a value of `0` effectively clears the selected date.
303 * `getNextWeek()` : Advance to the next week.
304 * `getPreviousWeek()` : Rewind to the previous week.
305 * `updateWeekView(date, startDate)` : Show the week that includes the `date` param. If `startDate` is provided, the first day of the week resets to it as long as `useIsoWeekday` is false.
306
307#### ToDo
308
309 * [ ] Add unit tests
310 * [ ] Add Travis CI integration
311 * [ ] Implement week view, which should show weeks in a month rather than days, so that you select the whole week
312 * [ ] Add a month and a year selection in a form of a popup
313
\No newline at end of file