@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
136 lines (135 loc) • 4.3 kB
JavaScript
"use client";
var _ChevronIcon, _ChevronIcon2, _div;
import clsx from 'clsx';
import { ListContext } from "./ListContext.js";
import ItemContent from "./ItemContent.js";
import React, { useCallback, useContext, useRef } from 'react';
import IconPrimary from "../IconPrimary.js";
import Anchor from "../Anchor.js";
import ItemIcon from "./ItemIcon.js";
import ItemTitle from "./ItemTitle.js";
import withComponentMarkers from "../../shared/helpers/withComponentMarkers.js";
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
function ItemAction(props) {
var _useContext;
const {
className,
onClick,
children,
variant,
selected,
pending,
disabled,
skeleton,
chevronPosition = 'right',
icon,
title,
href,
to,
element,
elementProps,
target,
rel,
...rest
} = props;
const inheritedDisabled = (_useContext = useContext(ListContext)) === null || _useContext === void 0 ? void 0 : _useContext.disabled;
const appliedDisabled = disabled !== null && disabled !== void 0 ? disabled : inheritedDisabled;
const isInactive = pending || appliedDisabled;
const handleClick = useCallback(event => {
if (!isInactive) {
onClick && onClick(event);
}
}, [onClick, isInactive]);
const handleKeyDown = useCallback(event => {
if (event.key === 'Enter' || event.key === ' ') {
event.preventDefault();
handleClick(event);
}
}, [handleClick]);
const anchorRef = useRef(null);
const handleLinkClick = useCallback(event => {
if (!isInactive) {
onClick === null || onClick === void 0 || onClick(event);
}
}, [onClick, isInactive]);
const handleLinkKeyDown = useCallback(event => {
if (event.key === 'Enter' || event.key === ' ') {
event.preventDefault();
if (!isInactive) {
var _anchorRef$current;
(_anchorRef$current = anchorRef.current) === null || _anchorRef$current === void 0 || _anchorRef$current.click();
onClick === null || onClick === void 0 || onClick(event);
}
}
}, [onClick, isInactive]);
const actionClassName = clsx('dnb-list__item__action', (href || to) && 'dnb-list__item__action--href', className, chevronPosition === 'left' && 'dnb-list__item--chevron-left');
const content = _jsxs(_Fragment, {
children: [chevronPosition === 'left' && (_ChevronIcon || (_ChevronIcon = _jsx(ChevronIcon, {}))), icon !== undefined && _jsx(ItemIcon, {
children: icon
}), title !== undefined && _jsx(ItemTitle, {
children: title
}), children, chevronPosition === 'right' && (_ChevronIcon2 || (_ChevronIcon2 = _jsx(ChevronIcon, {})))]
});
if (href || to) {
return _jsx(ItemContent, {
className: actionClassName,
role: "link",
tabIndex: isInactive ? -1 : 0,
"aria-disabled": isInactive ? true : undefined,
onClick: handleLinkClick,
onKeyDown: handleLinkKeyDown,
variant: variant,
selected: selected,
skeleton: skeleton,
pending: pending,
disabled: appliedDisabled,
...rest,
children: _jsx(Anchor, {
noStyle: true,
ref: anchorRef,
...(href != null ? {
href: isInactive ? undefined : href
} : {}),
to: isInactive ? undefined : to,
element: element,
target: target,
rel: rel,
tabIndex: -1,
"aria-disabled": isInactive ? true : undefined,
...elementProps,
children: content
})
});
}
return _jsx(ItemContent, {
className: actionClassName,
role: "button",
tabIndex: isInactive ? -1 : 0,
"aria-disabled": isInactive ? true : undefined,
onClick: handleClick,
onKeyDown: handleKeyDown,
variant: variant,
selected: selected,
skeleton: skeleton,
pending: pending,
disabled: appliedDisabled,
...rest,
children: content
});
}
withComponentMarkers(ChevronIcon, {
_supportsSpacingProps: true
});
withComponentMarkers(ItemAction, {
_supportsSpacingProps: true
});
export default ItemAction;
export function ChevronIcon() {
return _div || (_div = _jsx("div", {
className: "dnb-list__item__chevron",
children: _jsx(IconPrimary, {
icon: "chevron_right"
})
}));
}
//# sourceMappingURL=ItemAction.js.map