Skip to content

🎥 Make videos programmatically with React

zod union editor

Jonny Burger2a69676e423d
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodOrNullEditor.tsx b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodOrNullishEditor.tsx
similarity index 88%
rename from packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodOrNullEditor.tsx
rename to packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodOrNullishEditor.tsx
index 1423f05603..1835463bba 100644
--- a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodOrNullEditor.tsx
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodOrNullishEditor.tsx
@@ -26,7 +26,7 @@ const checkBoxWrapper: React.CSSProperties = {
marginTop: '5px',
};
-export const ZodOrNullEditor: React.FC<{
+export const ZodOrNullishEditor: React.FC<{
showSaveButton: boolean;
jsonPath: JSONPath;
compact: boolean;
@@ -36,6 +36,7 @@ export const ZodOrNullEditor: React.FC<{
setValue: React.Dispatch<React.SetStateAction<unknown>>;
onSave: (updater: (oldNum: unknown) => unknown) => void;
onRemove: null | (() => void);
+ nullishValue: null | undefined;
}> = ({
jsonPath,
compact,
@@ -46,8 +47,9 @@ export const ZodOrNullEditor: React.FC<{
value,
showSaveButton,
onRemove,
+ nullishValue,
}) => {
- const isChecked = value === null;
+ const isChecked = value === nullishValue;
const onValueChange = useCallback(
(newValue: unknown) => {
@@ -60,10 +62,10 @@ export const ZodOrNullEditor: React.FC<{
useCallback(
(e) => {
console.log({schema, newVal: createZodValues(schema)});
- const val = e.target.checked ? null : createZodValues(schema);
+ const val = e.target.checked ? nullishValue : createZodValues(schema);
onValueChange(val);
},
- [onValueChange, schema]
+ [nullishValue, onValueChange, schema]
);
const reset = useCallback(() => {
@@ -108,7 +110,7 @@ export const ZodOrNullEditor: React.FC<{
disabled={false}
/>
<Spacing x={1} />
- <div style={labelStyle}>null</div>
+ <div style={labelStyle}>{String(nullishValue)}</div>
</div>
</>
);
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodUnionEditor.tsx b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodUnionEditor.tsx
index ca4aeb10bf..3844276ae4 100644
--- a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodUnionEditor.tsx
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodUnionEditor.tsx
@@ -1,7 +1,38 @@
import {z} from 'remotion';
import type {JSONPath} from './zod-types';
import {ZonNonEditableValue} from './ZodNonEditableValue';
-import {ZodOrNullEditor} from './ZodOrNullEditor';
+import {ZodOrNullishEditor} from './ZodOrNullishEditor';
+
+const findNull = (
+ value: readonly [z.ZodTypeAny, z.ZodTypeAny, ...z.ZodTypeAny[]]
+) => {
+ const nullIndex = value.findIndex(
+ (v) =>
+ v._def.typeName === z.ZodFirstPartyTypeKind.ZodNull ||
+ v._def.typeName === z.ZodFirstPartyTypeKind.ZodUndefined
+ );
+ if (nullIndex === -1) {
+ return null;
+ }
+
+ const nullishValue =
+ value[nullIndex]._def.typeName === z.ZodFirstPartyTypeKind.ZodNull
+ ? null
+ : undefined;
+
+ const otherSchema = value[nullIndex === 0 ? 1 : 0];
+
+ const otherSchemaIsAlsoNullish =
+ otherSchema._def.typeName === z.ZodFirstPartyTypeKind.ZodNull ||
+ otherSchema._def.typeName === z.ZodFirstPartyTypeKind.ZodUndefined;
+
+ return {
+ nullIndex,
+ nullishValue,
+ otherSchema,
+ otherSchemaIsAlsoNullish,
+ };
+};
export const ZodUnionEditor: React.FC<{
showSaveButton: boolean;
@@ -26,44 +57,66 @@ export const ZodUnionEditor: React.FC<{
}) => {
const {options} = schema._def as z.ZodUnionDef;
- if (options[0]._def.typeName === z.ZodFirstPartyTypeKind.ZodNull) {
+ if (options.length > 2) {
return (
- <ZodOrNullEditor
+ <ZonNonEditableValue
+ jsonPath={jsonPath}
+ label={'Union with more than 2 options not editable'}
compact={compact}
- defaultValue={defaultValue}
+ showSaveButton={showSaveButton}
+ />
+ );
+ }
+
+ if (options.length < 2) {
+ return (
+ <ZonNonEditableValue
jsonPath={jsonPath}
- onRemove={onRemove}
- onSave={onSave}
- schema={options[1]}
- setValue={setValue}
+ label={'Union with less than 2 options not editable'}
+ compact={compact}
showSaveButton={showSaveButton}
- value={value}
/>
);
}
- if (options[1]._def.typeName === z.ZodFirstPartyTypeKind.ZodNull) {
+ const nullResult = findNull(options);
+
+ if (!nullResult) {
return (
- <ZodOrNullEditor
+ <ZonNonEditableValue
+ jsonPath={jsonPath}
+ label={'Union only editable with 1 value being null'}
compact={compact}
- defaultValue={defaultValue}
+ showSaveButton={showSaveButton}
+ />
+ );
+ }
+
+ const {otherSchema, nullishValue, otherSchemaIsAlsoNullish} = nullResult;
+
+ if (otherSchemaIsAlsoNullish) {
+ return (
+ <ZonNonEditableValue
jsonPath={jsonPath}
- onRemove={onRemove}
- onSave={onSave}
- schema={options[0]}
- setValue={setValue}
+ label={'Not editable - both union values are nullish'}
+ compact={compact}
showSaveButton={showSaveButton}
- value={value}
/>
);
}
return (
- <ZonNonEditableValue
- jsonPath={jsonPath}
- label={'Union only supported with null'}
+ <ZodOrNullishEditor
compact={compact}
+ defaultValue={defaultValue}
+ jsonPath={jsonPath}
+ onRemove={onRemove}
+ onSave={onSave}
+ schema={otherSchema}
+ setValue={setValue}
showSaveButton={showSaveButton}
+ value={value}
+ nullishValue={nullishValue}
/>
);
};
diff --git a/packages/example/src/Video.tsx b/packages/example/src/Video.tsx
index 5755931726..04b4feb1c6 100644
--- a/packages/example/src/Video.tsx
+++ b/packages/example/src/Video.tsx
@@ -710,13 +710,14 @@ export const Index: React.FC = () => {
date: z.date(),
values: z.enum(['a', 'b', 'c']),
supersuperlongvalueabcdefghji: z.string(),
+ incompatible: z.null().or(z.undefined()),
})}
defaultProps={{
vehicle: 'car' as const,
other: 'hi' as const,
abc: {
union: null,
- def: {unionArray: ['huthere' as const], pef: 'hu' as const},
+ def: {unionArray: [null], pef: 'hu' as const},
jkl: 'sting' as const,
xyz: 'hi' as const,
},
@@ -734,6 +735,7 @@ export const Index: React.FC = () => {
date: new Date('1999-02-12T20:20:00.000Z'),
values: 'a' as const,
supersuperlongvalueabcdefghji: 'hi' as const,
+ incompatible: null,
}}
durationInFrames={150}
/>