@sanity/form-builder
Version:
Sanity form builder
77 lines (76 loc) • 4.55 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isThenable = isThenable;
exports.isTrueIsh = isTrueIsh;
exports.mappedChildren = mappedChildren;
exports.omitDeprecatedRole = omitDeprecatedRole;
exports.useCheckCondition = useCheckCondition;
var _react = _interopRequireWildcard(require("react"));
var _excluded = ["role"];
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); }
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; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function isThenable(value) {
return typeof (value === null || value === void 0 ? void 0 : value.then) === 'function';
}
var warningMap = new Map();
function isTrueIsh(checkProperty, checkPropertyName, _ref) {
var document = _ref.document,
parent = _ref.parent,
value = _ref.value,
currentUser = _ref.currentUser;
var result = false;
if (typeof checkProperty === 'boolean' || !checkProperty) {
return checkProperty;
}
try {
result = checkProperty({
document,
parent,
value,
currentUser
});
} catch (err) {
console.error("An error occurred while running the callback from `".concat(checkPropertyName, "`: ").concat(err.message));
return false;
}
if (isThenable(result) && !warningMap.has(checkPropertyName)) {
warningMap.set(checkPropertyName, true);
console.warn("The `".concat(checkPropertyName, "` option is either a promise or a promise returning function. Async callbacks for `").concat(checkPropertyName, "` option is not currently supported."));
return false;
}
if (typeof result === 'undefined' && !warningMap.has(checkPropertyName)) {
warningMap.set(checkPropertyName, true);
console.warn("The `".concat(checkPropertyName, "` option is either a promise or a promise returning function. Async callbacks for `").concat(checkPropertyName, "` option is not currently supported."));
}
return result;
}
function omitDeprecatedRole(user) {
if (user !== null && user !== void 0 && user.role) {
var role = user.role,
rest = _objectWithoutProperties(user, _excluded);
return rest;
}
return user;
}
function useCheckCondition(checkProperty, checkPropertyName, context) {
return (0, _react.useMemo)(() => {
return isTrueIsh(checkProperty, checkPropertyName, context);
}, [checkProperty, checkPropertyName, context]);
}
function mappedChildren(_ref2) {
var children = _ref2.children,
childProps = _ref2.childProps;
if (!Array.isArray(children)) {
children = [children];
}
return _react.default.Children.map(children, function (child) {
if ( /*#__PURE__*/_react.default.isValidElement(child)) {
return /*#__PURE__*/_react.default.cloneElement(child, childProps);
}
return child;
});
}