Skip to content

Commit 54e02c3

Browse files
Fix code review issues: proper size mapping and document DOM manipulation workaround
Co-authored-by: HenriqueLimas <2222191+HenriqueLimas@users.noreply.github.com>
1 parent 639eb5a commit 54e02c3

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

packages/ebayui-core-react/src/ebay-filter-input/filter-input.tsx

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
import classnames from "classnames";
3-
import React, { ComponentProps, FC } from "react";
3+
import React, { ComponentProps, FC, useRef } from "react";
44
import { EbayTextbox, EbayTextboxPrefixIcon, EbayTextboxPostfixIcon } from "../ebay-textbox";
55
import { EbayIconSearch16 } from "../ebay-icon/icons/ebay-icon-search-16";
66
import { EbayIconClear16 } from "../ebay-icon/icons/ebay-icon-clear-16";
@@ -59,25 +59,33 @@ const EbayFilterInput: FC<EbayFilterInputProps> = ({
5959
autoFocus,
6060
tabIndex
6161
}) => {
62+
const inputRef = useRef<HTMLInputElement>(null);
63+
64+
// Map filter-input sizes to textbox sizes
65+
// filter-input "small" -> textbox "default", filter-input "large" -> textbox "large"
66+
const textboxSize: TextboxSize | undefined = inputSize && validSizes.includes(inputSize)
67+
? (inputSize === "small" ? "default" : "large")
68+
: undefined;
6269

63-
const size = inputSize && validSizes.includes(inputSize) ? inputSize as TextboxSize : undefined;
64-
65-
const handleClear = (event: any) => {
66-
// Find the input element in the DOM (since ref may not be connected properly)
67-
const inputElement = (event.target as HTMLElement).closest('.textbox')?.querySelector('input') as HTMLInputElement;
70+
const handleButtonClick = (event: any) => {
71+
// Note: This uses DOM manipulation as a workaround because the textbox component
72+
// manages its own internal state and doesn't expose a proper clear API.
73+
// This is not ideal React code but is necessary for integration with the existing textbox.
74+
const inputElement = inputRef.current ||
75+
(event.target as HTMLElement).closest('.textbox')?.querySelector('input') as HTMLInputElement;
6876

6977
if (inputElement) {
70-
// Use React's synthetic event to properly trigger input change
78+
// Use React's property setter to update the value
7179
const nativeInputValueSetter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value')?.set;
7280
if (nativeInputValueSetter) {
7381
nativeInputValueSetter.call(inputElement, '');
7482

75-
// Create a proper input event
83+
// Dispatch input event to trigger React's onChange handlers
7684
const inputEvent = new Event('input', { bubbles: true });
7785
inputElement.dispatchEvent(inputEvent);
7886
}
7987

80-
// Also trigger our own clear event
88+
// Create synthetic event for the clear callback
8189
const syntheticEvent = {
8290
...event,
8391
target: inputElement,
@@ -88,15 +96,18 @@ const EbayFilterInput: FC<EbayFilterInputProps> = ({
8896
}
8997
};
9098

99+
100+
91101
const containerClassName = classnames(
92102
"filter-input",
93-
size && `filter-input--${size}`,
103+
inputSize && validSizes.includes(inputSize) && `filter-input--${inputSize}`,
94104
className
95105
);
96106

97107
return (
98108
<span className={containerClassName}>
99109
<EbayTextbox
110+
forwardedRef={inputRef}
100111
id={id}
101112
name={name}
102113
value={value}
@@ -113,21 +124,21 @@ const EbayFilterInput: FC<EbayFilterInputProps> = ({
113124
type="search"
114125
aria-controls={a11yControlsId}
115126
placeholder={placeholder}
116-
inputSize={size}
127+
inputSize={textboxSize}
117128
onKeyDown={onKeyDown}
118129
onKeyPress={onKeyPress}
119130
onKeyUp={onKeyUp}
120131
onChange={onChange}
121132
onInputChange={onInputChange}
122133
onFocus={onFocus}
123134
onBlur={onBlur}
135+
onButtonClick={a11yClearButton ? handleButtonClick : undefined}
124136
>
125137
<EbayTextboxPrefixIcon icon={<EbayIconSearch16 />} />
126138
{a11yClearButton && (
127139
<EbayTextboxPostfixIcon
128140
icon={<EbayIconClear16 />}
129141
buttonAriaLabel={a11yClearButton}
130-
onClick={handleClear}
131142
className="filter-input__clear-btn"
132143
/>
133144
)}

0 commit comments

Comments
 (0)