Skip to content

Commit

Permalink
Merge pull request #2553 from adevinta/fix-combobox-input-blur
Browse files Browse the repository at this point in the history
fix(combobox): fuse event callback with downshift on combobox input
  • Loading branch information
Powerplex authored Jan 15, 2025
2 parents e959528 + 74733d6 commit 6da946d
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions packages/components/combobox/src/ComboboxInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,27 @@ export const Input = ({

const hasPlaceholder = ctx.multiple ? ctx.selectedItems.length === 0 : ctx.selectedItem === null

function mergeHandlers<T extends React.SyntheticEvent>(
handlerA?: (event: T) => void,
handlerB?: (event: T) => void
) {
return (event: T) => {
handlerA?.(event)
handlerB?.(event)
}
}

/**
* Downshift has its own callbacks set for a few events types.
* We must merge the event handlers with the (optional) forwarded props if consumer wish to use the same events for alernate purposes (ex: tracking)
*/
const mergedEventProps = {
onBlur: mergeHandlers(props.onBlur, downshiftInputProps.onBlur),
onChange: mergeHandlers(props.onChange, downshiftInputProps.onChange),
onClick: mergeHandlers(props.onClick, downshiftInputProps.onClick),
onKeyDown: mergeHandlers(props.onKeyDown, downshiftInputProps.onKeyDown),
}

return (
<>
{ariaLabel && (
Expand All @@ -97,6 +118,7 @@ export const Input = ({
)}
{...props}
{...downshiftInputProps}
{...mergedEventProps}
value={ctx.inputValue}
aria-label={ariaLabel}
disabled={ctx.disabled}
Expand Down

0 comments on commit 6da946d

Please sign in to comment.