carbon-react 101.4.5 → 102.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/__internal__/label/icon-wrapper.style.js +1 -8
- package/lib/__internal__/label/label.style.js +13 -24
- package/lib/components/button-toggle/button-toggle-input.component.js +4 -7
- package/lib/components/button-toggle/button-toggle.component.js +28 -1
- package/lib/components/button-toggle/button-toggle.d.ts +10 -0
- package/lib/components/button-toggle/button-toggle.style.js +5 -2
- package/lib/components/button-toggle-group/button-toggle-group.component.js +1 -1
- package/lib/components/decimal/decimal.component.js +178 -241
- package/lib/components/radio-button/radio-button.style.js +4 -5
- package/package.json +8 -4
|
@@ -11,8 +11,6 @@ var _validationIcon = _interopRequireDefault(require("../validations/validation-
|
|
|
11
11
|
|
|
12
12
|
var _help = _interopRequireDefault(require("../../components/help/help.style"));
|
|
13
13
|
|
|
14
|
-
var _base = _interopRequireDefault(require("../../style/themes/base"));
|
|
15
|
-
|
|
16
14
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
17
15
|
|
|
18
16
|
const IconWrapperStyle = _styledComponents.default.div`
|
|
@@ -27,17 +25,12 @@ const IconWrapperStyle = _styledComponents.default.div`
|
|
|
27
25
|
vertical-align: top;
|
|
28
26
|
|
|
29
27
|
:focus {
|
|
30
|
-
outline:
|
|
31
|
-
theme
|
|
32
|
-
}) => `2px solid ${theme.colors.focus}`};
|
|
28
|
+
outline: 2px solid var(--colorsSemanticFocus500);
|
|
33
29
|
}
|
|
34
30
|
|
|
35
31
|
${_validationIcon.default}, ${_help.default} {
|
|
36
32
|
position: static;
|
|
37
33
|
}
|
|
38
34
|
`;
|
|
39
|
-
IconWrapperStyle.defaultProps = {
|
|
40
|
-
theme: _base.default
|
|
41
|
-
};
|
|
42
35
|
var _default = IconWrapperStyle;
|
|
43
36
|
exports.default = _default;
|
|
@@ -9,8 +9,6 @@ var _styledComponents = _interopRequireWildcard(require("styled-components"));
|
|
|
9
9
|
|
|
10
10
|
var _propTypes = _interopRequireDefault(require("prop-types"));
|
|
11
11
|
|
|
12
|
-
var _base = _interopRequireDefault(require("../../style/themes/base"));
|
|
13
|
-
|
|
14
12
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
15
13
|
|
|
16
14
|
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
|
|
@@ -18,36 +16,30 @@ function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return
|
|
|
18
16
|
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
19
17
|
|
|
20
18
|
const LabelStyle = _styledComponents.default.label`
|
|
21
|
-
color:
|
|
22
|
-
theme
|
|
23
|
-
}) => theme.text.color};
|
|
19
|
+
color: var(--colorsYin090);
|
|
24
20
|
display: block;
|
|
25
|
-
font-weight: 600;
|
|
21
|
+
font-weight: 600; //TODO: (tokens) use token var(--fontWeights500)
|
|
26
22
|
|
|
27
23
|
${({
|
|
28
|
-
isRequired
|
|
29
|
-
theme
|
|
24
|
+
isRequired
|
|
30
25
|
}) => isRequired && (0, _styledComponents.css)`
|
|
31
26
|
::after {
|
|
32
27
|
content: "*";
|
|
33
|
-
color:
|
|
28
|
+
color: var(--colorsSemanticNegative500);
|
|
34
29
|
font-weight: 700;
|
|
35
|
-
margin-left:
|
|
30
|
+
margin-left: var(--spacing100);
|
|
36
31
|
}
|
|
37
32
|
`}
|
|
38
33
|
|
|
39
34
|
${({
|
|
40
|
-
disabled
|
|
41
|
-
theme
|
|
35
|
+
disabled
|
|
42
36
|
}) => disabled && (0, _styledComponents.css)`
|
|
43
|
-
color:
|
|
37
|
+
color: var(--colorsYin030);
|
|
44
38
|
`}
|
|
45
39
|
`;
|
|
46
|
-
LabelStyle.defaultProps = {
|
|
47
|
-
theme: _base.default
|
|
48
|
-
};
|
|
49
40
|
LabelStyle.propTypes = {
|
|
50
|
-
disabled: _propTypes.default.bool
|
|
41
|
+
disabled: _propTypes.default.bool,
|
|
42
|
+
isRequired: _propTypes.default.bool
|
|
51
43
|
};
|
|
52
44
|
const StyledLabelContainer = _styledComponents.default.div`
|
|
53
45
|
display: flex;
|
|
@@ -59,16 +51,15 @@ const StyledLabelContainer = _styledComponents.default.div`
|
|
|
59
51
|
inline,
|
|
60
52
|
pr,
|
|
61
53
|
pl,
|
|
62
|
-
width
|
|
63
|
-
theme
|
|
54
|
+
width
|
|
64
55
|
}) => inline && (0, _styledComponents.css)`
|
|
65
56
|
box-sizing: border-box;
|
|
66
57
|
margin-bottom: 0;
|
|
67
58
|
${pr && (0, _styledComponents.css)`
|
|
68
|
-
padding-right: ${pr
|
|
59
|
+
padding-right: var(${pr === 1 ? "--spacing100" : "--spacing200"});
|
|
69
60
|
`};
|
|
70
61
|
${pl && (0, _styledComponents.css)`
|
|
71
|
-
padding-left: ${pl
|
|
62
|
+
padding-left: var(${pl === 1 ? "--spacing100" : "--spacing200"});
|
|
72
63
|
`};
|
|
73
64
|
justify-content: ${align === "right" ? "flex-end" : "flex-start"};
|
|
74
65
|
width: ${width === 0 ? StyledLabelContainer.defaultProps.width : width}%;
|
|
@@ -79,7 +70,7 @@ const StyledLabelContainer = _styledComponents.default.div`
|
|
|
79
70
|
}) => optional && (0, _styledComponents.css)`
|
|
80
71
|
::after {
|
|
81
72
|
content: "(optional)";
|
|
82
|
-
font-weight: 350;
|
|
73
|
+
font-weight: 350; //TODO: (tokens) use token var(--fontWeights400)
|
|
83
74
|
margin-left: 4px;
|
|
84
75
|
}
|
|
85
76
|
`}
|
|
@@ -87,14 +78,12 @@ const StyledLabelContainer = _styledComponents.default.div`
|
|
|
87
78
|
exports.StyledLabelContainer = StyledLabelContainer;
|
|
88
79
|
StyledLabelContainer.defaultProps = {
|
|
89
80
|
align: "right",
|
|
90
|
-
theme: _base.default,
|
|
91
81
|
width: 30
|
|
92
82
|
};
|
|
93
83
|
StyledLabelContainer.propTypes = {
|
|
94
84
|
align: _propTypes.default.oneOf(["left", "right"]),
|
|
95
85
|
inline: _propTypes.default.bool,
|
|
96
86
|
width: _propTypes.default.number,
|
|
97
|
-
readOnly: _propTypes.default.bool,
|
|
98
87
|
pr: _propTypes.default.number,
|
|
99
88
|
pl: _propTypes.default.number
|
|
100
89
|
};
|
|
@@ -19,6 +19,8 @@ function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return
|
|
|
19
19
|
|
|
20
20
|
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
21
21
|
|
|
22
|
+
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
|
23
|
+
|
|
22
24
|
const ButtonToggleInput = /*#__PURE__*/_react.default.forwardRef((props, forwardRef) => {
|
|
23
25
|
const {
|
|
24
26
|
onFocus,
|
|
@@ -41,18 +43,13 @@ const ButtonToggleInput = /*#__PURE__*/_react.default.forwardRef((props, forward
|
|
|
41
43
|
if (onFocus) onFocus(ev);
|
|
42
44
|
};
|
|
43
45
|
|
|
44
|
-
return /*#__PURE__*/_react.default.createElement(_buttonToggle.StyledButtonToggleInput, {
|
|
46
|
+
return /*#__PURE__*/_react.default.createElement(_buttonToggle.StyledButtonToggleInput, _extends({}, props, {
|
|
45
47
|
type: "radio",
|
|
46
|
-
name: props.name,
|
|
47
48
|
id: props.guid,
|
|
48
|
-
disabled: props.disabled,
|
|
49
|
-
checked: props.checked,
|
|
50
|
-
onChange: props.onChange,
|
|
51
49
|
onBlur: handleBlur,
|
|
52
50
|
onFocus: handleFocus,
|
|
53
|
-
value: props.value,
|
|
54
51
|
ref: forwardRef
|
|
55
|
-
});
|
|
52
|
+
}));
|
|
56
53
|
});
|
|
57
54
|
|
|
58
55
|
ButtonToggleInput.propTypes = {
|
|
@@ -27,10 +27,15 @@ function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj;
|
|
|
27
27
|
|
|
28
28
|
const ButtonToggle = props => {
|
|
29
29
|
const {
|
|
30
|
+
"aria-label": ariaLabel,
|
|
31
|
+
"aria-labelledby": ariaLabelledBy,
|
|
30
32
|
buttonIcon,
|
|
31
33
|
buttonIconSize,
|
|
32
34
|
checked,
|
|
33
35
|
children,
|
|
36
|
+
"data-component": dataComponent,
|
|
37
|
+
"data-element": dataElement,
|
|
38
|
+
"data-role": dataRole,
|
|
34
39
|
disabled,
|
|
35
40
|
grouped,
|
|
36
41
|
name,
|
|
@@ -50,8 +55,10 @@ const ButtonToggle = props => {
|
|
|
50
55
|
|
|
51
56
|
if (buttonIcon) {
|
|
52
57
|
icon = /*#__PURE__*/_react.default.createElement(_buttonToggleIcon.default, {
|
|
58
|
+
"aria-hidden": true,
|
|
53
59
|
buttonIcon: buttonIcon,
|
|
54
60
|
buttonIconSize: buttonIconSize,
|
|
61
|
+
hasNoContent: !children,
|
|
55
62
|
disabled: disabled
|
|
56
63
|
});
|
|
57
64
|
}
|
|
@@ -61,10 +68,15 @@ const ButtonToggle = props => {
|
|
|
61
68
|
}
|
|
62
69
|
|
|
63
70
|
return /*#__PURE__*/_react.default.createElement(_buttonToggle.StyledButtonToggle, {
|
|
64
|
-
"data-component": "button-toggle",
|
|
71
|
+
"data-component": dataComponent || "button-toggle",
|
|
72
|
+
"data-element": dataElement,
|
|
73
|
+
"data-role": dataRole,
|
|
65
74
|
grouped: grouped,
|
|
66
75
|
onClick: handleClick
|
|
67
76
|
}, /*#__PURE__*/_react.default.createElement(_buttonToggleInput.default, {
|
|
77
|
+
"aria-label": ariaLabel,
|
|
78
|
+
"aria-labelledby": ariaLabelledBy,
|
|
79
|
+
"data-element": "button-toggle-input",
|
|
68
80
|
name: name,
|
|
69
81
|
checked: checked,
|
|
70
82
|
disabled: disabled,
|
|
@@ -86,9 +98,24 @@ const ButtonToggle = props => {
|
|
|
86
98
|
};
|
|
87
99
|
|
|
88
100
|
ButtonToggle.propTypes = {
|
|
101
|
+
/** Prop to specify the aria-label of the component */
|
|
102
|
+
"aria-label": _propTypes.default.string,
|
|
103
|
+
|
|
104
|
+
/** Prop to specify the aria-labelledby property of the component */
|
|
105
|
+
"aria-labelledby": _propTypes.default.string,
|
|
106
|
+
|
|
89
107
|
/** Set the checked value of the radio button */
|
|
90
108
|
checked: _propTypes.default.bool,
|
|
91
109
|
|
|
110
|
+
/** Identifier used for testing purposes, applied to the root element of the component. */
|
|
111
|
+
"data-component": _propTypes.default.string,
|
|
112
|
+
|
|
113
|
+
/** Identifier used for testing purposes, applied to the root element of the component. */
|
|
114
|
+
"data-element": _propTypes.default.string,
|
|
115
|
+
|
|
116
|
+
/** Identifier used for testing purposes, applied to the root element of the component. */
|
|
117
|
+
"data-role": _propTypes.default.string,
|
|
118
|
+
|
|
92
119
|
/** Name used on the hidden radio button. */
|
|
93
120
|
name: _propTypes.default.string,
|
|
94
121
|
|
|
@@ -2,10 +2,20 @@ import * as React from "react";
|
|
|
2
2
|
import { ButtonToggleIconSizes } from "./button-toggle-types";
|
|
3
3
|
|
|
4
4
|
export interface ButtonToggleProps {
|
|
5
|
+
/** Prop to specify the aria-label of the component */
|
|
6
|
+
"aria-label"?: string;
|
|
7
|
+
/** Prop to specify the aria-labelledby property of the component */
|
|
8
|
+
"aria-labelledby"?: string;
|
|
5
9
|
/** buttonIcon to render. */
|
|
6
10
|
buttonIcon?: string;
|
|
7
11
|
/** Sets the size of the buttonIcon (eg. large) */
|
|
8
12
|
buttonIconSize?: ButtonToggleIconSizes;
|
|
13
|
+
/** Identifier used for testing purposes, applied to the root element of the component. */
|
|
14
|
+
"data-component"?: string;
|
|
15
|
+
/** Identifier used for testing purposes, applied to the root element of the component. */
|
|
16
|
+
"data-element"?: string;
|
|
17
|
+
/** Identifier used for testing purposes, applied to the root element of the component. */
|
|
18
|
+
"data-role"?: string;
|
|
9
19
|
/** ButtonToggle size */
|
|
10
20
|
size?: "small" | "medium" | "large";
|
|
11
21
|
/** Set the checked value of the radio button */
|
|
@@ -134,8 +134,11 @@ const iconFontSizes = {
|
|
|
134
134
|
largeIcon: 32
|
|
135
135
|
};
|
|
136
136
|
const StyledButtonToggleIcon = _styledComponents.default.div`
|
|
137
|
-
|
|
138
|
-
|
|
137
|
+
${({
|
|
138
|
+
hasNoContent
|
|
139
|
+
}) => hasNoContent ? "" : (0, _styledComponents.css)`
|
|
140
|
+
margin-right: 8px;
|
|
141
|
+
`}
|
|
139
142
|
${({
|
|
140
143
|
buttonIconSize
|
|
141
144
|
}) => buttonIconSize === "large" && (0, _styledComponents.css)`
|
|
@@ -80,7 +80,7 @@ const ButtonToggleGroup = ({
|
|
|
80
80
|
"data-element": dataElement
|
|
81
81
|
}, validationProps, (0, _utils.filterStyledSystemMarginProps)(props)), /*#__PURE__*/_react.default.createElement(_buttonToggleGroup.default, _extends({
|
|
82
82
|
"aria-label": label,
|
|
83
|
-
role: "
|
|
83
|
+
role: "radiogroup",
|
|
84
84
|
inputWidth: inputWidth
|
|
85
85
|
}, validationProps), /*#__PURE__*/_react.default.createElement(_radioButtonMapper.default, {
|
|
86
86
|
name: name,
|
|
@@ -5,7 +5,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
7
|
|
|
8
|
-
var _react =
|
|
8
|
+
var _react = _interopRequireWildcard(require("react"));
|
|
9
9
|
|
|
10
10
|
var _propTypes = _interopRequireDefault(require("prop-types"));
|
|
11
11
|
|
|
@@ -21,268 +21,211 @@ var _i18nContext = _interopRequireDefault(require("../../__internal__/i18n-conte
|
|
|
21
21
|
|
|
22
22
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
23
23
|
|
|
24
|
-
function
|
|
25
|
-
|
|
26
|
-
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
27
|
-
|
|
28
|
-
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
|
29
|
-
|
|
30
|
-
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
|
31
|
-
|
|
32
|
-
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
|
|
33
|
-
|
|
34
|
-
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
|
35
|
-
|
|
36
|
-
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
|
|
37
|
-
|
|
38
|
-
function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
|
|
39
|
-
|
|
40
|
-
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
|
|
41
|
-
|
|
42
|
-
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
|
|
24
|
+
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
|
|
43
25
|
|
|
44
|
-
function
|
|
26
|
+
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
45
27
|
|
|
46
|
-
function
|
|
28
|
+
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
|
47
29
|
|
|
48
30
|
const marginPropTypes = (0, _utils.filterStyledSystemMarginProps)(_propTypes2.default.space);
|
|
49
31
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
_this.setState({
|
|
78
|
-
value: _this.toStandardDecimal(value),
|
|
79
|
-
visibleValue: value
|
|
80
|
-
}, () => {
|
|
81
|
-
_this.callOnChange();
|
|
82
|
-
});
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
_defineProperty(_assertThisInitialized(_this), "onBlur", () => {
|
|
86
|
-
let shouldCallOnChange = false;
|
|
87
|
-
|
|
88
|
-
_this.setState(({
|
|
89
|
-
value,
|
|
90
|
-
visibleValue
|
|
91
|
-
}) => {
|
|
92
|
-
if (!visibleValue) {
|
|
93
|
-
shouldCallOnChange = value !== _this.emptyValue;
|
|
94
|
-
return {
|
|
95
|
-
value: _this.emptyValue,
|
|
96
|
-
visibleValue: _this.formatValue(_this.emptyValue)
|
|
97
|
-
};
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
return {
|
|
101
|
-
visibleValue: _this.formatValue(value)
|
|
102
|
-
};
|
|
103
|
-
}, () => {
|
|
104
|
-
if (shouldCallOnChange) {
|
|
105
|
-
_this.callOnChange();
|
|
106
|
-
}
|
|
32
|
+
const Decimal = ({
|
|
33
|
+
align = "right",
|
|
34
|
+
defaultValue,
|
|
35
|
+
precision = 2,
|
|
36
|
+
inputWidth,
|
|
37
|
+
readOnly,
|
|
38
|
+
onChange,
|
|
39
|
+
onBlur,
|
|
40
|
+
onKeyPress,
|
|
41
|
+
id,
|
|
42
|
+
name,
|
|
43
|
+
allowEmptyValue = false,
|
|
44
|
+
required,
|
|
45
|
+
locale,
|
|
46
|
+
value,
|
|
47
|
+
...rest
|
|
48
|
+
}) => {
|
|
49
|
+
const l = (0, _react.useContext)(_i18nContext.default);
|
|
50
|
+
const emptyValue = allowEmptyValue ? "" : "0.00";
|
|
51
|
+
|
|
52
|
+
const getSafeValueProp = initialValue => {
|
|
53
|
+
// We're intentionally preventing the use of number values to help prevent any unintentional rounding issues
|
|
54
|
+
(0, _invariant.default)(typeof initialValue === "string", "Decimal `value` prop must be a string");
|
|
55
|
+
|
|
56
|
+
if (initialValue && !allowEmptyValue) {
|
|
57
|
+
(0, _invariant.default)(initialValue !== "", "Decimal `value` must not be an empty string. Please use `allowEmptyValue` or `0.00`");
|
|
58
|
+
}
|
|
107
59
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
}
|
|
111
|
-
});
|
|
112
|
-
});
|
|
60
|
+
return initialValue;
|
|
61
|
+
};
|
|
113
62
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
formattedValue
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
};
|
|
128
|
-
});
|
|
63
|
+
const getSeparator = (0, _react.useCallback)(separatorType => {
|
|
64
|
+
const numberWithGroupAndDecimalSeparator = 10000.1;
|
|
65
|
+
return Intl.NumberFormat(locale || l.locale()).formatToParts(numberWithGroupAndDecimalSeparator).find(part => part.type === separatorType).value;
|
|
66
|
+
}, [l, locale]);
|
|
67
|
+
const isNaN = (0, _react.useCallback)(valueToTest => {
|
|
68
|
+
return Number.isNaN(Number(valueToTest));
|
|
69
|
+
}, []);
|
|
70
|
+
/**
|
|
71
|
+
* Format a user defined value
|
|
72
|
+
*/
|
|
129
73
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
74
|
+
const formatValue = (0, _react.useCallback)(valueToFormat => {
|
|
75
|
+
if (isNaN(valueToFormat)) {
|
|
76
|
+
return valueToFormat;
|
|
77
|
+
}
|
|
78
|
+
/* Guards against any white-space only strings like " " being
|
|
79
|
+
mishandled and returned as `NaN` for the value displayed in the textbox */
|
|
133
80
|
|
|
134
|
-
_defineProperty(_assertThisInitialized(_this), "getSafeValueProp", isInitialValue => {
|
|
135
|
-
const {
|
|
136
|
-
value,
|
|
137
|
-
allowEmptyValue
|
|
138
|
-
} = _this.props; // We're intentionally preventing the use of number values to help prevent any unintentional rounding issues
|
|
139
81
|
|
|
140
|
-
|
|
82
|
+
if (valueToFormat === "" || valueToFormat.match(/\s+/g)) {
|
|
83
|
+
return valueToFormat;
|
|
84
|
+
}
|
|
141
85
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
86
|
+
const separator = getSeparator("decimal");
|
|
87
|
+
const [integer, remainder] = valueToFormat.split(".");
|
|
88
|
+
const formattedInteger = Intl.NumberFormat(locale || l.locale(), {
|
|
89
|
+
maximumFractionDigits: 0
|
|
90
|
+
}).format(integer);
|
|
91
|
+
let formattedNumber = formattedInteger;
|
|
92
|
+
|
|
93
|
+
if ((remainder === null || remainder === void 0 ? void 0 : remainder.length) > precision) {
|
|
94
|
+
formattedNumber += `${separator + remainder}`;
|
|
95
|
+
} else if ((remainder === null || remainder === void 0 ? void 0 : remainder.length) <= precision) {
|
|
96
|
+
formattedNumber += `${separator + remainder + "0".repeat(precision - remainder.length)}`;
|
|
97
|
+
} else {
|
|
98
|
+
formattedNumber += `${precision ? separator + "0".repeat(precision) : ""}`;
|
|
99
|
+
}
|
|
145
100
|
|
|
146
|
-
|
|
147
|
-
|
|
101
|
+
return formattedNumber;
|
|
102
|
+
}, [getSeparator, isNaN, l, locale, precision]); // Return previous value before state is changed. Used to compare prevState and newState.
|
|
148
103
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
104
|
+
function usePrevious(arg) {
|
|
105
|
+
const ref = (0, _react.useRef)();
|
|
106
|
+
(0, _react.useEffect)(() => {
|
|
107
|
+
ref.current = arg;
|
|
152
108
|
});
|
|
109
|
+
return ref.current;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Determine if the precision value has changed from the previous ref value for precision
|
|
113
|
+
*/
|
|
153
114
|
|
|
154
|
-
_defineProperty(_assertThisInitialized(_this), "formatValue", value => {
|
|
155
|
-
if (_this.isNaN(value)) {
|
|
156
|
-
return value;
|
|
157
|
-
}
|
|
158
|
-
/* Guards against any white-space only strings like " " being
|
|
159
|
-
mishandled and returned as `NaN` for visibleValue */
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
if (value === "" || value.match(/\s+/g)) {
|
|
163
|
-
return value;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
const separator = _this.getSeparator("decimal");
|
|
167
|
-
|
|
168
|
-
const [integer, remainder] = value.split(".");
|
|
169
|
-
const formattedInteger = Intl.NumberFormat(_this.props.locale || _this.context.locale(), {
|
|
170
|
-
maximumFractionDigits: 0
|
|
171
|
-
}).format(integer);
|
|
172
|
-
let formattedNumber = formattedInteger;
|
|
173
|
-
|
|
174
|
-
if (remainder && remainder.length > _this.props.precision) {
|
|
175
|
-
formattedNumber += `${separator + remainder}`;
|
|
176
|
-
} else if (remainder && remainder.length <= _this.props.precision) {
|
|
177
|
-
formattedNumber += `${separator + remainder + "0".repeat(_this.props.precision - remainder.length)}`;
|
|
178
|
-
} else {
|
|
179
|
-
formattedNumber += `${_this.props.precision ? separator + "0".repeat(_this.props.precision) : ""}`;
|
|
180
|
-
}
|
|
181
115
|
|
|
182
|
-
|
|
183
|
-
|
|
116
|
+
const prevPrecisionValue = usePrevious(precision);
|
|
117
|
+
(0, _react.useEffect)(() => {
|
|
118
|
+
if (prevPrecisionValue && prevPrecisionValue !== precision) {
|
|
119
|
+
// eslint-disable-next-line no-console
|
|
120
|
+
console.error("Decimal `precision` prop has changed value. Changing the Decimal `precision` prop has no effect.");
|
|
121
|
+
}
|
|
122
|
+
}, [precision]);
|
|
123
|
+
const removeDelimiters = (0, _react.useCallback)(valueToFormat => {
|
|
124
|
+
const delimiterMatcher = new RegExp(`[\\${getSeparator("group")} ]*`, "g");
|
|
125
|
+
return valueToFormat.replace(delimiterMatcher, "");
|
|
126
|
+
}, [getSeparator]);
|
|
127
|
+
/**
|
|
128
|
+
* Convert raw input to a standard decimal format
|
|
129
|
+
*/
|
|
184
130
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
131
|
+
const toStandardDecimal = (0, _react.useCallback)(i18nValue => {
|
|
132
|
+
const valueWithoutNBS = getSeparator("group").match(/\s+/) && !i18nValue.match(/\s{2,}/) ? i18nValue.replace(/\s+/g, "") : i18nValue;
|
|
133
|
+
/* If a value is passed in that is a number but has too many delimiters in succession, we want to handle this
|
|
134
|
+
value without formatting it or removing delimiters. We also want to consider that,
|
|
135
|
+
if a value consists of only delimiters, we want to treat that
|
|
136
|
+
value in the same way as if the value was NaN. We want to pass this value to the
|
|
137
|
+
formatValue function, before the delimiters can be removed. */
|
|
192
138
|
|
|
193
|
-
|
|
139
|
+
const errorsWithDelimiter = new RegExp(`([^A-Za-z0-9]{2,})|(^[^A-Za-z0-9-]+)|([^0-9a-z-,.])|([^0-9-,.]+)|([W,.])$`, "g");
|
|
140
|
+
const separator = getSeparator("decimal");
|
|
141
|
+
const separatorRegex = new RegExp(separator === "." ? `\\${separator}` : separator, "g");
|
|
194
142
|
|
|
195
|
-
|
|
143
|
+
if (valueWithoutNBS.match(errorsWithDelimiter) || (valueWithoutNBS.match(separatorRegex) || []).length > 1) {
|
|
144
|
+
return valueWithoutNBS;
|
|
145
|
+
}
|
|
196
146
|
|
|
197
|
-
|
|
147
|
+
const withoutDelimiters = removeDelimiters(valueWithoutNBS);
|
|
148
|
+
return withoutDelimiters.replace(new RegExp(`\\${separator}`, "g"), ".");
|
|
149
|
+
}, [getSeparator, removeDelimiters]);
|
|
150
|
+
const decimalValue = getSafeValueProp(defaultValue || value || emptyValue);
|
|
151
|
+
const [stateValue, setStateValue] = (0, _react.useState)(isNaN(toStandardDecimal(decimalValue)) ? decimalValue : formatValue(decimalValue));
|
|
198
152
|
|
|
199
|
-
|
|
200
|
-
|
|
153
|
+
const createEvent = (formatted, raw) => {
|
|
154
|
+
return {
|
|
155
|
+
target: {
|
|
156
|
+
name,
|
|
157
|
+
id,
|
|
158
|
+
value: {
|
|
159
|
+
formattedValue: formatValue(toStandardDecimal(formatted)),
|
|
160
|
+
rawValue: raw || toStandardDecimal(formatted)
|
|
161
|
+
}
|
|
201
162
|
}
|
|
202
|
-
|
|
203
|
-
const withoutDelimiters = _this.removeDelimiters(valueWithoutNBS);
|
|
204
|
-
|
|
205
|
-
return withoutDelimiters.replace(new RegExp(`\\${separator}`, "g"), ".");
|
|
206
|
-
});
|
|
207
|
-
|
|
208
|
-
const isControlled = _this.isControlled();
|
|
209
|
-
|
|
210
|
-
const _value = isControlled ? _this.getSafeValueProp(true) : _this.props.defaultValue || _this.emptyValue;
|
|
211
|
-
|
|
212
|
-
_this.state = {
|
|
213
|
-
value: _value,
|
|
214
|
-
visibleValue: _this.formatValue(_value),
|
|
215
|
-
isControlled
|
|
216
163
|
};
|
|
217
|
-
|
|
218
|
-
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
const handleOnChange = ev => {
|
|
167
|
+
const {
|
|
168
|
+
value: val
|
|
169
|
+
} = ev.target;
|
|
170
|
+
setStateValue(val);
|
|
171
|
+
if (onChange) onChange(createEvent(val));
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
const handleOnBlur = ev => {
|
|
175
|
+
const {
|
|
176
|
+
value: updatedValue
|
|
177
|
+
} = ev.target;
|
|
178
|
+
let event;
|
|
179
|
+
|
|
180
|
+
if (updatedValue) {
|
|
181
|
+
const standardVisible = toStandardDecimal(updatedValue);
|
|
182
|
+
const formattedValue = isNaN(standardVisible) ? updatedValue : formatValue(standardVisible);
|
|
183
|
+
event = createEvent(formattedValue, standardVisible);
|
|
184
|
+
setStateValue(formattedValue);
|
|
185
|
+
} else {
|
|
186
|
+
event = createEvent(emptyValue);
|
|
187
|
+
setStateValue(emptyValue);
|
|
188
|
+
}
|
|
219
189
|
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
value: function componentDidUpdate(prevProps, prevState) {
|
|
223
|
-
const message = "Input elements should not switch from uncontrolled to controlled (or vice versa). " + "Decide between using a controlled or uncontrolled input element for the lifetime of the component";
|
|
224
|
-
const isControlled = this.isControlled();
|
|
225
|
-
(0, _invariant.default)(this.state.isControlled === isControlled, message);
|
|
190
|
+
if (onBlur) onBlur(event);
|
|
191
|
+
};
|
|
226
192
|
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
193
|
+
const isComponentControlled = value !== undefined;
|
|
194
|
+
const prevControlledState = usePrevious(isComponentControlled);
|
|
195
|
+
(0, _react.useEffect)(() => {
|
|
196
|
+
const message = "Input elements should not switch from uncontrolled to controlled (or vice versa). " + "Decide between using a controlled or uncontrolled input element for the lifetime of the component";
|
|
197
|
+
(0, _invariant.default)(prevControlledState !== isComponentControlled, message);
|
|
198
|
+
}, [isComponentControlled]);
|
|
199
|
+
(0, _react.useEffect)(() => {
|
|
200
|
+
const unformattedValue = toStandardDecimal(stateValue);
|
|
231
201
|
|
|
232
|
-
|
|
233
|
-
|
|
202
|
+
if (isComponentControlled) {
|
|
203
|
+
const valueProp = getSafeValueProp(value);
|
|
234
204
|
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
value: valueProp,
|
|
238
|
-
visibleValue: this.formatValue(valueProp)
|
|
239
|
-
});
|
|
240
|
-
}
|
|
205
|
+
if (unformattedValue !== valueProp) {
|
|
206
|
+
setStateValue(formatValue(value));
|
|
241
207
|
}
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
name,
|
|
263
|
-
defaultValue,
|
|
264
|
-
locale,
|
|
265
|
-
...rest
|
|
266
|
-
} = this.props;
|
|
267
|
-
return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_textbox.default, _extends({}, rest, {
|
|
268
|
-
onChange: this.onChange,
|
|
269
|
-
onBlur: this.onBlur,
|
|
270
|
-
value: this.state.visibleValue
|
|
271
|
-
})), /*#__PURE__*/_react.default.createElement("input", {
|
|
272
|
-
name: name,
|
|
273
|
-
value: this.toStandardDecimal(this.state.visibleValue),
|
|
274
|
-
type: "hidden",
|
|
275
|
-
"data-component": "hidden-input"
|
|
276
|
-
}));
|
|
277
|
-
}
|
|
278
|
-
}]);
|
|
279
|
-
|
|
280
|
-
return Decimal;
|
|
281
|
-
}(_react.default.Component);
|
|
282
|
-
|
|
283
|
-
_defineProperty(Decimal, "maxPrecision", 15);
|
|
208
|
+
} // eslint-disable-next-line react-hooks/exhaustive-deps
|
|
209
|
+
|
|
210
|
+
}, [value]);
|
|
211
|
+
return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_textbox.default, _extends({
|
|
212
|
+
onKeyPress: onKeyPress,
|
|
213
|
+
align: align,
|
|
214
|
+
readOnly: readOnly,
|
|
215
|
+
required: required,
|
|
216
|
+
inputWidth: inputWidth,
|
|
217
|
+
onChange: handleOnChange,
|
|
218
|
+
onBlur: handleOnBlur,
|
|
219
|
+
value: stateValue,
|
|
220
|
+
"data-component": "decimal"
|
|
221
|
+
}, rest)), /*#__PURE__*/_react.default.createElement("input", {
|
|
222
|
+
name: name,
|
|
223
|
+
value: toStandardDecimal(stateValue),
|
|
224
|
+
type: "hidden",
|
|
225
|
+
"data-component": "hidden-input"
|
|
226
|
+
}));
|
|
227
|
+
};
|
|
284
228
|
|
|
285
|
-
Decimal.contextType = _i18nContext.default;
|
|
286
229
|
Decimal.propTypes = {
|
|
287
230
|
/** Styled-system margin props */
|
|
288
231
|
...marginPropTypes,
|
|
@@ -300,7 +243,7 @@ Decimal.propTypes = {
|
|
|
300
243
|
*/
|
|
301
244
|
// eslint-disable-next-line consistent-return
|
|
302
245
|
precision: props => {
|
|
303
|
-
if (!Number.isInteger(props.precision) || props.precision < 0 || props.precision >
|
|
246
|
+
if (!Number.isInteger(props.precision) || props.precision < 0 || props.precision > 15) {
|
|
304
247
|
return new Error("Precision prop must be a number greater than 0 or equal to or less than 15.");
|
|
305
248
|
}
|
|
306
249
|
},
|
|
@@ -366,11 +309,5 @@ Decimal.propTypes = {
|
|
|
366
309
|
/** Aria label for rendered help component */
|
|
367
310
|
helpAriaLabel: _propTypes.default.string
|
|
368
311
|
};
|
|
369
|
-
Decimal.defaultProps = {
|
|
370
|
-
align: "right",
|
|
371
|
-
precision: 2,
|
|
372
|
-
allowEmptyValue: false,
|
|
373
|
-
"data-component": "decimal"
|
|
374
|
-
};
|
|
375
312
|
var _default = Decimal;
|
|
376
313
|
exports.default = _default;
|
|
@@ -37,10 +37,9 @@ const RadioButtonStyle = (0, _styledComponents.default)(_checkbox.default)`
|
|
|
37
37
|
fieldHelpInline,
|
|
38
38
|
reverse,
|
|
39
39
|
size,
|
|
40
|
-
theme,
|
|
41
40
|
inline
|
|
42
41
|
}) => (0, _styledComponents.css)`
|
|
43
|
-
margin-bottom:
|
|
42
|
+
margin-bottom: var(--spacing150);
|
|
44
43
|
|
|
45
44
|
:last-of-type {
|
|
46
45
|
margin-bottom: 0;
|
|
@@ -79,16 +78,16 @@ const RadioButtonStyle = (0, _styledComponents.default)(_checkbox.default)`
|
|
|
79
78
|
}
|
|
80
79
|
|
|
81
80
|
${_hiddenCheckableInput.default}:checked + ${_checkableInputSvgWrapper.default} circle {
|
|
82
|
-
fill:
|
|
81
|
+
fill: var(--colorsUtilityYin090);
|
|
83
82
|
}
|
|
84
83
|
|
|
85
84
|
${disabled && (0, _styledComponents.css)`
|
|
86
85
|
circle {
|
|
87
|
-
fill:
|
|
86
|
+
fill: var(--colorsCtilityDisabled400);
|
|
88
87
|
}
|
|
89
88
|
|
|
90
89
|
${_hiddenCheckableInput.default}:checked + ${_checkableInputSvgWrapper.default} circle {
|
|
91
|
-
fill:
|
|
90
|
+
fill: var(--colorsUtilityDisabled600);
|
|
92
91
|
}
|
|
93
92
|
`}
|
|
94
93
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "carbon-react",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "102.2.0",
|
|
4
4
|
"description": "A library of reusable React components for easily building user interfaces.",
|
|
5
5
|
"engineStrict": true,
|
|
6
6
|
"engines": {
|
|
@@ -17,7 +17,8 @@
|
|
|
17
17
|
"start:debug-theme": "cross-env STORYBOOK_DEBUG_THEME=true npm run start",
|
|
18
18
|
"test": "jest --config=./jest.conf.json",
|
|
19
19
|
"test-update": "jest --config=./jest.conf.json --updateSnapshot",
|
|
20
|
-
"test
|
|
20
|
+
"test:cypress": "cypress open",
|
|
21
|
+
"cypress:react": "npx cypress open-ct",
|
|
21
22
|
"debug": "node --inspect ./node_modules/jest-cli/bin/jest --watch --config=./jest.conf.json",
|
|
22
23
|
"format": "prettier --write './src'",
|
|
23
24
|
"lint": "eslint ./src",
|
|
@@ -27,7 +28,7 @@
|
|
|
27
28
|
"watch": "npm run clean-lib && npm run copy-files -- --watch & npm run babel -- --watch",
|
|
28
29
|
"build-storybook": "build-storybook -c .storybook -s .assets",
|
|
29
30
|
"start:static": "npx http-server -p 9001 ./storybook-static",
|
|
30
|
-
"babel": "cross-env NODE_ENV=production babel ./src --config-file ./babel.config.js --out-dir ./lib --ignore '**/*/__spec__.js','**/*.spec.js','**/__definition__.js' --quiet",
|
|
31
|
+
"babel": "cross-env NODE_ENV=production babel ./src --config-file ./babel.config.js --out-dir ./lib --ignore '**/*/__spec__.js','**/*.spec.js','**/__definition__.js','**/*.test.js' --quiet",
|
|
31
32
|
"clean-lib": "rimraf ./lib",
|
|
32
33
|
"copy-files": "cpy \"**/\" \"!**/(*.js|*.md|*.mdx|*.stories.*|*.snap)\" ../lib/ --cwd=src --parents",
|
|
33
34
|
"commit": "git-cz",
|
|
@@ -59,6 +60,8 @@
|
|
|
59
60
|
"@babel/preset-typescript": "7.12.7",
|
|
60
61
|
"@commitlint/cli": "^11.0.0",
|
|
61
62
|
"@commitlint/config-conventional": "^11.0.0",
|
|
63
|
+
"@cypress/react": "^5.9.2",
|
|
64
|
+
"@cypress/webpack-dev-server": "^1.4.0",
|
|
62
65
|
"@semantic-release/changelog": "^6.0.1",
|
|
63
66
|
"@semantic-release/exec": "^6.0.2",
|
|
64
67
|
"@semantic-release/git": "^10.0.1",
|
|
@@ -136,7 +139,8 @@
|
|
|
136
139
|
"semver": "^7.3.5",
|
|
137
140
|
"sprintf-js": "^1.1.2",
|
|
138
141
|
"styled-components": "^4.4.1",
|
|
139
|
-
"typescript": "^3.9.5"
|
|
142
|
+
"typescript": "^3.9.5",
|
|
143
|
+
"webpack-dev-server": "^3.11.2"
|
|
140
144
|
},
|
|
141
145
|
"dependencies": {
|
|
142
146
|
"@octokit/rest": "^18.12.0",
|