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
40 changes: 25 additions & 15 deletions components/lib/dropdown/Dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,30 @@ export const Dropdown = React.memo(
when: overlayVisibleState
});

const flattenedOptions = props.optionGroupLabel ? props.options.flatMap((item) => item.items) : null
const appropriateOptions = () => flattenedOptions ?? visibleOptions
function flatOptions(options) {
melloware marked this conversation as resolved.
Show resolved Hide resolved
return (options || []).reduce((result, option, index) => {
result.push({ optionGroup: option, group: true, index, code: option.code, label: option.label });

const optionGroupChildren = getOptionGroupChildren(option);

optionGroupChildren && optionGroupChildren.forEach((o) => result.push(o));

return result;
}, []);
}

const getVisibleOptions = () => {

const options = props.optionGroupLabel ? flatOptions(props.options) : props.options

if (hasFilter && !isLazy) {
const filterValue = filterState.trim().toLocaleLowerCase(props.filterLocale);
const searchFields = props.filterBy ? props.filterBy.split(',') : [props.optionLabel || 'label'];

if (props.optionGroupLabel) {
let filteredGroups = [];

for (let optgroup of props.options) {
for (let optgroup of options) {
Copy link
Contributor Author

@KirilCycle KirilCycle Apr 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did not check with filter functionality yet

let filteredSubOptions = FilterService.filter(getOptionGroupChildren(optgroup), searchFields, filterValue, props.filterMatchMode, props.filterLocale);

if (filteredSubOptions && filteredSubOptions.length) {
Expand All @@ -79,10 +91,10 @@ export const Dropdown = React.memo(
return filteredGroups;
}

return FilterService.filter(props.options, searchFields, filterValue, props.filterMatchMode, props.filterLocale);
return FilterService.filter(options, searchFields, filterValue, props.filterMatchMode, props.filterLocale);
}

return props.options;
return options;
};

const onFirstHiddenFocus = (event) => {
Expand Down Expand Up @@ -301,8 +313,7 @@ export const Dropdown = React.memo(
};

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

const findFirstFocusedOptionIndex = () => {
Expand Down Expand Up @@ -357,22 +368,21 @@ export const Dropdown = React.memo(
};

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

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

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

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

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

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

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

hide();
Expand Down Expand Up @@ -1289,4 +1299,4 @@ export const Dropdown = React.memo(
})
);

Dropdown.displayName = 'Dropdown';
Dropdown.displayName = 'Dropdown';
60 changes: 7 additions & 53 deletions components/lib/dropdown/DropdownPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,36 +76,6 @@ export const DropdownPanel = React.memo(
}
};

const createGroupChildren = (optionGroup, style, index) => {
const groupChildren = props.getOptionGroupChildren(optionGroup);

return groupChildren.map((option, j) => {
const optionLabel = props.getOptionLabel(option);
const optionKey = j + '_' + props.getOptionRenderKey(option);
const disabled = props.isOptionDisabled(option);

return (
<DropdownItem
key={optionKey}
index={j + index}
focusedOptionIndex={props.focusedOptionIndex}
label={optionLabel}
option={option}
style={style}
template={props.itemTemplate}
selected={props.isSelected(option)}
highlightOnSelect={props.highlightOnSelect}
disabled={disabled}
onClick={props.onOptionClick}
onMouseMove={changeFocusedItemOnHover}
ptm={ptm}
cx={cx}
checkmark={props.checkmark}
/>
);
});
};

const createEmptyMessage = (emptyMessage, isFilter) => {
const message = ObjectUtils.getJSXElement(emptyMessage, props) || localeOption(isFilter ? 'emptyFilterMessage' : 'emptyMessage');
const emptyMessageProps = mergeProps(
Expand All @@ -118,15 +88,14 @@ export const DropdownPanel = React.memo(
return <li {...emptyMessageProps}>{message}</li>;
};

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

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

if (props.optionGroupLabel) {
if (option.group && props.optionGroupLabel) {
const { optionGroupLabel } = props;
const groupContent = props.optionGroupTemplate ? ObjectUtils.getJSXElement(props.optionGroupTemplate, option, index) : props.getOptionGroupLabel(option);
const groupChildrenContent = createGroupChildren(option, style, passedOptionsCount);
const key = index + '_' + props.getOptionGroupRenderKey(option);
const itemGroupProps = mergeProps(
{
Expand All @@ -144,17 +113,14 @@ export const DropdownPanel = React.memo(
);

return (
<React.Fragment key={key}>
<li {...itemGroupProps}>
<li key={key} {...itemGroupProps}>
<span {...itemGroupLabelProps}>{groupContent}</span>
</li>
{groupChildrenContent}
</React.Fragment>
);
</li>
)

}

const optionLabel = props.getOptionLabel(option);
const optionKey = index + '_' + props.getOptionRenderKey(option);
melloware marked this conversation as resolved.
Show resolved Hide resolved
const optionLabel = props.getOptionLabel(option);
const disabled = props.isOptionDisabled(option);

return (
Expand All @@ -180,18 +146,6 @@ 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