@sanity/form-builder
Version:
Sanity form builder
61 lines (57 loc) • 3.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = resolveArrayInput;
exports.hasOptionsList = hasOptionsList;
exports.isArrayOfPrimitives = isArrayOfPrimitives;
var is = _interopRequireWildcard(require("../../utils/is"));
var _OptionsArrayInput = _interopRequireDefault(require("../../inputs/arrays/OptionsArrayInput"));
var _PortableTextInput = require("../../inputs/PortableText/PortableTextInput");
var _TagsArrayInput = require("../../inputs/TagsArrayInput");
var _SanityArrayInput = require("../inputs/SanityArrayInput");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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; }
var PRIMITIVES = ['string', 'number', 'boolean'];
function isArrayOfPrimitives(type) {
return type.of.every(ofType => PRIMITIVES.includes(ofType.jsonType));
}
function isStringArray(type) {
return type.of.length === 1 && is.type('string', type.of[0]);
}
function isTagsArray(type) {
var _type$options;
return ((_type$options = type.options) === null || _type$options === void 0 ? void 0 : _type$options.layout) === 'tags';
}
function isPortableText(type) {
// TODO: better testing here, not only for type 'block' !
return type.of.some(memberType => is.type('block', memberType));
}
function hasOptionsList(type) {
var _type$options2;
return Boolean((_type$options2 = type.options) === null || _type$options2 === void 0 ? void 0 : _type$options2.list);
}
function resolveArrayInput(type) {
// Schema provides predefines list
if (hasOptionsList(type)) {
// @todo: fix typing
return _OptionsArrayInput.default;
}
if (isStringArray(type) && isTagsArray(type)) {
return _TagsArrayInput.TagsArrayInput;
}
// Special component for array of primitive values
if (isArrayOfPrimitives(type)) {
// @todo: fix typing
return _SanityArrayInput.SanityArrayOfPrimitivesInput;
}
// Use Portable Text editor if portable text.
if (isPortableText(type)) {
// @todo: fix typing
return _PortableTextInput.PortableTextInput;
}
// use default
// @todo: fix typing
return _SanityArrayInput.SanityArrayInput;
}