From 6203ab3c0651ad389fb774966b39aa87e1517e4f Mon Sep 17 00:00:00 2001 From: Jan-Gerke Salomon Date: Thu, 25 Jan 2024 17:09:47 +0800 Subject: [PATCH] feat: add icon-button that opens the picker widget --- .../src/calendar-input/calendar-input.js | 120 ++++++++++-------- .../src/stories/calendar-input.stories.js | 1 + 2 files changed, 65 insertions(+), 56 deletions(-) diff --git a/components/calendar/src/calendar-input/calendar-input.js b/components/calendar/src/calendar-input/calendar-input.js index ec89673283..106b19e0ac 100644 --- a/components/calendar/src/calendar-input/calendar-input.js +++ b/components/calendar/src/calendar-input/calendar-input.js @@ -4,6 +4,7 @@ import { InputField, InputFieldProps } from '@dhis2-ui/input' import { Layer } from '@dhis2-ui/layer' import { Popper } from '@dhis2-ui/popper' import { useDatePicker } from '@dhis2/multi-calendar-dates' +import { IconCheckmark24 } from '@dhis2/ui-icons' import cx from 'classnames' import React, { useCallback, useRef, useState } from 'react' import { Calendar, CalendarProps } from '../calendar/calendar.js' @@ -50,72 +51,45 @@ export const CalendarInput = ({ const ref = useRef() const [open, setOpen] = useState(false) const [tempInputValue, _setTempInputValue] = useState(date) - - const setTempInputValue = (nextTempValue) => { + const currentValidDate = validateInput(tempInputValue) + ? tempInputValue + : date + const { calendarWeekDays } = useDatePicker({ + date: currentValidDate, + options: { calendar }, + }) + + const setTempInputValue = useCallback((nextTempValue) => { _setTempInputValue(nextTempValue) const isValidInputDate = validateInput(nextTempValue) if (isValidInputDate) { - const [year, month, day] = nextTempValue.split('-') - const nextDate = Temporal.PlainDate.from({ - calendar, - year, - month, - day, - }) - + const calendarDate = searchCalendarWeekDays(nextTempValue, calendarWeekDays) onDateSelect({ - calendarDate: nextDate, + calendarDate, calendarDateString: nextTempValue, }) } - } + }, [onDateSelect, calendarWeekDays]) - const calendarProps = React.useMemo(() => { - const onDateSelectWrapper = (selectedDate) => { - setOpen(false) - setTempInputValue(selectedDate.calendarDateString) - } - - return { - onDateSelect: onDateSelectWrapper, - calendar, - date, - dir, - locale, - numberingSystem, - weekDayFormat, - timeZone, - width, - cellSize, - } - }, [ - calendar, - cellSize, - date, - dir, - locale, - numberingSystem, - timeZone, - weekDayFormat, - width, - ]) - - const onFocus = () => { - setOpen(true) - } + const onDateSelectWrapper = useCallback((selectedDate) => { + setOpen(false) + setTempInputValue(selectedDate?.calendarDateString || '') + }, [setTempInputValue]) return ( <>
- setTempInputValue(value)} - /> +
+ setTempInputValue(value)} + /> +
+ {clearable && (
calendarProps.onDateSelect(null)} + onClick={() => { + onDateSelectWrapper(null) + }} type="button" > {i18n.t('Clear')}
)} + +
+
{open && ( - + @@ -164,19 +158,33 @@ export const CalendarInput = ({ {` .calendar-input-wrapper { position: relative; + display: flex; + gap: 8px; + } + + .input { + flex-grow: 1; } + .calendar-clear-button { position: absolute; - inset-inline-end: 6px; + inset-inline-end: 51px; inset-block-start: 27px; } .calendar-clear-button.with-icon { inset-inline-end: 36px; + } + .calendar-clear-button.with-dense-wrapper { inset-block-start: 23px; } + + .open-calendar-widget { + padding-top: 22px; + flex-shrink: 1; + } `} diff --git a/components/calendar/src/stories/calendar-input.stories.js b/components/calendar/src/stories/calendar-input.stories.js index 13cc451670..801b67014a 100644 --- a/components/calendar/src/stories/calendar-input.stories.js +++ b/components/calendar/src/stories/calendar-input.stories.js @@ -27,6 +27,7 @@ const buildCalendar = () => (