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

Commit

Permalink
Merge pull request #2460 from teamleadercrm/FRAF-1154-v5
Browse files Browse the repository at this point in the history
DatePickerInput improvements
  • Loading branch information
qubis741 authored Nov 22, 2022
2 parents c1e31c8 + f0a3c62 commit 06d9338
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
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.1.0] - 2022-11-22

### 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 <development@teamleader.eu>",
"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
1 change: 1 addition & 0 deletions src/components/datepicker/datePickerInput.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export const inputSingleDateWithoutTyping: ComponentStory<typeof DatePickerInput
size="medium"
onChange={handleOnChange}
selectedDate={preSelectedDate}
typeable={false}
/>
);
};
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

0 comments on commit 06d9338

Please sign in to comment.