suomifi-ui-components
Version:
Suomi.fi UI component library
114 lines (111 loc) • 5.09 kB
JavaScript
import { __makeTemplateObject, __rest, __assign } from 'tslib';
import React, { useRef, useLayoutEffect } from 'react';
import { styled } from 'styled-components';
import { IconCheck } from 'suomifi-icons';
import { baseStyles } from './DropdownItem.basestyles.js';
import { dropdownClassNames, DropdownConsumer } from '../Dropdown/Dropdown.js';
import classnames from 'classnames';
import { SuomifiThemeConsumer } from '../../../theme/SuomifiThemeProvider/SuomifiThemeProvider.js';
import '../../../theme/SuomifiTheme/SuomifiTheme.js';
import '../../../theme/SpacingProvider/SpacingProvider.js';
import '../../../../reset/HtmlA/HtmlA.js';
import '../../../../reset/HtmlButton/HtmlButton.js';
import '../../../../reset/HtmlDiv/HtmlDiv.js';
import '../../../../reset/HtmlFieldSet/HtmlFieldSet.js';
import '../../../../reset/HtmlH/HtmlH.js';
import '../../../../reset/HtmlInput/HtmlInput.js';
import '../../../../reset/HtmlLabel/HtmlLabel.js';
import '../../../../reset/HtmlLegend/HtmlLegend.js';
import { HtmlLi } from '../../../../reset/HtmlLi/HtmlLi.js';
import '../../../../reset/HtmlNav/HtmlNav.js';
import '../../../../reset/HtmlOl/HtmlOl.js';
import '../../../../reset/HtmlSpan/HtmlSpan.js';
import '../../../../reset/HtmlTextarea/HtmlTextarea.js';
import '../../../../reset/HtmlUl/HtmlUl.js';
import '../../../../reset/HtmlTable/HtmlTable.js';
import '../../../../reset/HtmlTable/HtmlTableCaption.js';
import '../../../../reset/HtmlTable/HtmlTableHeader.js';
import '../../../../reset/HtmlTable/HtmlTableRow.js';
import '../../../../reset/HtmlTable/HtmlTableBody.js';
import '../../../../reset/HtmlTable/HtmlTableHeaderCell.js';
import '../../../../reset/HtmlTable/HtmlTableCell.js';
import { getOwnerDocument } from '../../../../utils/common/common.js';
var dropdownItemClassNames = {
hasKeyboardFocus: "".concat(dropdownClassNames.item, "--hasKeyboardFocus"),
selected: "".concat(dropdownClassNames.item, "--selected"),
disabled: "".concat(dropdownClassNames.item, "--disabled"),
noSelectedStyles: "".concat(dropdownClassNames.item, "--noSelectedStyles"),
icon: "".concat(dropdownClassNames.item, "_icon")
};
var BaseDropdownItem = function BaseDropdownItem(props) {
var _a;
var children = props.children,
className = props.className;
props.theme;
var consumer = props.consumer,
value = props.value,
_b = props.disabled,
disabled = _b === void 0 ? false : _b,
passProps = __rest(props, ["children", "className", "theme", "consumer", "value", "disabled"]);
var selected = consumer.selectedDropdownValue === value;
var hasKeyboardFocus = consumer.focusedItemValue === value;
var listElementId = "".concat(consumer.id, "-").concat(value);
var listElementRef = useRef(null);
useLayoutEffect(function () {
var _a, _b;
var ownerDocument = getOwnerDocument(listElementRef);
if (((_a = ownerDocument.activeElement) === null || _a === void 0 ? void 0 : _a.id) !== listElementId && hasKeyboardFocus) {
(_b = listElementRef.current) === null || _b === void 0 ? void 0 : _b.focus({
preventScroll: true
});
}
}, [consumer.focusedItemValue]);
var handleClick = function handleClick() {
if (!disabled) {
consumer.onItemClick(value);
}
};
return /*#__PURE__*/React.createElement(HtmlLi, __assign({
className: classnames(className, dropdownClassNames.item, (_a = {}, _a[dropdownItemClassNames.hasKeyboardFocus] = hasKeyboardFocus, _a[dropdownItemClassNames.selected] = selected, _a[dropdownItemClassNames.disabled] = disabled, _a[dropdownItemClassNames.noSelectedStyles] = consumer.noSelectedStyles, _a)),
tabIndex: hasKeyboardFocus ? 0 : -1,
role: "option",
"aria-disabled": disabled,
"aria-selected": selected,
id: listElementId,
onClick: handleClick,
forwardedRef: listElementRef,
onKeyDown: function onKeyDown(event) {
if (event.key === 'Tab') {
event.preventDefault();
consumer.onItemTabPress();
}
},
onMouseOver: function onMouseOver() {
consumer.onItemMouseOver(value);
}
}, passProps), children, selected && !consumer.noSelectedStyles && ( /*#__PURE__*/React.createElement(IconCheck, {
className: dropdownItemClassNames.icon,
"aria-hidden": true
})));
};
var StyledDropdownItem = styled(BaseDropdownItem).withConfig({
componentId: "sc-1pc651a-0"
})(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n ", "\n"], ["\n ", "\n"])), function (_a) {
var theme = _a.theme;
return baseStyles(theme);
});
var DropdownItem = function DropdownItem(props) {
return /*#__PURE__*/React.createElement(SuomifiThemeConsumer, null, function (_a) {
var suomifiTheme = _a.suomifiTheme;
return /*#__PURE__*/React.createElement(DropdownConsumer, null, function (consumer) {
return /*#__PURE__*/React.createElement(StyledDropdownItem, __assign({
theme: suomifiTheme,
consumer: consumer
}, props));
});
});
};
DropdownItem.displayName = 'DropdownItem';
var templateObject_1;
export { DropdownItem };
//# sourceMappingURL=DropdownItem.js.map