/
element.js
205 lines (186 loc) · 4.5 KB
/
element.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/**
* External dependencies
*/
import React, { Fragment } from 'react';
import PropTypes from 'prop-types';
import moment from 'moment';
import { noop } from 'lodash';
import classNames from 'classnames';
import { ScrollTo, ScrollArea } from 'react-scroll-to';
/**
* WordPress dependencies
*/
import {
Dropdown,
Dashicon,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { PreventBlockClose } from '@moderntribe/common/components';
import Button from '@moderntribe/common/elements/button';
import Input from '@moderntribe/common/elements/input';
import {
date as dateUtil,
moment as momentUtil,
time as timeUtil,
TribePropTypes,
} from '@moderntribe/common/utils';
import './style.pcss';
const TimePicker = ( {
allDay,
current,
disabled,
end,
onBlur,
onChange,
onClick,
onFocus,
showAllDay,
start,
step,
timeFormat,
} ) => {
const renderLabel = ( onAllDayClick ) => {
if ( allDay ) {
return (
<Button
className="tribe-editor__timepicker__all-day-btn"
disabled={ disabled }
onClick={ onAllDayClick }
>
{ __( 'All Day', 'tribe-common' ) }
</Button>
);
}
return (
<Input
className="tribe-editor__timepicker__input"
disabled={ disabled }
onBlur={ onBlur }
onChange={ onChange }
onFocus={ onFocus }
type="text"
value={ current }
/>
);
};
const renderToggle = ( { onToggle, isOpen } ) => (
<Fragment>
{ renderLabel( onToggle ) }
<Button
aria-expanded={ isOpen }
className="tribe-editor__timepicker__toggle-btn"
disabled={ disabled }
onClick={ onToggle }
>
<Dashicon
className="tribe-editor__timepicker__toggle-btn-icon"
icon={ isOpen ? 'arrow-up' : 'arrow-down' }
/>
</Button>
</Fragment>
);
const formatLabel = ( seconds ) => {
return momentUtil
.setTimeInSeconds( moment(), seconds )
.format( momentUtil.toFormat( timeFormat ) );
};
const getItems = () => {
const items = [];
const startSeconds = timeUtil.toSeconds( start, timeUtil.TIME_FORMAT_HH_MM );
const endSeconds = timeUtil.toSeconds( end, timeUtil.TIME_FORMAT_HH_MM );
const currentMoment = moment( current, momentUtil.TIME_FORMAT );
for ( let time = startSeconds; time <= endSeconds; time += step ) {
let isCurrent = false;
if ( currentMoment.isValid() ) {
const currentTime = momentUtil.toTime24Hr( currentMoment );
isCurrent = time === timeUtil.toSeconds( currentTime, timeUtil.TIME_FORMAT_HH_MM );
}
items.push( {
value: time,
text: formatLabel( time ),
isCurrent,
} );
}
return items;
};
const renderItem = ( item, onClose ) => {
const itemClasses = {
'tribe-editor__timepicker__item': true,
'tribe-editor__timepicker__item--current': item.isCurrent && ! allDay,
};
return (
<Button
key={ `time-${ item.value }` }
className={ classNames( itemClasses ) }
value={ item.value }
onClick={ () => onClick( item.value, onClose ) }
>
{ item.text }
</Button>
);
};
const renderContent = ( { onClose } ) => (
<ScrollTo>
{ () => (
<PreventBlockClose>
<ScrollArea
key="tribe-element-timepicker-items"
className="tribe-editor__timepicker__items"
>
{ showAllDay && renderItem(
{ text: __( 'All Day', 'tribe-common' ), value: 'all-day' },
onClose,
) }
{ getItems().map( ( item ) => renderItem( item, onClose ) ) }
</ScrollArea>
</PreventBlockClose>
) }
</ScrollTo>
);
return (
<div
key="tribe-element-timepicker"
className="tribe-editor__timepicker"
>
<Dropdown
className="tribe-editor__timepicker__toggle"
contentClassName="tribe-editor__timepicker__content"
position="bottom center"
renderToggle={ renderToggle }
renderContent={ renderContent }
/>
</div>
);
};
TimePicker.defaultProps = {
allDay: false,
onBlur: noop,
onChange: noop,
onClick: noop,
onFocus: noop,
step: timeUtil.HALF_HOUR_IN_SECONDS,
timeFormat: dateUtil.FORMATS.WP.time,
};
TimePicker.propTypes = {
/**
* TribePropTypes.timeFormat check for string formatted as a time
* using 24h clock in hh:mm format
* e.g. 00:24, 03:57, 21:12
*/
allDay: PropTypes.bool,
current: PropTypes.string,
disabled: PropTypes.bool,
end: TribePropTypes.timeFormat.isRequired,
onBlur: PropTypes.func,
onChange: PropTypes.func,
onClick: PropTypes.func,
onFocus: PropTypes.func,
showAllDay: PropTypes.bool,
start: TribePropTypes.timeFormat.isRequired,
step: PropTypes.number,
timeFormat: PropTypes.string,
};
export default TimePicker;