-
-
Notifications
You must be signed in to change notification settings - Fork 875
[WIP] Add timezone selector to DateTimeWidget for event start and end #8304
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,12 +5,23 @@ import loadable from '@loadable/component'; | |
| import cx from 'classnames'; | ||
| import Icon from '@plone/volto/components/theme/Icon/Icon'; | ||
| import FormFieldWrapper from '@plone/volto/components/manage/Widgets/FormFieldWrapper'; | ||
| import { parseDateTime, toBackendLang } from '@plone/volto/helpers/Utils/Utils'; | ||
| import { | ||
| parseDateTime, | ||
| toBackendLang, | ||
| getTimeZoneOffset, | ||
| } from '@plone/volto/helpers/Utils/Utils'; | ||
| import { injectLazyLibs } from '@plone/volto/helpers/Loadable/Loadable'; | ||
| import { | ||
| customSelectStyles, | ||
| DropdownIndicator, | ||
| Option, | ||
| selectTheme, | ||
| } from '@plone/volto/components/manage/Widgets/SelectStyling'; | ||
|
|
||
| import leftKey from '@plone/volto/icons/left-key.svg'; | ||
| import rightKey from '@plone/volto/icons/right-key.svg'; | ||
| import clearSVG from '@plone/volto/icons/clear.svg'; | ||
| import clockSVG from '@plone/volto/icons/clock.svg'; | ||
|
|
||
| import 'rc-time-picker/assets/index.css'; | ||
| import 'react-dates/initialize'; | ||
|
|
@@ -27,6 +38,14 @@ const messages = defineMessages({ | |
| id: 'Time', | ||
| defaultMessage: 'Time', | ||
| }, | ||
| timezone: { | ||
| id: 'timezone', | ||
| defaultMessage: 'Time zone', | ||
| }, | ||
| editTimezone: { | ||
| id: 'editTimezone', | ||
| defaultMessage: 'Edit time zone', | ||
| }, | ||
| clearDateTime: { | ||
| id: 'Clear date/time', | ||
| defaultMessage: 'Clear date and time', | ||
|
|
@@ -71,6 +90,52 @@ const defaultTimeDateOnly = { | |
| second: 0, | ||
| }; | ||
|
|
||
| const TimezoneSelector = injectLazyLibs(['reactSelect'])(({ | ||
| value, | ||
| onChange, | ||
| reactSelect, | ||
| }) => { | ||
| const intl = useIntl(); | ||
| const [open, setOpen] = useState(false); | ||
| const Select = reactSelect.default; | ||
|
|
||
| return ( | ||
| <div className="date-time-widget-timezone"> | ||
| <button | ||
| type="button" | ||
| aria-label={intl.formatMessage(messages.editTimezone)} | ||
| onClick={() => setOpen(!open)} | ||
| > | ||
| <Icon name={clockSVG} size="16px" /> {open ? null : value} | ||
| </button> | ||
| {open ? ( | ||
| <div className="date-time-widget-timezone-selector"> | ||
| <Select | ||
| placeholder={intl.formatMessage(messages.editTimezone)} | ||
| // eslint-disable-next-line jsx-a11y/no-autofocus | ||
| autoFocus | ||
| value={{ value, label: value }} | ||
| onChange={(option) => { | ||
| onChange(option.value); | ||
| setOpen(false); | ||
| }} | ||
| onBlur={() => setOpen(false)} | ||
| options={Intl.supportedValuesOf('timeZone').map((tz) => ({ | ||
| value: tz, | ||
| label: tz, | ||
| }))} | ||
| styles={customSelectStyles} | ||
| theme={selectTheme} | ||
| components={{ | ||
| DropdownIndicator, | ||
| Option, | ||
| }} | ||
| /> | ||
| </div> | ||
| ) : null} | ||
| </div> | ||
| ); | ||
| }); | ||
| const DatetimeWidgetComponent = (props) => { | ||
| const { | ||
| id, | ||
|
|
@@ -101,20 +166,30 @@ const DatetimeWidgetComponent = (props) => { | |
|
|
||
| const renderWidget = !(id === 'end' && formData?.open_end); | ||
|
|
||
| const localTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was chewing on the I think the timezone setting in the |
||
| const selectedTimezone = formData?.[id + '_timezone'] || localTimezone; | ||
|
|
||
| useEffect(() => { | ||
| const parsedDateTime = parseDateTime( | ||
| toBackendLang(lang), | ||
| value, | ||
| undefined, | ||
| moment.default, | ||
| selectedTimezone, | ||
| ); | ||
| setIsDefault( | ||
| parsedDateTime?.toISOString() === moment.default().utc().toISOString(), | ||
| ); | ||
| }, [value, lang, moment]); | ||
| }, [value, lang, moment, selectedTimezone]); | ||
|
|
||
| const getInternalValue = () => { | ||
| return parseDateTime(toBackendLang(lang), value, undefined, moment.default); | ||
| return parseDateTime( | ||
| toBackendLang(lang), | ||
| value, | ||
| undefined, | ||
| moment.default, | ||
| selectedTimezone, | ||
| ); | ||
| }; | ||
|
|
||
| const getDateOnly = () => { | ||
|
|
@@ -154,6 +229,18 @@ const DatetimeWidgetComponent = (props) => { | |
| } | ||
| }; | ||
|
|
||
| const onTimezoneChange = (timezone) => { | ||
| if (timezone) { | ||
| let value = getInternalValue(); | ||
| value = value.utcOffset( | ||
| getTimeZoneOffset(value.toDate(), timezone), | ||
| true, | ||
| ); | ||
| onChange(id, value.toISOString()); | ||
| onChange(id + '_timezone', timezone); | ||
| } | ||
| }; | ||
|
|
||
| const onResetDates = () => { | ||
| setIsDefault(false); | ||
| onChange(id, null); | ||
|
|
@@ -198,68 +285,76 @@ const DatetimeWidgetComponent = (props) => { | |
| return ( | ||
| <FormFieldWrapper {...props}> | ||
| {renderWidget && ( | ||
| <div className="date-time-widget-wrapper"> | ||
| <div | ||
| className={cx('ui input date-input', { | ||
| 'default-date': isDefault, | ||
| })} | ||
| > | ||
| <SingleDatePicker | ||
| date={datetime} | ||
| disabled={isDisabled} | ||
| onDateChange={onDateChange} | ||
| focused={focused} | ||
| numberOfMonths={1} | ||
| {...(noPastDates ? {} : { isOutsideRange: () => false })} | ||
| onFocusChange={onFocusChange} | ||
| noBorder | ||
| required={required} | ||
| displayFormat={moment.default | ||
| .localeData(toBackendLang(lang)) | ||
| .longDateFormat('L')} | ||
| navPrev={<PrevIcon />} | ||
| navNext={<NextIcon />} | ||
| id={`${id}-date`} | ||
| placeholder={intl.formatMessage(messages.date)} | ||
| /> | ||
| </div> | ||
| {!isDateOnly && ( | ||
| <> | ||
| <div className="date-time-widget-wrapper"> | ||
| <div | ||
| ref={timeInputRef} | ||
| className={cx('ui input time-input', { | ||
| className={cx('ui input date-input', { | ||
| 'default-date': isDefault, | ||
| })} | ||
| > | ||
| <TimePicker | ||
| <SingleDatePicker | ||
| date={datetime} | ||
| disabled={isDisabled} | ||
| defaultValue={datetime} | ||
| value={datetime} | ||
| onChange={onTimeChange} | ||
| allowEmpty={false} | ||
| showSecond={false} | ||
| use12Hours={lang === 'en'} | ||
| id={`${id}-time`} | ||
| format={moment.default | ||
| onDateChange={onDateChange} | ||
| focused={focused} | ||
| numberOfMonths={1} | ||
| {...(noPastDates ? {} : { isOutsideRange: () => false })} | ||
| onFocusChange={onFocusChange} | ||
| noBorder | ||
| required={required} | ||
| displayFormat={moment.default | ||
| .localeData(toBackendLang(lang)) | ||
| .longDateFormat('LT')} | ||
| placeholder={intl.formatMessage(messages.time)} | ||
| focusOnOpen | ||
| placement="bottomRight" | ||
| .longDateFormat('L')} | ||
| navPrev={<PrevIcon />} | ||
| navNext={<NextIcon />} | ||
| id={`${id}-date`} | ||
| placeholder={intl.formatMessage(messages.date)} | ||
| /> | ||
| </div> | ||
| )} | ||
| {resettable && ( | ||
| <button | ||
| type="button" | ||
| disabled={isDisabled || !datetime} | ||
| onClick={onResetDates} | ||
| className="item ui noborder button" | ||
| aria-label={intl.formatMessage(messages.clearDateTime)} | ||
| > | ||
| <Icon name={clearSVG} size="24px" className="close" /> | ||
| </button> | ||
| )} | ||
| </div> | ||
| {!isDateOnly && ( | ||
| <div | ||
| ref={timeInputRef} | ||
| className={cx('ui input time-input', { | ||
| 'default-date': isDefault, | ||
| })} | ||
| > | ||
| <TimePicker | ||
| disabled={isDisabled} | ||
| defaultValue={datetime} | ||
| value={datetime} | ||
| onChange={onTimeChange} | ||
| allowEmpty={false} | ||
| showSecond={false} | ||
| use12Hours={lang === 'en'} | ||
| id={`${id}-time`} | ||
| format={moment.default | ||
| .localeData(toBackendLang(lang)) | ||
| .longDateFormat('LT')} | ||
| placeholder={intl.formatMessage(messages.time)} | ||
| focusOnOpen | ||
| placement="bottomRight" | ||
| /> | ||
| </div> | ||
| )} | ||
| {resettable && ( | ||
| <button | ||
| type="button" | ||
| disabled={isDisabled || !datetime} | ||
| onClick={onResetDates} | ||
| className="item ui noborder button" | ||
| aria-label={intl.formatMessage(messages.clearDateTime)} | ||
| > | ||
| <Icon name={clearSVG} size="24px" className="close" /> | ||
| </button> | ||
| )} | ||
| </div> | ||
| {id === 'start' || id === 'end' ? ( | ||
| <TimezoneSelector | ||
| value={selectedTimezone} | ||
| onChange={onTimezoneChange} | ||
| /> | ||
| ) : null} | ||
| </> | ||
| )} | ||
| </FormFieldWrapper> | ||
| ); | ||
|
|
@@ -289,6 +384,6 @@ DatetimeWidgetComponent.defaultProps = { | |
| resettable: true, | ||
| }; | ||
|
|
||
| export default injectLazyLibs(['reactDates', 'moment'])( | ||
| export default injectLazyLibs(['reactDates', 'reactSelect', 'moment'])( | ||
| DatetimeWidgetComponent, | ||
| ); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the future: we should translate this - TIL from Gemini - using CLDR: https://en.wikipedia.org/wiki/Common_Locale_Data_Repository
For example "Europe/Vienna" -> "Mitteleuropäische Zeit - Wien (GMT+2)".
An example Python Code:
However, I'd still like to find timezones by it's English identifier, as I'm used to do so.
But this is definitely for a future improvement. I'm just mentioning this here.