@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
296 lines (295 loc) • 12.8 kB
JavaScript
"use client";
import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
var _span, _span2, _span3;
const _excluded = ["class", "className", "variant", "size", "title", "custom_content", "tooltip", "status", "status_state", "status_props", "status_no_animation", "globalStatus", "id", "disabled", "text", "icon", "icon_position", "icon_size", "wrap", "bounding", "stretch", "skeleton", "element", "inner_ref", "innerRef"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import React from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import Context from '../../shared/Context';
import { warn, makeUniqueId, isTrue, extendPropsWithContextInClassComponent, validateDOMAttributes, processChildren, getStatusState, dispatchCustomElementEvent } from '../../shared/component-helper';
import { spacingPropTypes, createSpacingClasses } from '../space/SpacingHelper';
import { skeletonDOMAttributes, createSkeletonClass } from '../skeleton/SkeletonHelper';
import { pickFormElementProps } from '../../shared/helpers/filterValidProps';
import IconPrimary from '../icon-primary/IconPrimary';
import FormStatus from '../form-status/FormStatus';
import Anchor, { pickIcon, opensNewTab } from '../anchor/Anchor';
import { launch } from '../../icons';
import Tooltip from '../tooltip/Tooltip';
export const buttonVariantPropType = {
variant: PropTypes.oneOf(['primary', 'secondary', 'tertiary', 'signal', 'unstyled'])
};
export default class Button extends React.PureComponent {
static getContent(props) {
return processChildren(props);
}
constructor(props) {
super(props);
_defineProperty(this, "getOnClickHandler", src => event => {
const afterContent = dispatchCustomElementEvent(src, 'on_click', {
event
});
if (afterContent && React.isValidElement(afterContent)) {
this.setState({
afterContent
});
}
});
this._id = props.id || (props.status || props.tooltip) && makeUniqueId();
this._ref = React.createRef();
this.state = {
afterContent: null
};
}
componentDidMount() {
if (this.props.innerRef) {
typeof this.props.innerRef === 'function' ? this.props.innerRef(this._ref.current) : this.props.innerRef.current = this._ref.current;
}
if (this.props.inner_ref) {
typeof this.props.innerRef === 'function' ? this.props.inner_ref(this._ref.current) : this.props.inner_ref.current = this._ref.current;
}
}
render() {
var _this$context, _this$context2, _this$context3, _this$context4, _this$context4$theme;
const props = extendPropsWithContextInClassComponent(this.props, Button.defaultProps, {
skeleton: (_this$context = this.context) === null || _this$context === void 0 ? void 0 : _this$context.skeleton
}, pickFormElementProps((_this$context2 = this.context) === null || _this$context2 === void 0 ? void 0 : _this$context2.FormRow), pickFormElementProps((_this$context3 = this.context) === null || _this$context3 === void 0 ? void 0 : _this$context3.formElement), this.context.Button);
const {
class: classProp,
className,
variant,
size,
title,
custom_content,
tooltip,
status,
status_state,
status_props,
status_no_animation,
globalStatus,
id,
disabled,
text: _text,
icon: _icon,
icon_position,
icon_size,
wrap,
bounding,
stretch,
skeleton,
element,
inner_ref,
innerRef
} = props,
attributes = _objectWithoutProperties(props, _excluded);
const showStatus = getStatusState(status);
let {
text,
icon
} = props;
let usedVariant = variant;
let usedSize = size;
let iconSize = icon_size;
const content = Button.getContent(this.props);
if (variant === 'tertiary' && (text || content) && !icon && icon !== false) {
warn(`Icon required: A Tertiary Button requires an icon to be WCAG compliant in most cases, because variant tertiary has no underline.
(Override this warning using icon={false}, or consider using one of the other variants)`);
}
const isIconOnly = Boolean(!text && !content && icon);
if (isIconOnly) {
if (!usedVariant) {
usedVariant = 'secondary';
}
if (!iconSize && (usedSize === 'default' || usedSize === 'large')) {
iconSize = 'medium';
}
if (!usedSize) {
usedSize = 'medium';
}
} else if (content) {
if (!usedVariant) {
usedVariant = 'primary';
}
if (!usedSize) {
usedSize = 'default';
}
}
if (!iconSize && variant === 'tertiary' && icon_position === 'top') {
iconSize = 'medium';
}
const Element = element ? element : props.href || props.to ? Anchor : 'button';
if (Element === Anchor) {
attributes.omitClass = true;
if (opensNewTab(props.target, props.href) && !icon) {
icon = launch;
}
}
const classes = classnames(`dnb-button dnb-button--${usedVariant || 'primary'}`, (text || content || custom_content) && 'dnb-button--has-text', createSkeletonClass(variant === 'tertiary' ? 'font' : 'shape', skeleton, this.context), createSpacingClasses(props), classProp, className, (props.href || props.to) && '', icon && `dnb-button--icon-position-${icon_position} dnb-button--has-icon` + (iconSize ? ` dnb-button--icon-size-${iconSize}` : ""), usedSize && usedSize !== 'default' && `dnb-button--size-${usedSize}`, ((_this$context4 = this.context) === null || _this$context4 === void 0 ? void 0 : (_this$context4$theme = _this$context4.theme) === null || _this$context4$theme === void 0 ? void 0 : _this$context4$theme.darkBackground) && `dnb-button--on-dark-background`, isTrue(stretch) && 'dnb-button--stretch', wrap && 'dnb-button--wrap', status && `dnb-button__status--${status_state}`, Element === Anchor && 'dnb-anchor--no-style');
const params = _objectSpread({
className: classes,
title,
id: this._id,
disabled: isTrue(disabled)
}, attributes);
if (props.on_click || props.onClick) {
params.onClick = this.getOnClickHandler(props);
}
if (Element !== Anchor && !params.type) {
params.type = params.type === '' ? undefined : 'button';
}
if (isIconOnly) {
params['aria-label'] = params['aria-label'] || title;
}
skeletonDOMAttributes(params, skeleton, this.context);
validateDOMAttributes(this.props, params);
return React.createElement(React.Fragment, null, React.createElement(Element, _extends({
ref: this._ref
}, params), React.createElement(Content, _extends({}, this.props, {
icon: icon,
icon_size: iconSize,
content: text || content,
custom_content: custom_content,
isIconOnly: isIconOnly,
skeleton: isTrue(skeleton)
}))), this.state.afterContent, React.createElement(FormStatus, _extends({
show: showStatus,
id: this._id + '-form-status',
globalStatus: globalStatus,
label: text,
text: status,
state: status_state,
text_id: this._id + '-status',
no_animation: status_no_animation,
skeleton: skeleton
}, status_props)), tooltip && this._ref && React.createElement(Tooltip, {
id: this._id + '-tooltip',
targetElement: this._ref,
tooltip: tooltip
}));
}
}
_defineProperty(Button, "contextType", Context);
process.env.NODE_ENV !== "production" ? Button.propTypes = _objectSpread(_objectSpread({
text: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
type: PropTypes.string,
title: PropTypes.string,
variant: buttonVariantPropType.variant,
size: PropTypes.oneOf(['default', 'small', 'medium', 'large']),
icon: PropTypes.oneOfType([PropTypes.string, PropTypes.node, PropTypes.func]),
icon_position: PropTypes.oneOf(['left', 'right', 'top']),
icon_size: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
tooltip: PropTypes.oneOfType([PropTypes.string, PropTypes.func, PropTypes.node]),
status: PropTypes.oneOfType([PropTypes.string, PropTypes.bool, PropTypes.func, PropTypes.node]),
status_state: PropTypes.string,
status_props: PropTypes.object,
status_no_animation: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
globalStatus: PropTypes.shape({
id: PropTypes.string,
message: PropTypes.oneOfType([PropTypes.string, PropTypes.node])
}),
id: PropTypes.string,
href: PropTypes.string,
target: PropTypes.string,
rel: PropTypes.string,
to: PropTypes.oneOfType([PropTypes.string, PropTypes.object, PropTypes.func]),
custom_content: PropTypes.node,
wrap: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
bounding: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
stretch: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
skeleton: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
disabled: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
inner_ref: PropTypes.oneOfType([PropTypes.object, PropTypes.func]),
className: PropTypes.string,
class: PropTypes.string,
innerRef: PropTypes.oneOfType([PropTypes.object, PropTypes.func]),
children: PropTypes.oneOfType([PropTypes.string, PropTypes.func, PropTypes.node]),
element: PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.node])
}, spacingPropTypes), {}, {
on_click: PropTypes.oneOfType([PropTypes.string, PropTypes.func])
}) : void 0;
Button.defaultProps = {
type: null,
text: null,
variant: null,
size: null,
title: null,
icon: null,
icon_position: 'right',
icon_size: null,
href: null,
target: null,
rel: null,
to: null,
id: null,
custom_content: null,
wrap: null,
bounding: null,
stretch: null,
skeleton: null,
disabled: null,
tooltip: null,
status: null,
status_state: 'error',
status_props: null,
status_no_animation: null,
globalStatus: null,
inner_ref: null,
className: null,
class: null,
innerRef: null,
children: null,
element: null,
on_click: null,
onClick: null
};
function Content({
title = null,
content = null,
custom_content = null,
icon = null,
icon_size = 'default',
bounding = null,
skeleton = null,
isIconOnly = null
}) {
return React.createElement(React.Fragment, null, isTrue(bounding) && (_span || (_span = React.createElement("span", {
key: "button-bounding",
className: "dnb-button__bounding"
}))), custom_content && React.createElement(React.Fragment, {
key: "button-custom-content"
}, custom_content), content && React.createElement(React.Fragment, null, _span2 || (_span2 = React.createElement("span", {
key: "button-alignment",
className: "dnb-button__alignment",
"aria-hidden": true
}, "\u200C")), React.createElement("span", {
key: "button-text",
className: "dnb-button__text dnb-skeleton--show-font"
}, content)), !content && icon && (_span3 || (_span3 = React.createElement("span", {
key: "button-alignment",
className: "dnb-button__alignment",
"aria-hidden": true
}, "\u200C"))), icon && (pickIcon(icon, 'dnb-button__icon') || React.createElement(IconPrimary, {
key: "button-icon",
className: "dnb-button__icon",
icon: icon,
size: icon_size,
"aria-hidden": isIconOnly && !title ? null : true,
skeleton: skeleton
})));
}
process.env.NODE_ENV !== "production" ? Content.propTypes = {
title: PropTypes.string,
custom_content: PropTypes.node,
content: PropTypes.oneOfType([PropTypes.string, PropTypes.array, PropTypes.node]),
icon: PropTypes.oneOfType([PropTypes.string, PropTypes.node, PropTypes.func]),
icon_size: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
bounding: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
skeleton: PropTypes.bool,
isIconOnly: PropTypes.bool
} : void 0;
Button._formElement = true;
Button._supportsSpacingProps = true;
//# sourceMappingURL=Button.js.map