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 #6124 : Dropdown navigation like PrimeVue #6430

Merged
21 changes: 14 additions & 7 deletions components/lib/dropdown/Dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ export const Dropdown = React.memo(
when: overlayVisibleState
});

const flatedGroupOptions = props.optionGroupLabel ? props.options.flatMap((item) => item.items) : null
melloware marked this conversation as resolved.
Show resolved Hide resolved

const getVisibleOptions = () => {
if (hasFilter && !isLazy) {
const filterValue = filterState.trim().toLocaleLowerCase(props.filterLocale);
Expand Down Expand Up @@ -298,7 +300,8 @@ export const Dropdown = React.memo(
};

const findSelectedOptionIndex = () => {
return hasSelectedOption ? visibleOptions.findIndex((option) => isValidSelectedOption(option)) : -1;
const appropriateOptions = flatedGroupOptions ?? visibleOptions
return hasSelectedOption ? appropriateOptions.findIndex((option) => isValidSelectedOption(option)) : -1;
};

const findFirstFocusedOptionIndex = () => {
Expand Down Expand Up @@ -352,22 +355,26 @@ export const Dropdown = React.memo(
return selectedIndex < 0 ? findLastOptionIndex() : selectedIndex;
};


const appropriateOptions = () => flatedGroupOptions ?? visibleOptions
melloware marked this conversation as resolved.
Show resolved Hide resolved

const findFirstOptionIndex = () => {
return visibleOptions.findIndex((option) => isValidOption(option));
return appropriateOptions().findIndex((option) => isValidOption(option));
};

const findLastOptionIndex = () => {
return ObjectUtils.findLastIndex(visibleOptions, (option) => isValidOption(option));
return ObjectUtils.findLastIndex(appropriateOptions(), (option) => isValidOption(option));
};

const findNextOptionIndex = (index) => {
const matchedOptionIndex = index < visibleOptions.length - 1 ? visibleOptions.slice(index + 1).findIndex((option) => isValidOption(option)) : -1;
const options = appropriateOptions()
const matchedOptionIndex = index < options.length - 1 ? options.slice(index + 1).findIndex((option) => isValidOption(option)) : -1;

return matchedOptionIndex > -1 ? matchedOptionIndex + index + 1 : index;
};

const findPrevOptionIndex = (index) => {
const matchedOptionIndex = index > 0 ? ObjectUtils.findLastIndex(visibleOptions.slice(0, index), (option) => isValidOption(option)) : -1;
const matchedOptionIndex = index > 0 ? ObjectUtils.findLastIndex(appropriateOptions().slice(0, index), (option) => isValidOption(option)) : -1;

return matchedOptionIndex > -1 ? matchedOptionIndex : index;
};
Expand All @@ -377,7 +384,7 @@ export const Dropdown = React.memo(
setFocusedOptionIndex(index);

if (props.selectOnFocus) {
onOptionSelect(event, visibleOptions[index], false);
onOptionSelect(event, flatedGroupOptions ? flatedGroupOptions[index]: visibleOptions[index], false);
}
}
};
Expand Down Expand Up @@ -464,7 +471,7 @@ export const Dropdown = React.memo(
onArrowDownKey(event);
} else {
if (focusedOptionIndex !== -1) {
onOptionSelect(event, visibleOptions[focusedOptionIndex]);
onOptionSelect(event, flatedGroupOptions ? flatedGroupOptions[focusedOptionIndex]: visibleOptions[focusedOptionIndex]);
}

hide();
Expand Down
20 changes: 16 additions & 4 deletions components/lib/dropdown/DropdownPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const DropdownPanel = React.memo(
}
};

const createGroupChildren = (optionGroup, style) => {
const createGroupChildren = (optionGroup, style, passedOptionsCount) => {
melloware marked this conversation as resolved.
Show resolved Hide resolved
const groupChildren = props.getOptionGroupChildren(optionGroup);

return groupChildren.map((option, j) => {
Expand All @@ -87,7 +87,7 @@ export const DropdownPanel = React.memo(
return (
<DropdownItem
key={optionKey}
index={j}
index={j + passedOptionsCount}
focusedOptionIndex={props.focusedOptionIndex}
label={optionLabel}
option={option}
Expand Down Expand Up @@ -118,15 +118,15 @@ export const DropdownPanel = React.memo(
return <li {...emptyMessageProps}>{message}</li>;
};

const createItem = (option, index, scrollerOptions = {}) => {
const createItem = (option, index, scrollerOptions = {},passedOptionsCount) => {
let style = { height: scrollerOptions.props ? scrollerOptions.props.itemSize : undefined };

style = { ...style, ...option.style };

if (props.optionGroupLabel) {
const { optionGroupLabel } = props;
const groupContent = props.optionGroupTemplate ? ObjectUtils.getJSXElement(props.optionGroupTemplate, option, index) : props.getOptionGroupLabel(option);
const groupChildrenContent = createGroupChildren(option, style);
const groupChildrenContent = createGroupChildren(option, style, passedOptionsCount);
const key = index + '_' + props.getOptionGroupRenderKey(option);
const itemGroupProps = mergeProps(
{
Expand Down Expand Up @@ -180,6 +180,18 @@ export const DropdownPanel = React.memo(

const createItems = () => {
if (ObjectUtils.isNotEmpty(props.visibleOptions)) {


if (props.optionGroupLabel) {
let passedOptionsCount = 0

return props.visibleOptions.map((group,index) => {
passedOptionsCount += group.items.length
return createItem(group,index,{},passedOptionsCount - group.items.length)
})

}

return props.visibleOptions.map(createItem);
} else if (props.hasFilter) {
return createEmptyMessage(props.emptyFilterMessage, true);
Expand Down
Loading