UNPKG

@remotion/studio

Version:

APIs for interacting with the Remotion Studio

46 lines (45 loc) 2.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ZodUnionEditor = void 0; const jsx_runtime_1 = require("react/jsx-runtime"); const zod_schema_type_1 = require("./zod-schema-type"); const ZodNonEditableValue_1 = require("./ZodNonEditableValue"); const ZodOrNullishEditor_1 = require("./ZodOrNullishEditor"); const findNull = (value) => { const nullIndex = value.findIndex((v) => { const type = (0, zod_schema_type_1.getZodSchemaType)(v); return type === 'null' || type === 'undefined'; }); if (nullIndex === -1) { return null; } const nullishValue = (0, zod_schema_type_1.getZodSchemaType)(value[nullIndex]) === 'null' ? null : undefined; const otherSchema = value[nullIndex === 0 ? 1 : 0]; const otherType = (0, zod_schema_type_1.getZodSchemaType)(otherSchema); const otherSchemaIsAlsoNullish = otherType === 'null' || otherType === 'undefined'; return { nullIndex, nullishValue, otherSchema, otherSchemaIsAlsoNullish, }; }; const ZodUnionEditor = ({ jsonPath, schema, setValue, value, onRemove, mayPad }) => { const options = (0, zod_schema_type_1.getUnionOptions)(schema); if (options.length > 2) { return (jsx_runtime_1.jsx(ZodNonEditableValue_1.ZonNonEditableValue, { jsonPath: jsonPath, label: 'Union with more than 2 options not editable', mayPad: mayPad })); } if (options.length < 2) { return (jsx_runtime_1.jsx(ZodNonEditableValue_1.ZonNonEditableValue, { jsonPath: jsonPath, label: 'Union with less than 2 options not editable', mayPad: mayPad })); } const nullResult = findNull(options); if (!nullResult) { return (jsx_runtime_1.jsx(ZodNonEditableValue_1.ZonNonEditableValue, { jsonPath: jsonPath, label: 'Union only editable with 1 value being null', mayPad: mayPad })); } const { otherSchema, nullishValue, otherSchemaIsAlsoNullish } = nullResult; if (otherSchemaIsAlsoNullish) { return (jsx_runtime_1.jsx(ZodNonEditableValue_1.ZonNonEditableValue, { jsonPath: jsonPath, label: 'Not editable - both union values are nullish', mayPad: mayPad })); } return (jsx_runtime_1.jsx(ZodOrNullishEditor_1.ZodOrNullishEditor, { jsonPath: jsonPath, onRemove: onRemove, schema: schema, innerSchema: otherSchema, setValue: setValue, value: value, nullishValue: nullishValue, mayPad: mayPad })); }; exports.ZodUnionEditor = ZodUnionEditor;