Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added floating-ui hook usage for ComboBox #16585

Merged
merged 11 commits into from
Jun 11, 2024
15 changes: 15 additions & 0 deletions packages/react/src/components/ComboBox/ComboBox.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,21 @@ export const AllowCustomValue = () => {
</div>
);
};
export const ExperimentalAutoAlign = () => (
<div style={{ width: 400 }}>
<div style={{ height: 300 }}></div>
<ComboBox
onChange={() => {}}
id="carbon-combobox"
items={items}
itemToString={(item) => (item ? item.text : '')}
titleText="ComboBox title"
helperText="Combobox helper text"
autoAlign={true}
/>
<div style={{ height: 800 }}></div>
</div>
);

export const _WithLayer = () => (
<WithLayer>
Expand Down
42 changes: 41 additions & 1 deletion packages/react/src/components/ComboBox/ComboBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import mergeRefs from '../../tools/mergeRefs';
import deprecate from '../../prop-types/deprecate';
import { usePrefix } from '../../internal/usePrefix';
import { FormContext } from '../FluidForm';
import { useFloating, flip, autoUpdate } from '@floating-ui/react';

const {
InputBlur,
Expand Down Expand Up @@ -150,6 +151,11 @@ export interface ComboBoxProps<ItemType>
*/
ariaLabel?: string;

/**
* Will auto-align the dropdown on first render if it is not visible. This prop is currently experimental and is subject to future changes.
Gururajj77 marked this conversation as resolved.
Show resolved Hide resolved
*/
autoAlign?: boolean;

/**
* An optional className to add to the container node
*/
Expand Down Expand Up @@ -313,6 +319,7 @@ const ComboBox = forwardRef(
const {
['aria-label']: ariaLabel = 'Choose an item',
ariaLabel: deprecatedAriaLabel,
autoAlign = false,
className: containerClassName,
direction = 'bottom',
disabled = false,
Expand Down Expand Up @@ -342,6 +349,33 @@ const ComboBox = forwardRef(
slug,
...rest
} = props;
const { refs, floatingStyles } = useFloating(
autoAlign
? {
strategy: 'fixed',
Gururajj77 marked this conversation as resolved.
Show resolved Hide resolved
middleware: [
flip({
fallbackAxisSideDirection: 'none',
}),
Gururajj77 marked this conversation as resolved.
Show resolved Hide resolved
],
whileElementsMounted: autoUpdate,
}
: {}
);
const parentWidth = (refs?.reference?.current as HTMLElement)?.clientWidth;

useEffect(() => {
if (autoAlign) {
Object.keys(floatingStyles).forEach((style) => {
if (refs.floating.current) {
refs.floating.current.style[style] = floatingStyles[style];
}
});
if (parentWidth && refs.floating.current) {
refs.floating.current.style.width = parentWidth + 'px';
}
}
}, [autoAlign, floatingStyles, refs.floating, parentWidth]);
const prefix = usePrefix();
const { isFluid } = useContext(FormContext);
const textInput = useRef<HTMLInputElement>(null);
Expand Down Expand Up @@ -630,6 +664,7 @@ const ComboBox = forwardRef(
light={light}
size={size}
warn={warn}
ref={refs.setReference}
warnText={warnText}
warnTextId={warnTextId}>
<div className={`${prefix}--list-box__field`}>
Expand Down Expand Up @@ -739,7 +774,8 @@ const ComboBox = forwardRef(
<ListBox.Menu
{...getMenuProps({
'aria-label': deprecatedAriaLabel || ariaLabel,
})}>
})}
ref={mergeRefs(getMenuProps().ref, refs.setFloating)}>
{isOpen
? filterItems(items, itemToString, inputValue).map(
(item, index) => {
Expand Down Expand Up @@ -821,6 +857,10 @@ ComboBox.propTypes = {
PropTypes.string,
'This prop syntax has been deprecated. Please use the new `aria-label`.'
),
/**
* Will auto-align the dropdown on first render if it is not visible. This prop is currently experimental and is subject to future changes.
Gururajj77 marked this conversation as resolved.
Show resolved Hide resolved
*/
autoAlign: PropTypes.bool,

/**
* An optional className to add to the container node
Expand Down
Loading