|
| 1 | +import React from 'react' |
| 2 | +import PropTypes from 'prop-types' |
| 3 | +import { TextInput } from 'react-materialize' |
| 4 | +import idgen from 'libs/idgen' |
| 5 | + |
| 6 | +class DatePicker extends React.Component { |
| 7 | + constructor(props) { |
| 8 | + super(props) |
| 9 | + |
| 10 | + this.id = props.id || `datepicker${idgen()}` |
| 11 | + } |
| 12 | + |
| 13 | + componentDidMount() { |
| 14 | + if (typeof window.M !== 'undefined') { |
| 15 | + const { onChange } = this.props |
| 16 | + const elem = document.getElementById(this.id) |
| 17 | + const options = onChange ? { onSelect: onChange, ...this.props.options } : this.props.options |
| 18 | + |
| 19 | + this.instance = window.M.Datepicker.init(elem, options) |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + componentWillUnmount() { |
| 24 | + if (this.instance) { |
| 25 | + this.instance.destroy() |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + render() { |
| 30 | + const { ...other } = this.props |
| 31 | + |
| 32 | + return <TextInput id={this.id} inputClassName="datepicker" {...other} /> |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +DatePicker.propTypes = { |
| 37 | + /** |
| 38 | + * Event called when Time has been selected |
| 39 | + */ |
| 40 | + onChange: PropTypes.func, |
| 41 | + /** |
| 42 | + * id passed to datepicker, also used for init method |
| 43 | + */ |
| 44 | + id: PropTypes.string, |
| 45 | + /** |
| 46 | + * options passed to init method |
| 47 | + * more info: https://materializecss.com/pickers.html#date-picker |
| 48 | + */ |
| 49 | + options: PropTypes.shape({ |
| 50 | + /** |
| 51 | + * Automatically close picker when date is selected. |
| 52 | + */ |
| 53 | + autoClose: PropTypes.bool, |
| 54 | + /** |
| 55 | + * The date output format for the input field value. |
| 56 | + * @default 'mmm dd, yyyy' |
| 57 | + */ |
| 58 | + format: PropTypes.string, |
| 59 | + /** |
| 60 | + * Used to create date object from current input string. |
| 61 | + * @default null |
| 62 | + */ |
| 63 | + parse: PropTypes.any, |
| 64 | + /** |
| 65 | + * The initial date to view when first opened. |
| 66 | + * @default null |
| 67 | + */ |
| 68 | + defaultDate: PropTypes.any, |
| 69 | + /** |
| 70 | + * Make the defaultDate the initial selected value. |
| 71 | + * @default false |
| 72 | + */ |
| 73 | + setDefaultDate: PropTypes.bool, |
| 74 | + /** |
| 75 | + * Prevent selection of any date on the weekend. |
| 76 | + * @default false |
| 77 | + */ |
| 78 | + disableWeekends: PropTypes.bool, |
| 79 | + /** |
| 80 | + * Custom function to disable certain days. |
| 81 | + * @default null |
| 82 | + */ |
| 83 | + disableDayFn: PropTypes.any, |
| 84 | + /** |
| 85 | + * First day of week (0: Sunday, 1: Monday etc). |
| 86 | + * @default 0 |
| 87 | + */ |
| 88 | + firstDay: PropTypes.number, |
| 89 | + /** |
| 90 | + * The earliest date that can be selected. |
| 91 | + * @default null |
| 92 | + */ |
| 93 | + minDate: PropTypes.any, |
| 94 | + /** |
| 95 | + * The latest date that can be selected. |
| 96 | + * @default null |
| 97 | + */ |
| 98 | + maxDate: PropTypes.any, |
| 99 | + /** |
| 100 | + * Number of years either side, or array of upper/lower range. |
| 101 | + * @default 10 |
| 102 | + */ |
| 103 | + yearRange: PropTypes.oneOf([PropTypes.number, PropTypes.array]), |
| 104 | + /** |
| 105 | + * Changes Datepicker to RTL. |
| 106 | + * @default false |
| 107 | + */ |
| 108 | + isRTL: PropTypes.bool, |
| 109 | + /** |
| 110 | + * Show month after year in Datepicker title. |
| 111 | + * @default false |
| 112 | + */ |
| 113 | + showMonthAfterYear: PropTypes.bool, |
| 114 | + /** |
| 115 | + * Render days of the calendar grid that fall in the next or previous month. |
| 116 | + * @default false |
| 117 | + */ |
| 118 | + showDaysInNextAndPreviousMonths: PropTypes.bool, |
| 119 | + /** |
| 120 | + * Specify a DOM element to render the calendar in, by default it will be placed before the input. |
| 121 | + * @default null |
| 122 | + */ |
| 123 | + container: PropTypes.any, |
| 124 | + /** |
| 125 | + * Show the clear button in the datepicker. |
| 126 | + * @default false |
| 127 | + */ |
| 128 | + showClearBtn: PropTypes.bool, |
| 129 | + /** |
| 130 | + * Internationalization options. |
| 131 | + * @default See i18n documentation. |
| 132 | + */ |
| 133 | + i18n: PropTypes.any, |
| 134 | + /** |
| 135 | + * An array of string returned by `Date.toDateString()`, indicating there are events in the specified days. |
| 136 | + * @default [] |
| 137 | + */ |
| 138 | + events: PropTypes.array, |
| 139 | + /** |
| 140 | + * Callback function when date is selected, first parameter is the newly selected date. |
| 141 | + * @default null |
| 142 | + */ |
| 143 | + onSelect: PropTypes.any, |
| 144 | + /** |
| 145 | + * Callback function when Datepicker is opened. |
| 146 | + * @default null |
| 147 | + */ |
| 148 | + onOpen: PropTypes.any, |
| 149 | + /** |
| 150 | + * Callback function when Datepicker is closed. |
| 151 | + * @default null |
| 152 | + */ |
| 153 | + onClose: PropTypes.any, |
| 154 | + /** |
| 155 | + * Callback function when Datepicker HTML is refreshed. |
| 156 | + * @default null |
| 157 | + */ |
| 158 | + onDraw: PropTypes.any, |
| 159 | + }), |
| 160 | +} |
| 161 | + |
| 162 | +export default DatePicker |
0 commit comments