| 1 |
import { |
| 2 |
BaseControl, |
| 3 |
Button, |
| 4 |
DateTimePicker as WPDateTimePicker, |
| 5 |
Popover, |
| 6 |
} from '@wordpress/components'; |
| 7 |
import { dateI18n, getSettings } from '@wordpress/date'; |
| 8 |
import { useState } from '@wordpress/element'; |
| 9 |
import { __ } from '@wordpress/i18n'; |
| 10 |
|
| 11 |
const { luxon } = window; |
| 12 |
|
| 13 |
/** |
| 14 |
* Component Class |
| 15 |
* |
| 16 |
* @param props |
| 17 |
*/ |
| 18 |
export default function DateTimePicker(props) { |
| 19 |
const [isPickerOpen, setIsPickerOpen] = useState(false); |
| 20 |
|
| 21 |
const { value, onChange, label, is12Hour = false } = props; |
| 22 |
|
| 23 |
const settings = getSettings(); |
| 24 |
const resolvedFormat = settings.formats.datetime || 'F j, Y g:i a'; |
| 25 |
|
| 26 |
return ( |
| 27 |
<BaseControl |
| 28 |
id={label} |
| 29 |
label={label} |
| 30 |
className="ghostkit-components-date-time-picker" |
| 31 |
> |
| 32 |
<div> |
| 33 |
<Button isLink onClick={() => setIsPickerOpen(!isPickerOpen)}> |
| 34 |
{value |
| 35 |
? `${dateI18n(resolvedFormat, value)} ${ |
| 36 |
settings.timezone.abbr || |
| 37 |
settings.timezone.string || |
| 38 |
`UTC${settings.timezone.offset}` |
| 39 |
}` |
| 40 |
: __('Select Date', 'ghostkit')} |
| 41 |
</Button> |
| 42 |
{isPickerOpen ? ( |
| 43 |
<Popover |
| 44 |
className="ghostkit-components-date-time-picker-popover" |
| 45 |
onClose={() => setIsPickerOpen(false)} |
| 46 |
> |
| 47 |
<WPDateTimePicker |
| 48 |
label={label} |
| 49 |
currentDate={ |
| 50 |
luxon.DateTime.fromISO(value).isValid |
| 51 |
? value |
| 52 |
: '' |
| 53 |
} |
| 54 |
onChange={onChange} |
| 55 |
is12Hour={is12Hour} |
| 56 |
/> |
| 57 |
</Popover> |
| 58 |
) : null} |
| 59 |
</div> |
| 60 |
</BaseControl> |
| 61 |
); |
| 62 |
} |
| 63 |
|