@automattic/vip-design-system 0.27.1 → 0.27.3
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/build/system/Form/Radio.js +80 -59
- package/package.json +1 -1
- package/src/system/Form/Radio.js +76 -48
|
@@ -19,15 +19,25 @@ var _Label = require("./Label");
|
|
|
19
19
|
|
|
20
20
|
var _ScreenReaderText = require("../ScreenReaderText/ScreenReaderText");
|
|
21
21
|
|
|
22
|
+
var _theme = _interopRequireDefault(require("../theme"));
|
|
23
|
+
|
|
22
24
|
var _jsxRuntime = require("theme-ui/jsx-runtime");
|
|
23
25
|
|
|
24
|
-
var _excluded = ["
|
|
25
|
-
_excluded2 = ["id", "
|
|
26
|
+
var _excluded = ["children"],
|
|
27
|
+
_excluded2 = ["id", "value", "className", "label", "labelProps"],
|
|
28
|
+
_excluded3 = ["disabled", "defaultValue", "onChange", "name", "options", "className"];
|
|
26
29
|
|
|
27
30
|
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
28
31
|
|
|
29
32
|
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && 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; }
|
|
30
33
|
|
|
34
|
+
var prefix = 'vip-radio-component-';
|
|
35
|
+
var itemStyle = {
|
|
36
|
+
display: 'flex',
|
|
37
|
+
alignItems: 'center',
|
|
38
|
+
my: 2
|
|
39
|
+
};
|
|
40
|
+
var radioPosition = _theme["default"].space[4] - _theme["default"].space[1];
|
|
31
41
|
var inputStyle = (0, _extends2["default"])({}, _ScreenReaderText.screenReaderTextClass, {
|
|
32
42
|
width: '16px',
|
|
33
43
|
height: '16px',
|
|
@@ -37,7 +47,7 @@ var inputStyle = (0, _extends2["default"])({}, _ScreenReaderText.screenReaderTex
|
|
|
37
47
|
border: '2px solid',
|
|
38
48
|
borderColor: 'border',
|
|
39
49
|
zIndex: 3,
|
|
40
|
-
left: -1 *
|
|
50
|
+
left: -1 * radioPosition + "px"
|
|
41
51
|
});
|
|
42
52
|
},
|
|
43
53
|
'&:checked ~ label::after': {
|
|
@@ -48,9 +58,7 @@ var inputStyle = (0, _extends2["default"])({}, _ScreenReaderText.screenReaderTex
|
|
|
48
58
|
var labelStyle = {
|
|
49
59
|
cursor: 'pointer',
|
|
50
60
|
position: 'relative',
|
|
51
|
-
marginLeft:
|
|
52
|
-
return theme.space[4] - theme.space[2] + "px";
|
|
53
|
-
},
|
|
61
|
+
marginLeft: radioPosition + "px",
|
|
54
62
|
marginBottom: 0,
|
|
55
63
|
userSelect: 'none',
|
|
56
64
|
color: 'heading',
|
|
@@ -59,9 +67,7 @@ var labelStyle = {
|
|
|
59
67
|
borderRadius: '50%',
|
|
60
68
|
position: 'absolute',
|
|
61
69
|
top: 0,
|
|
62
|
-
left:
|
|
63
|
-
return -1 * (theme.space[4] - theme.space[2]) + "px";
|
|
64
|
-
},
|
|
70
|
+
left: -1 * radioPosition + "px",
|
|
65
71
|
transition: 'all .3s ease-out',
|
|
66
72
|
zIndex: 2,
|
|
67
73
|
width: '16px',
|
|
@@ -88,12 +94,13 @@ var labelStyle = {
|
|
|
88
94
|
};
|
|
89
95
|
|
|
90
96
|
var CustomLabel = function CustomLabel(_ref) {
|
|
91
|
-
var children = _ref.children
|
|
97
|
+
var children = _ref.children,
|
|
98
|
+
restLabel = (0, _objectWithoutPropertiesLoose2["default"])(_ref, _excluded);
|
|
92
99
|
return (0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
|
|
93
100
|
children: /*#__PURE__*/_react["default"].cloneElement(_react["default"].Children.only(children), (0, _extends2["default"])({}, children.props, {
|
|
94
101
|
sx: (0, _extends2["default"])({}, labelStyle, children.props.sx),
|
|
95
|
-
className: children.props.className + "
|
|
96
|
-
}))
|
|
102
|
+
className: children.props.className + " " + prefix + "item-label"
|
|
103
|
+
}, restLabel))
|
|
97
104
|
});
|
|
98
105
|
};
|
|
99
106
|
|
|
@@ -101,61 +108,75 @@ CustomLabel.propTypes = {
|
|
|
101
108
|
children: _propTypes["default"].any
|
|
102
109
|
};
|
|
103
110
|
|
|
104
|
-
var
|
|
105
|
-
var
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
_ref2$
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
111
|
+
var RadioOption = function RadioOption(_ref2) {
|
|
112
|
+
var _ref2$option = _ref2.option,
|
|
113
|
+
id = _ref2$option.id,
|
|
114
|
+
value = _ref2$option.value,
|
|
115
|
+
className = _ref2$option.className,
|
|
116
|
+
label = _ref2$option.label,
|
|
117
|
+
_ref2$option$labelPro = _ref2$option.labelProps,
|
|
118
|
+
labelProps = _ref2$option$labelPro === void 0 ? {} : _ref2$option$labelPro,
|
|
119
|
+
restOption = (0, _objectWithoutPropertiesLoose2["default"])(_ref2$option, _excluded2),
|
|
120
|
+
name = _ref2.name,
|
|
121
|
+
onChangeHandler = _ref2.onChangeHandler,
|
|
122
|
+
checked = _ref2.checked;
|
|
123
|
+
return (0, _jsxRuntime.jsxs)("div", {
|
|
124
|
+
sx: itemStyle,
|
|
125
|
+
className: (0, _classnames["default"])(prefix + "item", prefix + "item-" + id, checked ? prefix + "item-checked" : '', className),
|
|
126
|
+
children: [(0, _jsxRuntime.jsx)("input", (0, _extends2["default"])({
|
|
127
|
+
type: "radio",
|
|
128
|
+
id: id,
|
|
129
|
+
name: name,
|
|
130
|
+
value: "" + value,
|
|
131
|
+
sx: inputStyle,
|
|
132
|
+
onChange: onChangeHandler,
|
|
133
|
+
className: prefix + "item-input",
|
|
134
|
+
checked: checked
|
|
135
|
+
}, restOption)), typeof label === 'string' ? (0, _jsxRuntime.jsx)(_Label.Label, (0, _extends2["default"])({
|
|
136
|
+
className: prefix + "item-label",
|
|
137
|
+
htmlFor: id,
|
|
138
|
+
sx: labelStyle
|
|
139
|
+
}, labelProps, {
|
|
140
|
+
children: label
|
|
141
|
+
})) : (0, _jsxRuntime.jsx)(CustomLabel, (0, _extends2["default"])({}, labelProps, {
|
|
142
|
+
children: label
|
|
143
|
+
}))]
|
|
144
|
+
});
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
RadioOption.propTypes = {
|
|
148
|
+
option: _propTypes["default"].object,
|
|
149
|
+
name: _propTypes["default"].string,
|
|
150
|
+
onChangeHandler: _propTypes["default"].func,
|
|
151
|
+
checked: _propTypes["default"].bool
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
var Radio = /*#__PURE__*/_react["default"].forwardRef(function (_ref3, forwardRef) {
|
|
155
|
+
var disabled = _ref3.disabled,
|
|
156
|
+
defaultValue = _ref3.defaultValue,
|
|
157
|
+
onChange = _ref3.onChange,
|
|
158
|
+
_ref3$name = _ref3.name,
|
|
159
|
+
name = _ref3$name === void 0 ? '' : _ref3$name,
|
|
160
|
+
_ref3$options = _ref3.options,
|
|
161
|
+
options = _ref3$options === void 0 ? [] : _ref3$options,
|
|
162
|
+
className = _ref3.className,
|
|
163
|
+
props = (0, _objectWithoutPropertiesLoose2["default"])(_ref3, _excluded3);
|
|
114
164
|
var onChangeHandler = (0, _react.useCallback)(function (e) {
|
|
115
165
|
var optionTriggered = options.find(function (option) {
|
|
116
166
|
return "" + option.value === "" + e.target.value;
|
|
117
167
|
});
|
|
118
168
|
onChange(e, optionTriggered);
|
|
119
169
|
}, []);
|
|
120
|
-
var renderedOptions = options.map(function (
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
restOption = (0, _objectWithoutPropertiesLoose2["default"])(_ref3, _excluded2);
|
|
128
|
-
return (0, _jsxRuntime.jsxs)("div", {
|
|
129
|
-
sx: {
|
|
130
|
-
display: 'flex',
|
|
131
|
-
alignItems: 'center',
|
|
132
|
-
minHeight: function minHeight(theme) {
|
|
133
|
-
return theme.space[4] - theme.space[2] + "px";
|
|
134
|
-
}
|
|
135
|
-
},
|
|
136
|
-
className: (0, _classnames["default"])('vip-radio-component-item', "vip-radio-component-item-" + id),
|
|
137
|
-
children: [(0, _jsxRuntime.jsx)("input", (0, _extends2["default"])({
|
|
138
|
-
type: "radio",
|
|
139
|
-
id: id,
|
|
140
|
-
name: name,
|
|
141
|
-
value: "" + value,
|
|
142
|
-
sx: inputStyle,
|
|
143
|
-
onChange: onChangeHandler,
|
|
144
|
-
className: (0, _classnames["default"])('vip-radio-component-item-input', optionClassName),
|
|
145
|
-
checked: "" + value === "" + defaultValue
|
|
146
|
-
}, restOption)), typeof label === 'string' ? (0, _jsxRuntime.jsx)(_Label.Label, (0, _extends2["default"])({
|
|
147
|
-
className: (0, _classnames["default"])('vip-radio-component-item-label', optionClassName),
|
|
148
|
-
htmlFor: id,
|
|
149
|
-
sx: labelStyle
|
|
150
|
-
}, labelProps, {
|
|
151
|
-
children: label
|
|
152
|
-
})) : (0, _jsxRuntime.jsx)(CustomLabel, (0, _extends2["default"])({}, labelProps, {
|
|
153
|
-
children: label
|
|
154
|
-
}))]
|
|
155
|
-
}, id);
|
|
170
|
+
var renderedOptions = options.map(function (option) {
|
|
171
|
+
return (0, _jsxRuntime.jsx)(RadioOption, {
|
|
172
|
+
name: name,
|
|
173
|
+
option: option,
|
|
174
|
+
onChangeHandler: onChangeHandler,
|
|
175
|
+
checked: "" + defaultValue === "" + (option == null ? void 0 : option.value)
|
|
176
|
+
}, option == null ? void 0 : option.id);
|
|
156
177
|
});
|
|
157
178
|
return (0, _jsxRuntime.jsx)("div", (0, _extends2["default"])({
|
|
158
|
-
className: (0, _classnames["default"])(
|
|
179
|
+
className: (0, _classnames["default"])(prefix, "" + prefix + name, className),
|
|
159
180
|
ref: forwardRef
|
|
160
181
|
}, props, {
|
|
161
182
|
children: renderedOptions
|
package/package.json
CHANGED
package/src/system/Form/Radio.js
CHANGED
|
@@ -9,6 +9,17 @@ import PropTypes from 'prop-types';
|
|
|
9
9
|
import classNames from 'classnames';
|
|
10
10
|
import { Label } from './Label';
|
|
11
11
|
import { screenReaderTextClass } from '../ScreenReaderText/ScreenReaderText';
|
|
12
|
+
import theme from '../theme';
|
|
13
|
+
|
|
14
|
+
const prefix = 'vip-radio-component-';
|
|
15
|
+
|
|
16
|
+
const itemStyle = {
|
|
17
|
+
display: 'flex',
|
|
18
|
+
alignItems: 'center',
|
|
19
|
+
my: 2,
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const radioPosition = theme.space[ 4 ] - theme.space[ 1 ];
|
|
12
23
|
|
|
13
24
|
const inputStyle = {
|
|
14
25
|
...screenReaderTextClass,
|
|
@@ -20,7 +31,7 @@ const inputStyle = {
|
|
|
20
31
|
border: '2px solid',
|
|
21
32
|
borderColor: 'border',
|
|
22
33
|
zIndex: 3,
|
|
23
|
-
left: `${ -1 *
|
|
34
|
+
left: `${ -1 * radioPosition }px`,
|
|
24
35
|
} ),
|
|
25
36
|
'&:checked ~ label::after': {
|
|
26
37
|
opacity: 1,
|
|
@@ -31,7 +42,7 @@ const inputStyle = {
|
|
|
31
42
|
const labelStyle = {
|
|
32
43
|
cursor: 'pointer',
|
|
33
44
|
position: 'relative',
|
|
34
|
-
marginLeft:
|
|
45
|
+
marginLeft: `${ radioPosition }px`,
|
|
35
46
|
marginBottom: 0,
|
|
36
47
|
userSelect: 'none',
|
|
37
48
|
color: 'heading',
|
|
@@ -40,7 +51,7 @@ const labelStyle = {
|
|
|
40
51
|
borderRadius: '50%',
|
|
41
52
|
position: 'absolute',
|
|
42
53
|
top: 0,
|
|
43
|
-
left:
|
|
54
|
+
left: `${ -1 * radioPosition }px`,
|
|
44
55
|
transition: 'all .3s ease-out',
|
|
45
56
|
zIndex: 2,
|
|
46
57
|
width: '16px',
|
|
@@ -68,13 +79,14 @@ const labelStyle = {
|
|
|
68
79
|
},
|
|
69
80
|
};
|
|
70
81
|
|
|
71
|
-
const CustomLabel = ( { children } ) => {
|
|
82
|
+
const CustomLabel = ( { children, ...restLabel } ) => {
|
|
72
83
|
return (
|
|
73
84
|
<>
|
|
74
85
|
{ React.cloneElement( React.Children.only( children ), {
|
|
75
86
|
...children.props,
|
|
76
87
|
sx: { ...labelStyle, ...children.props.sx },
|
|
77
|
-
className: `${ children.props.className }
|
|
88
|
+
className: `${ children.props.className } ${ prefix }item-label`,
|
|
89
|
+
...restLabel,
|
|
78
90
|
} ) }
|
|
79
91
|
</>
|
|
80
92
|
);
|
|
@@ -84,6 +96,55 @@ CustomLabel.propTypes = {
|
|
|
84
96
|
children: PropTypes.any,
|
|
85
97
|
};
|
|
86
98
|
|
|
99
|
+
const RadioOption = ( {
|
|
100
|
+
option: { id, value, className, label, labelProps = {}, ...restOption },
|
|
101
|
+
name,
|
|
102
|
+
onChangeHandler,
|
|
103
|
+
checked,
|
|
104
|
+
} ) => (
|
|
105
|
+
<div
|
|
106
|
+
sx={ itemStyle }
|
|
107
|
+
className={ classNames(
|
|
108
|
+
`${ prefix }item`,
|
|
109
|
+
`${ prefix }item-${ id }`,
|
|
110
|
+
checked ? `${ prefix }item-checked` : '',
|
|
111
|
+
className
|
|
112
|
+
) }
|
|
113
|
+
>
|
|
114
|
+
<input
|
|
115
|
+
type="radio"
|
|
116
|
+
id={ id }
|
|
117
|
+
name={ name }
|
|
118
|
+
value={ `${ value }` }
|
|
119
|
+
sx={ inputStyle }
|
|
120
|
+
onChange={ onChangeHandler }
|
|
121
|
+
className={ `${ prefix }item-input` }
|
|
122
|
+
checked={ checked }
|
|
123
|
+
{ ...restOption }
|
|
124
|
+
/>
|
|
125
|
+
|
|
126
|
+
{ typeof label === 'string' ? (
|
|
127
|
+
<Label
|
|
128
|
+
className={ `${ prefix }item-label` }
|
|
129
|
+
htmlFor={ id }
|
|
130
|
+
sx={ labelStyle }
|
|
131
|
+
{ ...labelProps }
|
|
132
|
+
>
|
|
133
|
+
{ label }
|
|
134
|
+
</Label>
|
|
135
|
+
) : (
|
|
136
|
+
<CustomLabel { ...labelProps }>{ label }</CustomLabel>
|
|
137
|
+
) }
|
|
138
|
+
</div>
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
RadioOption.propTypes = {
|
|
142
|
+
option: PropTypes.object,
|
|
143
|
+
name: PropTypes.string,
|
|
144
|
+
onChangeHandler: PropTypes.func,
|
|
145
|
+
checked: PropTypes.bool,
|
|
146
|
+
};
|
|
147
|
+
|
|
87
148
|
const Radio = React.forwardRef(
|
|
88
149
|
(
|
|
89
150
|
{ disabled, defaultValue, onChange, name = '', options = [], className, ...props },
|
|
@@ -96,52 +157,19 @@ const Radio = React.forwardRef(
|
|
|
96
157
|
onChange( e, optionTriggered );
|
|
97
158
|
}, [] );
|
|
98
159
|
|
|
99
|
-
const renderedOptions = options.map(
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
className={ classNames( 'vip-radio-component-item', `vip-radio-component-item-${ id }` ) }
|
|
109
|
-
>
|
|
110
|
-
<input
|
|
111
|
-
type="radio"
|
|
112
|
-
id={ id }
|
|
113
|
-
name={ name }
|
|
114
|
-
value={ `${ value }` }
|
|
115
|
-
sx={ inputStyle }
|
|
116
|
-
onChange={ onChangeHandler }
|
|
117
|
-
className={ classNames( 'vip-radio-component-item-input', optionClassName ) }
|
|
118
|
-
checked={ `${ value }` === `${ defaultValue }` }
|
|
119
|
-
{ ...restOption }
|
|
120
|
-
/>
|
|
121
|
-
|
|
122
|
-
{ typeof label === 'string' ? (
|
|
123
|
-
<Label
|
|
124
|
-
className={ classNames( 'vip-radio-component-item-label', optionClassName ) }
|
|
125
|
-
htmlFor={ id }
|
|
126
|
-
sx={ labelStyle }
|
|
127
|
-
{ ...labelProps }
|
|
128
|
-
>
|
|
129
|
-
{ label }
|
|
130
|
-
</Label>
|
|
131
|
-
) : (
|
|
132
|
-
<CustomLabel { ...labelProps }>{ label }</CustomLabel>
|
|
133
|
-
) }
|
|
134
|
-
</div>
|
|
135
|
-
)
|
|
136
|
-
);
|
|
160
|
+
const renderedOptions = options.map( option => (
|
|
161
|
+
<RadioOption
|
|
162
|
+
key={ option?.id }
|
|
163
|
+
name={ name }
|
|
164
|
+
option={ option }
|
|
165
|
+
onChangeHandler={ onChangeHandler }
|
|
166
|
+
checked={ `${ defaultValue }` === `${ option?.value }` }
|
|
167
|
+
/>
|
|
168
|
+
) );
|
|
137
169
|
|
|
138
170
|
return (
|
|
139
171
|
<div
|
|
140
|
-
className={ classNames(
|
|
141
|
-
'vip-radio-component',
|
|
142
|
-
`vip-radio-component-${ name }`,
|
|
143
|
-
className
|
|
144
|
-
) }
|
|
172
|
+
className={ classNames( prefix, `${ prefix }${ name }`, className ) }
|
|
145
173
|
ref={ forwardRef }
|
|
146
174
|
{ ...props }
|
|
147
175
|
>
|