Skip to content

Commit

Permalink
Merge pull request #26 from miksrv/develop
Browse files Browse the repository at this point in the history
Added Keyboard 'Escape' event for Dropdown and MultiSelect UI Components
  • Loading branch information
miksrv authored Nov 12, 2024
2 parents 12b084a + a72b57f commit e18316d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# simple-react-ui-kit

## 1.3.3

### Patch Changes

- Added Keyboard 'Escape' event for Dropdown and MultiSelect UI Components

## 1.3.2

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simple-react-ui-kit",
"version": "1.3.2",
"version": "1.3.3",
"description": "A lightweight and flexible UI framework for building responsive React applications with customizable components.",
"repository": "https://github.com/miksrv/simple-react-ui-kit.git",
"scripts": {
Expand Down
7 changes: 6 additions & 1 deletion src/components/dropdown/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ const Dropdown = <T,>({
}
}

const handleSearchKeyPress = (event: React.KeyboardEvent<HTMLInputElement>) => {
const handleSearchKeyPress = (event: React.KeyboardEvent<HTMLInputElement | HTMLButtonElement>) => {
if (
event.key === 'Enter' &&
filteredOptions?.length &&
Expand All @@ -123,6 +123,10 @@ const Dropdown = <T,>({
setSelectedOption(undefined)
onSelect?.(undefined)
}

if (event.key === 'Escape' && isOpen) {
setIsOpen(false)
}
}

const handleSearchChange = (event: React.ChangeEvent<HTMLInputElement>) => {
Expand Down Expand Up @@ -206,6 +210,7 @@ const Dropdown = <T,>({
variant={error ? 'negative' : undefined}
disabled={disabled}
onClick={toggleDropdown}
onKeyDown={handleSearchKeyPress}
className={cn(styles.dropdownButton, selectedOption && styles.selected, isOpen && styles.open)}
>
<span className={styles.value}>
Expand Down
12 changes: 11 additions & 1 deletion src/components/multiselect/MultiSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ const MultiSelect = <T,>({
setSelectedOption(selected)
onSelect?.(selected)
}

if (event.key === 'Escape' && isOpen) {
setIsOpen(false)
}
}

const handleSearchChange = (event: React.ChangeEvent<HTMLInputElement>) => {
Expand Down Expand Up @@ -152,6 +156,12 @@ const MultiSelect = <T,>({
}
}

const handleToggleDropdownClick = (event: React.KeyboardEvent<HTMLSpanElement>) => {
if (event.key === 'Enter' || event.key === 'Escape') {
toggleDropdown()
}
}

useEffect(() => {
document.addEventListener('mousedown', handleClickOutside)

Expand Down Expand Up @@ -218,7 +228,7 @@ const MultiSelect = <T,>({
tabIndex={0}
className={styles.toggleButton}
onClick={toggleDropdown}
onKeyDown={(e) => e.key === 'Enter' && toggleDropdown()}
onKeyDown={handleToggleDropdownClick}
>
{isOpen ? <Icon name={'KeyboardUp'} /> : <Icon name={'KeyboardDown'} />}
</span>
Expand Down

0 comments on commit e18316d

Please sign in to comment.