suomifi-ui-components
Version:
Suomi.fi UI component library
205 lines (202 loc) • 8.92 kB
JavaScript
import { __extends, __rest, __assign, __makeTemplateObject } from 'tslib';
import React, { Component, forwardRef } from 'react';
import { styled } from 'styled-components';
import classnames from 'classnames';
import { getConditionalAriaProp } from '../../../utils/aria/aria.js';
import { getLogger } from '../../../utils/log/logger.js';
import { AutoId } from '../../utils/AutoId/AutoId.js';
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 '../../../reset/HtmlA/HtmlA.js';
import '../../../reset/HtmlButton/HtmlButton.js';
import { HtmlDiv } from '../../../reset/HtmlDiv/HtmlDiv.js';
import '../../../reset/HtmlFieldSet/HtmlFieldSet.js';
import '../../../reset/HtmlH/HtmlH.js';
import { HtmlInput } from '../../../reset/HtmlInput/HtmlInput.js';
import { HtmlLabel } from '../../../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 '../../../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 { StatusText } from '../StatusText/StatusText.js';
import { HintText } from '../HintText/HintText.js';
import { CheckboxGroupConsumer } from './CheckboxGroup.js';
import { baseStyles } from './Checkbox.baseStyles.js';
import { IconCheck } from 'suomifi-icons';
import { filterDuplicateKeys } from '../../../utils/common/common.js';
var baseClassName = 'fi-checkbox';
var checkboxClassNames = {
container: "".concat(baseClassName, "_container"),
input: "".concat(baseClassName, "_input"),
label: "".concat(baseClassName, "_label"),
statusTextHasContent: "".concat(baseClassName, "_statusText--has-content"),
hintText: "".concat(baseClassName, "_hintText"),
disabled: "".concat(baseClassName, "--disabled"),
error: "".concat(baseClassName, "--error"),
checked: "".concat(baseClassName, "--checked"),
large: "".concat(baseClassName, "--large"),
icon: "".concat(baseClassName, "_icon")
};
var BaseCheckbox = function (_super) {
__extends(BaseCheckbox, _super);
function BaseCheckbox() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.state = {
checkedState: !!_this.props.checked || !!_this.props.defaultChecked
};
_this.handleClick = function () {
var _a = _this.props,
onClick = _a.onClick,
checked = _a.checked;
var checkedState = _this.state.checkedState;
if (checked === undefined) {
_this.setState({
checkedState: !checkedState
});
}
if (!!onClick) {
onClick({
checkboxState: !checkedState
});
}
};
return _this;
}
BaseCheckbox.getDerivedStateFromProps = function (nextProps, prevState) {
var checked = nextProps.checked;
if (checked !== undefined && checked !== prevState.checkedState) {
return {
checkedState: checked
};
}
return null;
};
BaseCheckbox.prototype.render = function () {
var _a, _b;
var _c = this.props,
id = _c.id,
className = _c.className,
_d = _c.disabled,
disabled = _d === void 0 ? false : _d,
ariaLabel = _c["aria-label"],
ariaLabelledBy = _c["aria-labelledby"],
ariaDescribedBy = _c["aria-describedby"],
_e = _c.statusTextAriaLiveMode,
statusTextAriaLiveMode = _e === void 0 ? 'assertive' : _e,
children = _c.children;
_c.checked;
_c.defaultChecked;
_c.onClick;
var hintText = _c.hintText,
status = _c.status,
statusText = _c.statusText,
name = _c.name,
value = _c.value,
forwardedRef = _c.forwardedRef;
_c.onClick;
var variant = _c.variant,
style = _c.style,
labelProps = _c.labelProps,
rest = __rest(_c, ["id", "className", "disabled", 'aria-label', 'aria-labelledby', 'aria-describedby', "statusTextAriaLiveMode", "children", "checked", "defaultChecked", "onClick", "hintText", "status", "statusText", "name", "value", "forwardedRef", "onClick", "variant", "style", "labelProps"]);
var _f = separateMarginProps(rest),
passProps = _f[1];
var checkedState = this.state.checkedState;
if (!children) {
getLogger().error('Checkbox component should have a label or a child element that acts as one. Add label content or a child element.');
}
if ('name' in this.props && (typeof name !== 'string' || name === '') || 'value' in this.props && (typeof value !== 'string' || value === '')) {
getLogger().warn('Name and value props should have non-empty values if provided.');
}
var statusTextId = !!statusText ? "".concat(id, "-statusText") : undefined;
var hintTextId = !!hintText ? "".concat(id, "-hintText") : undefined;
return /*#__PURE__*/React.createElement(HtmlDiv, {
className: classnames(checkboxClassNames.container, className, baseClassName, (_a = {}, _a[checkboxClassNames.error] = status === 'error' && !disabled, _a[checkboxClassNames.checked] = checkedState, _a[checkboxClassNames.large] = variant === 'large', _a[checkboxClassNames.disabled] = !!disabled, _a)),
style: style
}, /*#__PURE__*/React.createElement(HtmlInput, __assign({
type: "checkbox",
disabled: disabled,
id: id
}, getConditionalAriaProp('aria-label', [ariaLabel]), getConditionalAriaProp('aria-labelledby', [ariaLabelledBy]), getConditionalAriaProp('aria-describedby', [statusTextId, hintTextId, ariaDescribedBy]), {
"aria-invalid": status === 'error',
checked: !!checkedState,
className: checkboxClassNames.input,
onChange: this.handleClick,
name: name,
forwardedRef: forwardedRef
}, value ? {
value: value
} : {}, passProps)), /*#__PURE__*/React.createElement(HtmlLabel, __assign({
htmlFor: id,
className: checkboxClassNames.label,
id: "".concat(id, "-label")
}, labelProps), !!checkedState && /*#__PURE__*/React.createElement(IconCheck, {
className: checkboxClassNames.icon
}), children), /*#__PURE__*/React.createElement(HintText, {
id: hintTextId
}, hintText), /*#__PURE__*/React.createElement(StatusText, {
className: classnames((_b = {}, _b[checkboxClassNames.statusTextHasContent] = !!statusText, _b)),
id: statusTextId,
status: status,
disabled: disabled,
ariaLiveMode: statusTextAriaLiveMode
}, statusText));
};
return BaseCheckbox;
}(Component);
var StyledCheckbox = styled(function (_a) {
_a.theme;
_a.globalMargins;
var passProps = __rest(_a, ["theme", "globalMargins"]);
return /*#__PURE__*/React.createElement(BaseCheckbox, __assign({}, passProps));
}).withConfig({
componentId: "sc-1lesxnx-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.checkbox, marginProps);
return baseStyles(theme, cleanedGlobalMargins, marginProps);
});
var Checkbox = /*#__PURE__*/forwardRef(function (props, ref) {
var propId = props.id,
propStatus = props.status,
passProps = __rest(props, ["id", "status"]);
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(AutoId, {
id: propId
}, function (id) {
return /*#__PURE__*/React.createElement(CheckboxGroupConsumer, null, function (_a) {
var groupStatus = _a.status;
return /*#__PURE__*/React.createElement(StyledCheckbox, __assign({
theme: suomifiTheme,
id: id,
forwardedRef: ref,
globalMargins: margins,
status: !!propStatus ? propStatus : groupStatus
}, passProps));
});
});
});
});
});
Checkbox.displayName = 'Checkbox';
var templateObject_1;
export { Checkbox };
//# sourceMappingURL=Checkbox.js.map