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

fix: trigger remove chip event when the chip is visible #7193

Merged
merged 3 commits into from
Oct 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions components/lib/multiselect/MultiSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ export const MultiSelect = React.memo(
const firstHiddenFocusableElementOnOverlay = React.useRef(null);
const lastHiddenFocusableElementOnOverlay = React.useRef(null);
const inputRef = React.useRef(props.inputRef);
const labelRef = React.useRef(null);
const labelContainerRef = React.useRef(null);
const overlayRef = React.useRef(null);
const labelRef = React.useRef(null);
const hasFilter = filterState && filterState.trim().length > 0;
const empty = ObjectUtils.isEmpty(props.value);
const equalityKey = props.optionValue ? null : props.dataKey;
Expand Down Expand Up @@ -518,7 +519,7 @@ export const MultiSelect = React.memo(
};

const alignOverlay = () => {
DomHandler.alignOverlay(overlayRef.current, labelRef.current.parentElement, props.appendTo || (context && context.appendTo) || PrimeReact.appendTo);
DomHandler.alignOverlay(overlayRef.current, labelContainerRef.current.parentElement, props.appendTo || (context && context.appendTo) || PrimeReact.appendTo);
};

const isClearClicked = (event) => {
Expand Down Expand Up @@ -812,6 +813,8 @@ export const MultiSelect = React.memo(
const removeChip = (event, item) => {
event.stopPropagation();

if (!isVisible(event.currentTarget)) return;

const value = props.value.filter((val) => !ObjectUtils.equals(val, item, equalityKey));

if (props.onRemove) {
Expand All @@ -824,6 +827,22 @@ export const MultiSelect = React.memo(
updateModel(event, value, item);
};

const isVisible = (element) => {
const parentElement = labelRef.current;
const isOverflow = parentElement.clientWidth < parentElement.scrollWidth;

if (!isOverflow) return true;

const target = element.closest('[data-pc-section="token"]');
const parentStyles = window.getComputedStyle(parentElement);
const targetStyles = window.getComputedStyle(target);

const parentWidth = parentElement.clientWidth - parseFloat(parentStyles.paddingLeft) - parseFloat(parentStyles.paddingRight);
const targetRight = target.getBoundingClientRect().right + parseFloat(targetStyles.marginRight) - parentElement.getBoundingClientRect().left;

return targetRight <= parentWidth;
};

const getSelectedItemsLabel = () => {
const pattern = /{(.*?)}/;
const selectedItemsLabel = props.selectedItemsLabel || localeOption('selectionMessage');
Expand Down Expand Up @@ -1001,14 +1020,15 @@ export const MultiSelect = React.memo(

const labelContainerProps = mergeProps(
{
ref: labelRef,
ref: labelContainerRef,
className: cx('labelContainer')
},
ptm('labelContainer')
);

const labelProps = mergeProps(
{
ref: labelRef,
className: cx('label', { empty })
},
ptm('label')
Expand Down
Loading