Skip to content
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

Add 'show in modal' option in date input #154

Merged
merged 1 commit into from
Jul 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/components/Calendar/CalendarDate/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

&.today {
color: var(--dui-color-accent);
border: var(--dui-width-separator-thin) solid var(--dui-color-separator);
font-weight: var(--dui-font-weight-bold);
}

Expand Down
56 changes: 28 additions & 28 deletions src/components/Calendar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,34 @@ function Calendar<P extends CalendarDateProps>(props: Props<P>) {
optionsPopupContentClassName={styles.popupContent}
/>
</div>
<div className={styles.actions}>
<Button
name={undefined}
variant="action"
title="Go to current year / month"
onClick={handleGotoCurrentButtonClick}
>
<IoTimeOutline />
</Button>
<Button
name={undefined}
variant="action"
onClick={handlePreviousMonthButtonClick}
title="Previous month"
disabled={isNotDefined(year)}
>
<IoChevronBack />
</Button>
<Button
name={undefined}
variant="action"
onClick={handleNextMonthButtonClick}
title="Next month"
disabled={isNotDefined(year)}
>
<IoChevronForward />
</Button>
</div>
</div>
<div className={styles.weekDays}>
{weekDayNames.map((wd) => (
Expand Down Expand Up @@ -260,34 +288,6 @@ function Calendar<P extends CalendarDateProps>(props: Props<P>) {
Please select a valid year and month to view the dates
</div>
)}
<div className={styles.actions}>
<Button
name={undefined}
variant="action"
title="Go to current year / month"
onClick={handleGotoCurrentButtonClick}
>
<IoTimeOutline />
</Button>
<Button
name={undefined}
variant="action"
onClick={handlePreviousMonthButtonClick}
title="Previous month"
disabled={isNotDefined(year)}
>
<IoChevronBack />
</Button>
<Button
name={undefined}
variant="action"
onClick={handleNextMonthButtonClick}
title="Next month"
disabled={isNotDefined(year)}
>
<IoChevronForward />
</Button>
</div>
</div>
);
}
Expand Down
62 changes: 58 additions & 4 deletions src/components/DateInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import {
import useUiModeClassName from '../../hooks/useUiModeClassName';
import useBlurEffect from '../../hooks/useBlurEffect';
import useBooleanState from '../../hooks/useBooleanState';
import useModalState from '../../hooks/useModalState';
import InputContainer, { Props as InputContainerProps } from '../InputContainer';
import RawInput, { Props as RawInputProps } from '../RawInput';
import Button from '../Button';
import Popup from '../Popup';
import Modal from '../Modal';
import Calendar, { Props as CalendarProps } from '../Calendar';
import { Props as CalendarDateProps } from '../Calendar/CalendarDate';
import { genericMemo, ymdToDateString } from '../../utils';
Expand All @@ -28,6 +30,7 @@ type InheritedProps<T extends NameType> = (Omit<InputContainerProps, 'input'> &
export interface Props<T extends NameType> extends InheritedProps<T> {
inputElementRef?: React.RefObject<HTMLInputElement>;
inputClassName?: string;
showInModal?: boolean;
}

function DateInput<T extends NameType>(props: Props<T>) {
Expand Down Expand Up @@ -55,6 +58,7 @@ function DateInput<T extends NameType>(props: Props<T>) {
onChange,
name,
value,
showInModal,
...dateInputProps
} = props;

Expand All @@ -72,6 +76,30 @@ function DateInput<T extends NameType>(props: Props<T>) {
toggleShowCalendar,
] = useBooleanState(false);

const [
dateModalShown,
setDateModalVisible,
setDateModalHidden,
] = useModalState(false);

const handleShowCalendar = React.useCallback(() => {
if (showInModal) {
setDateModalVisible();
}
toggleShowCalendar();
}, [
showInModal,
setDateModalVisible,
toggleShowCalendar,
]);

const handleFocus = React.useCallback(() => {
if (showInModal) {
setDateModalVisible();
}
setShowCalendarTrue();
}, [showInModal, setShowCalendarTrue, setDateModalVisible]);

const handlePopupBlur = React.useCallback(
(isClickedOnPopup: boolean, isClickedOnParent: boolean, e: MouseEvent) => {
const isClickedWithin = isClickedOnPopup || isClickedOnParent;
Expand Down Expand Up @@ -104,8 +132,17 @@ function DateInput<T extends NameType>(props: Props<T>) {
onChange(ymdToDateString(year, month, day), name);
}
setShowCalendarFalse();
if (showInModal) {
setDateModalHidden();
}
},
[name, onChange, setShowCalendarFalse],
[
name,
onChange,
setShowCalendarFalse,
setDateModalHidden,
showInModal,
],
);

const handleClearButtonClick = React.useCallback(() => {
Expand Down Expand Up @@ -138,7 +175,7 @@ function DateInput<T extends NameType>(props: Props<T>) {
<Button
name={undefined}
variant="action"
onClick={toggleShowCalendar}
onClick={handleShowCalendar}
title="Set current date"
disabled={disabled}
>
Expand Down Expand Up @@ -181,12 +218,12 @@ function DateInput<T extends NameType>(props: Props<T>) {
uiMode={uiMode}
disabled={disabled}
value={value}
onFocus={setShowCalendarTrue}
onFocus={handleFocus}
type="date"
/>
)}
/>
{!readOnly && (
{!readOnly && !showInModal && (
<Popup
parentRef={containerRef}
elementRef={popupRef}
Expand All @@ -204,6 +241,23 @@ function DateInput<T extends NameType>(props: Props<T>) {
/>
</Popup>
)}
{!readOnly && showInModal && dateModalShown && (
<Modal
onCloseButtonClick={setDateModalHidden}
className={styles.calendarModal}
spacing="compact"
size="free"
freeHeight
>
<Calendar
onDateClick={handleCalendarDateClick}
className={styles.calendar}
monthSelectionPopupClassName={calendarMonthSelectionPopupClassName}
initialDate={value ?? undefined}
activeDate={value ?? undefined}
/>
</Modal>
)}
</>
);
}
Expand Down
5 changes: 5 additions & 0 deletions src/components/DateInput/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,8 @@
}
}
}

.calendar-modal {
--padding: var(--dui-spacing-medium);
width: calc(var(--dui-width-calendar-date) * 7 + 2 * var(--padding));
}
5 changes: 5 additions & 0 deletions storybook/stories/DateInput.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ Default.args = {
label: 'Birthdate',
};

export const ShowInModal = Template.bind({});
ShowInModal.args = {
showInModal: true,
};

export const Disabled = Template.bind({});
Disabled.args = {
label: 'Birthdate',
Expand Down