@bytedance/mona-client-web
Version:
web for mona
99 lines • 4.4 kB
JavaScript
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
import React, { useCallback, useRef } from 'react';
import { useHandlers } from '../hooks';
import { useFormContext } from '../Form/hooks';
export var RadioGroupContext = React.createContext(null);
var RadioGroup = function (_a) {
var children = _a.children, _b = _a.name, name = _b === void 0 ? '' : _b, onChange = _a.onChange, restProps = __rest(_a, ["children", "name", "onChange"]);
var _c = useHandlers(restProps), handleClassName = _c.handleClassName, handlerProps = __rest(_c, ["handleClassName"]);
var valuesRef = useRef([]);
var indexRef = useRef(0);
var clearsRef = useRef([]);
var reset = useCallback(function () {
var newValues = valuesRef.current;
var cbs = clearsRef.current;
for (var i = 0; i < newValues.length; i++) {
newValues[i] = __assign(__assign({}, newValues[i]), { checked: false });
var cb = cbs[i];
if (typeof cb === 'function') {
cb();
}
}
}, [clearsRef, valuesRef]);
var getValues = function () {
return valuesRef.current.filter(function (v) { return v.checked; }).map(function (v) { return v.value; })[0] || '';
};
useFormContext(name, getValues, reset);
var context = {
initValue: function (value, checked, clear) {
var index = indexRef.current++;
var newValues = __spreadArray([], valuesRef.current, true);
// record order
newValues[index] = { value: value, checked: checked };
valuesRef.current = newValues;
clearsRef.current[index] = clear;
return index;
},
changeValue: function (index, value) {
var newValues = __spreadArray([], valuesRef.current, true);
newValues[index] = __assign(__assign({}, newValues[index]), { value: value });
valuesRef.current = newValues;
},
toggleChecked: function (index, checked, event) {
var newValues = __spreadArray([], valuesRef.current, true);
var cbs = clearsRef.current;
for (var i = 0; i < newValues.length; i++) {
if (index !== i) {
newValues[i] = __assign(__assign({}, newValues[i]), { checked: false });
var cb = cbs[i];
if (checked && typeof cb === 'function') {
cb();
}
}
else {
newValues[i] = __assign(__assign({}, newValues[i]), { checked: checked });
}
}
valuesRef.current = newValues;
if (typeof onChange === 'function') {
var value = newValues[index];
var e = __assign(__assign({}, event), { detail: { value: value.checked ? value.value : undefined } });
onChange(e);
}
}
};
return (React.createElement("div", __assign({ className: handleClassName() }, handlerProps),
React.createElement(RadioGroupContext.Provider, { value: context }, children)));
};
export default RadioGroup;
//# sourceMappingURL=index.js.map