@mantine/core
Version:
React components library focused on usability, accessibility and developer experience
87 lines (83 loc) • 2.96 kB
JavaScript
'use client';
;
var React = require('react');
var Combobox_context = require('../Combobox.context.cjs');
function useComboboxTargetProps({
onKeyDown,
withKeyboardNavigation,
withAriaAttributes,
withExpandedAttribute,
targetType,
autoComplete
}) {
const ctx = Combobox_context.useComboboxContext();
const [selectedOptionId, setSelectedOptionId] = React.useState(null);
const handleKeyDown = (event) => {
onKeyDown?.(event);
if (ctx.readOnly) {
return;
}
if (withKeyboardNavigation) {
if (event.nativeEvent.isComposing) {
return;
}
if (event.nativeEvent.code === "ArrowDown") {
event.preventDefault();
if (!ctx.store.dropdownOpened) {
ctx.store.openDropdown("keyboard");
setSelectedOptionId(ctx.store.selectActiveOption());
ctx.store.updateSelectedOptionIndex("selected", { scrollIntoView: true });
} else {
setSelectedOptionId(ctx.store.selectNextOption());
}
}
if (event.nativeEvent.code === "ArrowUp") {
event.preventDefault();
if (!ctx.store.dropdownOpened) {
ctx.store.openDropdown("keyboard");
setSelectedOptionId(ctx.store.selectActiveOption());
ctx.store.updateSelectedOptionIndex("selected", { scrollIntoView: true });
} else {
setSelectedOptionId(ctx.store.selectPreviousOption());
}
}
if (event.nativeEvent.code === "Enter" || event.nativeEvent.code === "NumpadEnter") {
if (event.nativeEvent.keyCode === 229) {
return;
}
const selectedOptionIndex = ctx.store.getSelectedOptionIndex();
if (ctx.store.dropdownOpened && selectedOptionIndex !== -1) {
event.preventDefault();
ctx.store.clickSelectedOption();
} else if (targetType === "button") {
event.preventDefault();
ctx.store.openDropdown("keyboard");
}
}
if (event.nativeEvent.code === "Escape") {
ctx.store.closeDropdown("keyboard");
}
if (event.nativeEvent.code === "Space") {
if (targetType === "button") {
event.preventDefault();
ctx.store.toggleDropdown("keyboard");
}
}
}
};
const ariaAttributes = withAriaAttributes ? {
"aria-haspopup": "listbox",
"aria-expanded": withExpandedAttribute && !!(ctx.store.listId && ctx.store.dropdownOpened) || void 0,
"aria-controls": ctx.store.dropdownOpened ? ctx.store.listId : void 0,
"aria-activedescendant": ctx.store.dropdownOpened ? selectedOptionId || void 0 : void 0,
autoComplete,
"data-expanded": ctx.store.dropdownOpened || void 0,
"data-mantine-stop-propagation": ctx.store.dropdownOpened || void 0
} : {};
return {
...ariaAttributes,
onKeyDown: handleKeyDown
};
}
exports.useComboboxTargetProps = useComboboxTargetProps;
//# sourceMappingURL=use-combobox-target-props.cjs.map