Skip to content

Commit 64f885c

Browse files
committed
feat: add support for blur in input, text area and select on esc
1 parent 4172a48 commit 64f885c

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

src/Shared/Components/CustomInput/CustomInput.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ const CustomInput = ({
8181
endIconButtonConfig?.onClick(event)
8282
}
8383

84+
const handleKeyDown: CustomInputProps['onKeyDown'] = (e) => {
85+
if (e.key === 'Escape') {
86+
inputRef.current.blur()
87+
}
88+
89+
props.onKeyDown?.(e)
90+
}
91+
8492
return (
8593
<FormFieldWrapper
8694
inputId={name}
@@ -115,6 +123,7 @@ const CustomInput = ({
115123
data-testid={name}
116124
required={required}
117125
onBlur={handleBlur}
126+
onKeyDown={handleKeyDown}
118127
type={type}
119128
ref={inputRef}
120129
className={`${COMPONENT_SIZE_TYPE_TO_FONT_AND_BLOCK_PADDING_MAP[size]} ${COMPONENT_SIZE_TYPE_TO_INLINE_PADDING_MAP[size]} ${deriveBorderRadiusAndBorderClassFromConfig({ borderConfig, borderRadiusConfig })} ${endIconButtonConfig ? `custom-input__with-icon-button--${size}` : ''} w-100 dc__overflow-auto`}

src/Shared/Components/SelectPicker/SelectPicker.component.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
Props as ReactSelectProps,
2323
} from 'react-select'
2424
import CreatableSelect from 'react-select/creatable'
25-
import { ReactElement, useCallback, useMemo, useState } from 'react'
25+
import { ReactElement, useCallback, useMemo, useRef, useState } from 'react'
2626
import { ComponentSizeType } from '@Shared/constants'
2727
import { ConditionalWrap } from '@Common/Helper'
2828
import Tippy from '@tippyjs/react'
@@ -202,7 +202,7 @@ const SelectPicker = <OptionValue, IsMulti extends boolean>({
202202
classNamePrefix,
203203
shouldRenderCustomOptions = false,
204204
isSearchable,
205-
selectRef,
205+
selectRef: refFromConsumer,
206206
shouldMenuAlignRight = false,
207207
fullWidth = false,
208208
customSelectedOptionsCount = null,
@@ -223,6 +223,9 @@ const SelectPicker = <OptionValue, IsMulti extends boolean>({
223223
labelTooltipConfig,
224224
...props
225225
}: SelectPickerProps<OptionValue, IsMulti>) => {
226+
const innerRef = useRef<SelectPickerProps<OptionValue, IsMulti>['selectRef']['current']>(null)
227+
const selectRef = refFromConsumer ?? innerRef
228+
226229
const [isFocussed, setIsFocussed] = useState(false)
227230
const [inputValue, setInputValue] = useState('')
228231

@@ -356,6 +359,11 @@ const SelectPicker = <OptionValue, IsMulti extends boolean>({
356359
if ((e.metaKey || e.ctrlKey) && e.key === 'Enter') {
357360
e.preventDefault()
358361
}
362+
363+
if (e.key === 'Escape' && !selectRef.current.props.menuIsOpen) {
364+
selectRef.current.blur()
365+
}
366+
359367
onKeyDown?.(e)
360368
}
361369

src/Shared/Components/Textarea/Textarea.component.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ const Textarea = ({
114114
) => {
115115
if (event.key === 'Enter' || event.key === 'Escape') {
116116
event.stopPropagation()
117+
118+
if (event.key === 'Escape') {
119+
textareaRef.current.blur()
120+
}
117121
}
118122
}
119123

0 commit comments

Comments
 (0)