Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

DatePickerInput improvements #2460

Merged
merged 5 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@

### Dependency updates

## [17.0.5] - 2022-11-22
qubis741 marked this conversation as resolved.
Show resolved Hide resolved

### Changed

- `DatePickerInput`: Used proper cursor and added ability to close DatePicker on ESC and ENTER key ([@qubis741](https://github.com/qubis741)) in ([#2460](https://github.com/teamleadercrm/ui/pull/2460))

## [17.0.4] - 2022-11-22

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@teamleader/ui",
"description": "Teamleader UI library",
"version": "17.0.4",
"version": "17.0.5",
"author": "Teamleader <[email protected]>",
"bugs": {
"url": "https://github.com/teamleadercrm/ui/issues"
Expand Down
19 changes: 15 additions & 4 deletions src/components/datepicker/DatePickerInput.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { IconCalendarSmallOutline, IconCloseBadgedSmallFilled } from '@teamleader/ui-icons';
import React, { ReactNode, useCallback, useEffect, useState } from 'react';
import cx from 'classnames';
import React, { KeyboardEvent, ReactNode, useCallback, useEffect, useRef, useState } from 'react';
import { DayPickerProps as ReactDayPickerProps, Modifier } from 'react-day-picker';
import DatePicker from '.';
import { SIZES } from '../../constants';
import { KEY, SIZES } from '../../constants';
import Box, { pickBoxProps } from '../box';
import { BoxProps } from '../box/Box';
import Icon from '../icon';
Expand Down Expand Up @@ -97,7 +98,7 @@ function DatePickerInput<IsTypeable extends boolean = true>({

return customFormatDate(date, locale);
};

const inputRef = useRef<HTMLInputElement>(null);
const [isPopoverActive, setIsPopoverActive] = useState(false);
const [popoverAnchorEl, setPopoverAnchorEl] = useState<Element | null>(null);
const [selectedDate, setSelectedDate] = useState<Date | undefined>(
Expand Down Expand Up @@ -242,11 +243,20 @@ function DatePickerInput<IsTypeable extends boolean = true>({
) : null;
};

const handleKeyDown = (event: KeyboardEvent) => {
if (event.key === KEY.Escape || event.key === KEY.Enter) {
event.stopPropagation();
inputRef.current?.blur();
handlePopoverClose();
}
};

const boxProps = pickBoxProps(others);
const internalError = displayError ? errorText || true : false;
return (
<Box className={className} {...boxProps}>
<Input
ref={inputRef}
inverse={inverse}
prefix={renderIcon()}
suffix={renderClearIcon()}
Expand All @@ -258,9 +268,10 @@ function DatePickerInput<IsTypeable extends boolean = true>({
onClick={handleInputClick}
onFocus={handleInputFocus}
onBlur={handleInputBlur}
className={theme['date-picker-input']}
className={cx({ [theme['date-picker-input__non-typeable']]: !typeable })}
value={inputValue}
onChange={handleInputChange}
onKeyDown={handleKeyDown}
/>
<Popover
active={isPopoverActive}
Expand Down
2 changes: 1 addition & 1 deletion src/components/datepicker/theme.css
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
.date-picker {
display: flex;

&-input {
&-input__non-typeable {
input {
cursor: default;
}
Expand Down