suomifi-ui-components
Version:
Suomi.fi UI component library
112 lines (109 loc) • 5.05 kB
JavaScript
import { __rest, __assign, __makeTemplateObject } from 'tslib';
import React, { isValidElement, cloneElement } from 'react';
import classnames from 'classnames';
import { styled } from 'styled-components';
import { SuomifiThemeConsumer } from '../../theme/SuomifiThemeProvider/SuomifiThemeProvider.js';
import '../../theme/SuomifiTheme/SuomifiTheme.js';
import { SpacingConsumer } from '../../theme/SpacingProvider/SpacingProvider.js';
import { separateMarginProps } from '../../theme/utils/spacing.js';
import { baseStyles } from './Label.baseStyles.js';
import { VisuallyHidden } from '../../VisuallyHidden/VisuallyHidden.js';
import '../../../reset/HtmlA/HtmlA.js';
import '../../../reset/HtmlButton/HtmlButton.js';
import { HtmlDivWithRef } from '../../../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 '../../../reset/HtmlLi/HtmlLi.js';
import '../../../reset/HtmlNav/HtmlNav.js';
import '../../../reset/HtmlOl/HtmlOl.js';
import { HtmlSpan } from '../../../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 { filterDuplicateKeys } from '../../../utils/common/common.js';
var baseClassName = 'fi-label';
var labelTextClassNames = {
labelSpan: "".concat(baseClassName, "_label-span"),
optionalText: "".concat(baseClassName, "_optional-text")
};
var StyledLabel = styled(function (_a) {
var className = _a.className,
contentStyle = _a.contentStyle,
contentClassName = _a.contentClassName;
_a.theme;
var _b = _a.labelMode,
labelMode = _b === void 0 ? 'visible' : _b,
style = _a.style,
children = _a.children,
_c = _a.asProp,
asProp = _c === void 0 ? 'label' : _c;
_a.globalMargins;
var optionalText = _a.optionalText,
tooltipComponentProp = _a.tooltipComponent,
_d = _a.forceTooltipRerender,
forceTooltipRerender = _d === void 0 ? false : _d,
rest = __rest(_a, ["className", "contentStyle", "contentClassName", "theme", "labelMode", "style", "children", "asProp", "globalMargins", "optionalText", "tooltipComponent", "forceTooltipRerender"]);
var _e = separateMarginProps(rest),
passProps = _e[1];
var wrapperRef = React.useRef(null);
function getTooltipComponent(tooltipComponent) {
if ( /*#__PURE__*/isValidElement(tooltipComponent)) {
return /*#__PURE__*/cloneElement(tooltipComponent, {
anchorElement: wrapperRef.current,
key: forceTooltipRerender ? Date.now() : null
});
}
return null;
}
return /*#__PURE__*/React.createElement(HtmlDivWithRef, {
className: classnames(className, baseClassName),
forwardedRef: function forwardedRef(ref) {
wrapperRef.current = ref;
},
style: style
}, labelMode === 'hidden' ? ( /*#__PURE__*/React.createElement(VisuallyHidden, __assign({
asProp: asProp
}, passProps), children, optionalText && "(".concat(optionalText, ")"))) : ( /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(HtmlSpan, __assign({
as: asProp,
style: contentStyle ? __assign({}, contentStyle) : {}
}, passProps, {
className: classnames(labelTextClassNames.labelSpan, contentClassName)
}), children, optionalText && ( /*#__PURE__*/React.createElement(HtmlSpan, {
className: labelTextClassNames.optionalText
}, " (".concat(optionalText, ")")))), !!tooltipComponentProp && getTooltipComponent(tooltipComponentProp))));
}).withConfig({
componentId: "sc-cooxd2-0"
})(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n ", "\n"], ["\n ", "\n"])), function (_a) {
var theme = _a.theme,
globalMargins = _a.globalMargins,
rest = __rest(_a, ["theme", "globalMargins"]);
var _b = separateMarginProps(rest),
marginProps = _b[0];
var cleanedGlobalMargins = filterDuplicateKeys(globalMargins.label, marginProps);
return baseStyles(theme, cleanedGlobalMargins, marginProps);
});
var Label = function Label(props) {
return /*#__PURE__*/React.createElement(SpacingConsumer, null, function (_a) {
var margins = _a.margins;
return /*#__PURE__*/React.createElement(SuomifiThemeConsumer, null, function (_a) {
var suomifiTheme = _a.suomifiTheme;
return /*#__PURE__*/React.createElement(StyledLabel, __assign({
globalMargins: margins,
theme: suomifiTheme
}, props));
});
});
};
Label.displayName = 'Label';
var templateObject_1;
export { Label };
//# sourceMappingURL=Label.js.map