@automattic/vip-design-system 0.27.1 → 0.27.2
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 +76 -52
- package/package.json +1 -1
- package/src/system/Form/Radio.js +70 -45
|
@@ -21,13 +21,22 @@ var _ScreenReaderText = require("../ScreenReaderText/ScreenReaderText");
|
|
|
21
21
|
|
|
22
22
|
var _jsxRuntime = require("theme-ui/jsx-runtime");
|
|
23
23
|
|
|
24
|
-
var _excluded = ["
|
|
25
|
-
_excluded2 = ["id", "
|
|
24
|
+
var _excluded = ["children"],
|
|
25
|
+
_excluded2 = ["id", "value", "className", "label", "labelProps"],
|
|
26
|
+
_excluded3 = ["disabled", "defaultValue", "onChange", "name", "options", "className"];
|
|
26
27
|
|
|
27
28
|
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
29
|
|
|
29
30
|
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
31
|
|
|
32
|
+
var prefix = 'vip-radio-component-';
|
|
33
|
+
var itemStyle = {
|
|
34
|
+
display: 'flex',
|
|
35
|
+
alignItems: 'center',
|
|
36
|
+
minHeight: function minHeight(theme) {
|
|
37
|
+
return theme.space[4] - theme.space[2] + "px";
|
|
38
|
+
}
|
|
39
|
+
};
|
|
31
40
|
var inputStyle = (0, _extends2["default"])({}, _ScreenReaderText.screenReaderTextClass, {
|
|
32
41
|
width: '16px',
|
|
33
42
|
height: '16px',
|
|
@@ -88,12 +97,13 @@ var labelStyle = {
|
|
|
88
97
|
};
|
|
89
98
|
|
|
90
99
|
var CustomLabel = function CustomLabel(_ref) {
|
|
91
|
-
var children = _ref.children
|
|
100
|
+
var children = _ref.children,
|
|
101
|
+
restLabel = (0, _objectWithoutPropertiesLoose2["default"])(_ref, _excluded);
|
|
92
102
|
return (0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
|
|
93
103
|
children: /*#__PURE__*/_react["default"].cloneElement(_react["default"].Children.only(children), (0, _extends2["default"])({}, children.props, {
|
|
94
104
|
sx: (0, _extends2["default"])({}, labelStyle, children.props.sx),
|
|
95
|
-
className: children.props.className + "
|
|
96
|
-
}))
|
|
105
|
+
className: children.props.className + " " + prefix + "item-label"
|
|
106
|
+
}, restLabel))
|
|
97
107
|
});
|
|
98
108
|
};
|
|
99
109
|
|
|
@@ -101,61 +111,75 @@ CustomLabel.propTypes = {
|
|
|
101
111
|
children: _propTypes["default"].any
|
|
102
112
|
};
|
|
103
113
|
|
|
104
|
-
var
|
|
105
|
-
var
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
_ref2$
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
+
var RadioOption = function RadioOption(_ref2) {
|
|
115
|
+
var _ref2$option = _ref2.option,
|
|
116
|
+
id = _ref2$option.id,
|
|
117
|
+
value = _ref2$option.value,
|
|
118
|
+
className = _ref2$option.className,
|
|
119
|
+
label = _ref2$option.label,
|
|
120
|
+
_ref2$option$labelPro = _ref2$option.labelProps,
|
|
121
|
+
labelProps = _ref2$option$labelPro === void 0 ? {} : _ref2$option$labelPro,
|
|
122
|
+
restOption = (0, _objectWithoutPropertiesLoose2["default"])(_ref2$option, _excluded2),
|
|
123
|
+
name = _ref2.name,
|
|
124
|
+
onChangeHandler = _ref2.onChangeHandler,
|
|
125
|
+
checked = _ref2.checked;
|
|
126
|
+
return (0, _jsxRuntime.jsxs)("div", {
|
|
127
|
+
sx: itemStyle,
|
|
128
|
+
className: (0, _classnames["default"])(prefix + "item", prefix + "item-" + id, checked ? prefix + "item-checked" : '', className),
|
|
129
|
+
children: [(0, _jsxRuntime.jsx)("input", (0, _extends2["default"])({
|
|
130
|
+
type: "radio",
|
|
131
|
+
id: id,
|
|
132
|
+
name: name,
|
|
133
|
+
value: "" + value,
|
|
134
|
+
sx: inputStyle,
|
|
135
|
+
onChange: onChangeHandler,
|
|
136
|
+
className: prefix + "item-input",
|
|
137
|
+
checked: checked
|
|
138
|
+
}, restOption)), typeof label === 'string' ? (0, _jsxRuntime.jsx)(_Label.Label, (0, _extends2["default"])({
|
|
139
|
+
className: prefix + "item-label",
|
|
140
|
+
htmlFor: id,
|
|
141
|
+
sx: labelStyle
|
|
142
|
+
}, labelProps, {
|
|
143
|
+
children: label
|
|
144
|
+
})) : (0, _jsxRuntime.jsx)(CustomLabel, (0, _extends2["default"])({}, labelProps, {
|
|
145
|
+
children: label
|
|
146
|
+
}))]
|
|
147
|
+
});
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
RadioOption.propTypes = {
|
|
151
|
+
option: _propTypes["default"].object,
|
|
152
|
+
name: _propTypes["default"].string,
|
|
153
|
+
onChangeHandler: _propTypes["default"].func,
|
|
154
|
+
checked: _propTypes["default"].bool
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
var Radio = /*#__PURE__*/_react["default"].forwardRef(function (_ref3, forwardRef) {
|
|
158
|
+
var disabled = _ref3.disabled,
|
|
159
|
+
defaultValue = _ref3.defaultValue,
|
|
160
|
+
onChange = _ref3.onChange,
|
|
161
|
+
_ref3$name = _ref3.name,
|
|
162
|
+
name = _ref3$name === void 0 ? '' : _ref3$name,
|
|
163
|
+
_ref3$options = _ref3.options,
|
|
164
|
+
options = _ref3$options === void 0 ? [] : _ref3$options,
|
|
165
|
+
className = _ref3.className,
|
|
166
|
+
props = (0, _objectWithoutPropertiesLoose2["default"])(_ref3, _excluded3);
|
|
114
167
|
var onChangeHandler = (0, _react.useCallback)(function (e) {
|
|
115
168
|
var optionTriggered = options.find(function (option) {
|
|
116
169
|
return "" + option.value === "" + e.target.value;
|
|
117
170
|
});
|
|
118
171
|
onChange(e, optionTriggered);
|
|
119
172
|
}, []);
|
|
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);
|
|
173
|
+
var renderedOptions = options.map(function (option) {
|
|
174
|
+
return (0, _jsxRuntime.jsx)(RadioOption, {
|
|
175
|
+
name: name,
|
|
176
|
+
option: option,
|
|
177
|
+
onChangeHandler: onChangeHandler,
|
|
178
|
+
checked: "" + defaultValue === "" + (option == null ? void 0 : option.value)
|
|
179
|
+
}, option == null ? void 0 : option.id);
|
|
156
180
|
});
|
|
157
181
|
return (0, _jsxRuntime.jsx)("div", (0, _extends2["default"])({
|
|
158
|
-
className: (0, _classnames["default"])(
|
|
182
|
+
className: (0, _classnames["default"])(prefix, "" + prefix + name, className),
|
|
159
183
|
ref: forwardRef
|
|
160
184
|
}, props, {
|
|
161
185
|
children: renderedOptions
|
package/package.json
CHANGED
package/src/system/Form/Radio.js
CHANGED
|
@@ -10,6 +10,14 @@ import classNames from 'classnames';
|
|
|
10
10
|
import { Label } from './Label';
|
|
11
11
|
import { screenReaderTextClass } from '../ScreenReaderText/ScreenReaderText';
|
|
12
12
|
|
|
13
|
+
const prefix = 'vip-radio-component-';
|
|
14
|
+
|
|
15
|
+
const itemStyle = {
|
|
16
|
+
display: 'flex',
|
|
17
|
+
alignItems: 'center',
|
|
18
|
+
minHeight: theme => `${ theme.space[ 4 ] - theme.space[ 2 ] }px`,
|
|
19
|
+
};
|
|
20
|
+
|
|
13
21
|
const inputStyle = {
|
|
14
22
|
...screenReaderTextClass,
|
|
15
23
|
width: '16px',
|
|
@@ -68,13 +76,14 @@ const labelStyle = {
|
|
|
68
76
|
},
|
|
69
77
|
};
|
|
70
78
|
|
|
71
|
-
const CustomLabel = ( { children } ) => {
|
|
79
|
+
const CustomLabel = ( { children, ...restLabel } ) => {
|
|
72
80
|
return (
|
|
73
81
|
<>
|
|
74
82
|
{ React.cloneElement( React.Children.only( children ), {
|
|
75
83
|
...children.props,
|
|
76
84
|
sx: { ...labelStyle, ...children.props.sx },
|
|
77
|
-
className: `${ children.props.className }
|
|
85
|
+
className: `${ children.props.className } ${ prefix }item-label`,
|
|
86
|
+
...restLabel,
|
|
78
87
|
} ) }
|
|
79
88
|
</>
|
|
80
89
|
);
|
|
@@ -84,6 +93,55 @@ CustomLabel.propTypes = {
|
|
|
84
93
|
children: PropTypes.any,
|
|
85
94
|
};
|
|
86
95
|
|
|
96
|
+
const RadioOption = ( {
|
|
97
|
+
option: { id, value, className, label, labelProps = {}, ...restOption },
|
|
98
|
+
name,
|
|
99
|
+
onChangeHandler,
|
|
100
|
+
checked,
|
|
101
|
+
} ) => (
|
|
102
|
+
<div
|
|
103
|
+
sx={ itemStyle }
|
|
104
|
+
className={ classNames(
|
|
105
|
+
`${ prefix }item`,
|
|
106
|
+
`${ prefix }item-${ id }`,
|
|
107
|
+
checked ? `${ prefix }item-checked` : '',
|
|
108
|
+
className
|
|
109
|
+
) }
|
|
110
|
+
>
|
|
111
|
+
<input
|
|
112
|
+
type="radio"
|
|
113
|
+
id={ id }
|
|
114
|
+
name={ name }
|
|
115
|
+
value={ `${ value }` }
|
|
116
|
+
sx={ inputStyle }
|
|
117
|
+
onChange={ onChangeHandler }
|
|
118
|
+
className={ `${ prefix }item-input` }
|
|
119
|
+
checked={ checked }
|
|
120
|
+
{ ...restOption }
|
|
121
|
+
/>
|
|
122
|
+
|
|
123
|
+
{ typeof label === 'string' ? (
|
|
124
|
+
<Label
|
|
125
|
+
className={ `${ prefix }item-label` }
|
|
126
|
+
htmlFor={ id }
|
|
127
|
+
sx={ labelStyle }
|
|
128
|
+
{ ...labelProps }
|
|
129
|
+
>
|
|
130
|
+
{ label }
|
|
131
|
+
</Label>
|
|
132
|
+
) : (
|
|
133
|
+
<CustomLabel { ...labelProps }>{ label }</CustomLabel>
|
|
134
|
+
) }
|
|
135
|
+
</div>
|
|
136
|
+
);
|
|
137
|
+
|
|
138
|
+
RadioOption.propTypes = {
|
|
139
|
+
option: PropTypes.object,
|
|
140
|
+
name: PropTypes.string,
|
|
141
|
+
onChangeHandler: PropTypes.func,
|
|
142
|
+
checked: PropTypes.bool,
|
|
143
|
+
};
|
|
144
|
+
|
|
87
145
|
const Radio = React.forwardRef(
|
|
88
146
|
(
|
|
89
147
|
{ disabled, defaultValue, onChange, name = '', options = [], className, ...props },
|
|
@@ -96,52 +154,19 @@ const Radio = React.forwardRef(
|
|
|
96
154
|
onChange( e, optionTriggered );
|
|
97
155
|
}, [] );
|
|
98
156
|
|
|
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
|
-
);
|
|
157
|
+
const renderedOptions = options.map( option => (
|
|
158
|
+
<RadioOption
|
|
159
|
+
key={ option?.id }
|
|
160
|
+
name={ name }
|
|
161
|
+
option={ option }
|
|
162
|
+
onChangeHandler={ onChangeHandler }
|
|
163
|
+
checked={ `${ defaultValue }` === `${ option?.value }` }
|
|
164
|
+
/>
|
|
165
|
+
) );
|
|
137
166
|
|
|
138
167
|
return (
|
|
139
168
|
<div
|
|
140
|
-
className={ classNames(
|
|
141
|
-
'vip-radio-component',
|
|
142
|
-
`vip-radio-component-${ name }`,
|
|
143
|
-
className
|
|
144
|
-
) }
|
|
169
|
+
className={ classNames( prefix, `${ prefix }${ name }`, className ) }
|
|
145
170
|
ref={ forwardRef }
|
|
146
171
|
{ ...props }
|
|
147
172
|
>
|