Skip to content

🎥 Make videos programmatically with React

implement nullable and optional schema

Jonny Burger76341ab06e79
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodNullableEditor.tsx b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodNullableEditor.tsx
new file mode 100644
index 0000000000..09fa796802
--- /dev/null
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodNullableEditor.tsx
@@ -0,0 +1,42 @@
+import type {z} from 'remotion';
+import type {JSONPath} from './zod-types';
+import {ZodOrNullishEditor} from './ZodOrNullishEditor';
+
+export const ZodNullableEditor: React.FC<{
+ showSaveButton: boolean;
+ jsonPath: JSONPath;
+ compact: boolean;
+ value: unknown;
+ defaultValue: unknown;
+ schema: z.ZodTypeAny;
+ setValue: React.Dispatch<React.SetStateAction<unknown>>;
+ onSave: (updater: (oldNum: unknown) => unknown) => void;
+ onRemove: null | (() => void);
+}> = ({
+ jsonPath,
+ compact,
+ schema,
+ setValue,
+ onSave,
+ defaultValue,
+ value,
+ showSaveButton,
+ onRemove,
+}) => {
+ const {innerType} = schema._def as z.ZodOptionalDef;
+
+ return (
+ <ZodOrNullishEditor
+ compact={compact}
+ defaultValue={defaultValue}
+ jsonPath={jsonPath}
+ onRemove={onRemove}
+ onSave={onSave}
+ schema={innerType}
+ setValue={setValue}
+ showSaveButton={showSaveButton}
+ value={value}
+ nullishValue={null}
+ />
+ );
+};
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodOptionalEditor.tsx b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodOptionalEditor.tsx
new file mode 100644
index 0000000000..a998443f7b
--- /dev/null
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodOptionalEditor.tsx
@@ -0,0 +1,42 @@
+import type {z} from 'remotion';
+import type {JSONPath} from './zod-types';
+import {ZodOrNullishEditor} from './ZodOrNullishEditor';
+
+export const ZodOptionalEditor: React.FC<{
+ showSaveButton: boolean;
+ jsonPath: JSONPath;
+ compact: boolean;
+ value: unknown;
+ defaultValue: unknown;
+ schema: z.ZodTypeAny;
+ setValue: React.Dispatch<React.SetStateAction<unknown>>;
+ onSave: (updater: (oldNum: unknown) => unknown) => void;
+ onRemove: null | (() => void);
+}> = ({
+ jsonPath,
+ compact,
+ schema,
+ setValue,
+ onSave,
+ defaultValue,
+ value,
+ showSaveButton,
+ onRemove,
+}) => {
+ const {innerType} = schema._def as z.ZodOptionalDef;
+
+ return (
+ <ZodOrNullishEditor
+ compact={compact}
+ defaultValue={defaultValue}
+ jsonPath={jsonPath}
+ onRemove={onRemove}
+ onSave={onSave}
+ schema={innerType}
+ setValue={setValue}
+ showSaveButton={showSaveButton}
+ value={value}
+ nullishValue={undefined}
+ />
+ );
+};
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodOrNullishEditor.tsx b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodOrNullishEditor.tsx
index 1835463bba..abc73544b0 100644
--- a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodOrNullishEditor.tsx
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodOrNullishEditor.tsx
@@ -61,7 +61,6 @@ export const ZodOrNullishEditor: React.FC<{
const onCheckBoxChange: React.ChangeEventHandler<HTMLInputElement> =
useCallback(
(e) => {
- console.log({schema, newVal: createZodValues(schema)});
const val = e.target.checked ? nullishValue : createZodValues(schema);
onValueChange(val);
},
@@ -78,7 +77,7 @@ export const ZodOrNullishEditor: React.FC<{
return (
<>
- {value === null ? (
+ {value === nullishValue ? (
<SchemaLabel
isDefaultValue={value === defaultValue}
jsonPath={jsonPath}
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodSwitch.tsx b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodSwitch.tsx
index c21bc94cc1..7bb3169cef 100644
--- a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodSwitch.tsx
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodSwitch.tsx
@@ -8,8 +8,10 @@ import {ZodDateEditor} from './ZodDateEditor';
import {ZodEffectEditor} from './ZodEffectEditor';
import {ZodEnumEditor} from './ZodEnumEditor';
import {ZonNonEditableValue} from './ZodNonEditableValue';
+import {ZodNullableEditor} from './ZodNullableEditor';
import {ZodNumberEditor} from './ZodNumberEditor';
import {ZodObjectEditor} from './ZodObjectEditor';
+import {ZodOptionalEditor} from './ZodOptionalEditor';
import {ZodStringEditor} from './ZodStringEditor';
import {ZodUnionEditor} from './ZodUnionEditor';
@@ -260,6 +262,38 @@ export const ZodSwitch: React.FC<{
);
}
+ if (typeName === z.ZodFirstPartyTypeKind.ZodOptional) {
+ return (
+ <ZodOptionalEditor
+ compact={compact}
+ jsonPath={jsonPath}
+ showSaveButton={showSaveButton}
+ defaultValue={defaultValue}
+ value={value}
+ setValue={setValue}
+ onSave={onSave}
+ onRemove={onRemove}
+ schema={schema}
+ />
+ );
+ }
+
+ if (typeName === z.ZodFirstPartyTypeKind.ZodNullable) {
+ return (
+ <ZodNullableEditor
+ compact={compact}
+ jsonPath={jsonPath}
+ showSaveButton={showSaveButton}
+ defaultValue={defaultValue}
+ value={value}
+ setValue={setValue}
+ onSave={onSave}
+ onRemove={onRemove}
+ schema={schema}
+ />
+ );
+ }
+
return (
<ZonNonEditableValue
compact={compact}
diff --git a/packages/example/src/SchemaTest/index.tsx b/packages/example/src/SchemaTest/index.tsx
index 22e4b994b8..4ef9d4ba56 100644
--- a/packages/example/src/SchemaTest/index.tsx
+++ b/packages/example/src/SchemaTest/index.tsx
@@ -2,7 +2,7 @@ import React from 'react';
import {AbsoluteFill, Sequence, z} from 'remotion';
export const schemaTestSchema = z.object({
- title: z.string(),
+ title: z.string().nullable(),
delay: z.number().min(0).max(1000).step(0.1),
});
diff --git a/packages/example/src/Video.tsx b/packages/example/src/Video.tsx
index 3ae9049597..df800209ff 100644
--- a/packages/example/src/Video.tsx
+++ b/packages/example/src/Video.tsx
@@ -714,6 +714,8 @@ export const Index: React.FC = () => {
supersuperlongvalueabcdefghji: z.string(),
incompatible: z.null().or(z.undefined()),
color: zColor(),
+ nullable: z.nullable(z.string()),
+ optional: z.string().optional(),
longEnum: z.enum([
'a',
'b',
@@ -769,6 +771,8 @@ export const Index: React.FC = () => {
incompatible: null,
longEnum: 'k' as const,
color: '#eb3a60' as const,
+ nullable: null,
+ optional: undefined,
}}
durationInFrames={150}
/>