🎥 Make videos programmatically with React
union
Jonny Burgerd5a5ff4c1c86
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodOrNullEditor.tsx b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodOrNullEditor.tsxnew file mode 100644index 0000000000..1423f05603--- /dev/null+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodOrNullEditor.tsx@@ -0,0 +1,115 @@+import {useCallback} from 'react';+import type {z} from 'remotion';+import {LIGHT_TEXT} from '../../../helpers/colors';+import {Checkbox} from '../../Checkbox';+import {Spacing} from '../../layout';+import {createZodValues} from './create-zod-values';+import {SchemaLabel} from './SchemaLabel';+import type {JSONPath} from './zod-types';+import {ZodSwitch} from './ZodSwitch';++const fullWidth: React.CSSProperties = {+ width: '100%',+};++const labelStyle: React.CSSProperties = {+ fontFamily: 'sans-serif',+ fontSize: 14,+ color: LIGHT_TEXT,+};++const checkBoxWrapper: React.CSSProperties = {+ margin: '2px',+ display: 'flex',+ flexDirection: 'row',+ alignItems: 'center',+ marginTop: '5px',+};++export const ZodOrNullEditor: 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 isChecked = value === null;++ const onValueChange = useCallback(+ (newValue: unknown) => {+ setValue(newValue);+ },+ [setValue]+ );++ const onCheckBoxChange: React.ChangeEventHandler<HTMLInputElement> =+ useCallback(+ (e) => {+ console.log({schema, newVal: createZodValues(schema)});+ const val = e.target.checked ? null : createZodValues(schema);+ onValueChange(val);+ },+ [onValueChange, schema]+ );++ const reset = useCallback(() => {+ onValueChange(defaultValue);+ }, [defaultValue, onValueChange]);++ const save = useCallback(() => {+ onSave(() => value);+ }, [onSave, value]);++ return (+ <>+ {value === null ? (+ <SchemaLabel+ isDefaultValue={value === defaultValue}+ jsonPath={jsonPath}+ onReset={reset}+ onSave={save}+ showSaveButton={showSaveButton}+ compact={compact}+ onRemove={onRemove}+ />+ ) : (+ <div style={fullWidth}>+ <ZodSwitch+ value={value}+ setValue={onValueChange}+ jsonPath={jsonPath}+ schema={schema}+ compact={compact}+ defaultValue={defaultValue}+ onSave={onSave}+ showSaveButton={showSaveButton}+ onRemove={onRemove}+ />+ </div>+ )}+ <div style={checkBoxWrapper}>+ <Checkbox+ checked={isChecked}+ onChange={onCheckBoxChange}+ disabled={false}+ />+ <Spacing x={1} />+ <div style={labelStyle}>null</div>+ </div>+ </>+ );+};diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodUnionEditor.tsx b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodUnionEditor.tsxindex 97f02f1d46..dacd72a957 100644--- a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodUnionEditor.tsx+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodUnionEditor.tsx@@ -1,30 +1,8 @@-import {useCallback, useEffect, useState} from 'react';import {z} from 'remotion';-import {LIGHT_TEXT} from '../../../helpers/colors';-import {Checkbox} from '../../Checkbox';-import {Spacing} from '../../layout';-import {SchemaLabel} from './SchemaLabel';import type {JSONPath} from './zod-types';import {ZonNonEditableValue} from './ZodNonEditableValue';-import {ZodSwitch} from './ZodSwitch';+import {ZodOrNullEditor} from './ZodOrNullEditor';-const fullWidth: React.CSSProperties = {- width: '100%',-};--const labelStyle: React.CSSProperties = {- fontFamily: 'sans-serif',- fontSize: 14,- color: LIGHT_TEXT,-};--const checkBoxWrapper: React.CSSProperties = {- margin: '2px',- display: 'flex',- flexDirection: 'row',- alignItems: 'center',- marginTop: '5px',-};export const ZodUnionEditor: React.FC<{showSaveButton: boolean;jsonPath: JSONPath;@@ -46,92 +24,38 @@ export const ZodUnionEditor: React.FC<{showSaveButton,onRemove,}) => {- const {options} = schema._def;-- const [isChecked, setIsChecked] = useState<boolean>(false);- const onValueChange = useCallback(- (newValue: unknown) => {- setValue(newValue);- },- [setValue]- );- useEffect(() => {- if (value === null) {- setIsChecked(true);- }- }, [value]);+ console.log({schema, value});+ const {options} = schema._def as z.ZodUnionDef;- const onCheckBoxChange: React.ChangeEventHandler<HTMLInputElement> =- useCallback(- (e) => {- setIsChecked(!isChecked);- // TODO: Don't assume string- const val = e.target.checked ? null : '';- onValueChange(val);- },- [isChecked, onValueChange]+ if (options[0]._def.typeName === z.ZodFirstPartyTypeKind.ZodNull) {+ return (+ <ZodOrNullEditor+ compact={compact}+ defaultValue={defaultValue}+ jsonPath={jsonPath}+ onRemove={onRemove}+ onSave={onSave}+ schema={options[1]}+ setValue={setValue}+ showSaveButton={showSaveButton}+ value={value}+ />);+ }- const onChange = useCallback(- (newValue: unknown) => {- setValue(newValue);- },- [setValue]- );-- const reset = useCallback(() => {- onValueChange(defaultValue);- }, [defaultValue, onValueChange]);-- const save = useCallback(() => {- onSave(() => value);- }, [onSave, value]);-- if (- options[0]._def.typeName === z.ZodFirstPartyTypeKind.ZodNull ||- options[1]._def.typeName === z.ZodFirstPartyTypeKind.ZodNull- ) {+ if (options[1]._def.typeName === z.ZodFirstPartyTypeKind.ZodNull) {return (- <>- {value === null ? (- <SchemaLabel- isDefaultValue={value === defaultValue}- jsonPath={jsonPath}- onReset={reset}- onSave={save}- showSaveButton={showSaveButton}- compact={compact}- onRemove={onRemove}- />- ) : (- <div style={fullWidth}>- <ZodSwitch- value={value}- setValue={onChange}- jsonPath={jsonPath}- schema={- options[0]._def.typeName === z.ZodFirstPartyTypeKind.ZodNull- ? options[1]- : options[0]- }- compact={compact}- defaultValue={defaultValue}- onSave={onSave}- showSaveButton={showSaveButton}- onRemove={onRemove}- />- </div>- )}- <div style={checkBoxWrapper}>- <Checkbox- checked={isChecked}- onChange={onCheckBoxChange}- disabled={false}- />- <Spacing x={1} />- <div style={labelStyle}>null</div>- </div>- </>+ <ZodOrNullEditor+ compact={compact}+ defaultValue={defaultValue}+ jsonPath={jsonPath}+ onRemove={onRemove}+ onSave={onSave}+ schema={options[0]}+ setValue={setValue}+ showSaveButton={showSaveButton}+ value={value}+ />);}diff --git a/packages/example/src/Video.tsx b/packages/example/src/Video.tsxindex d978513f7a..5755931726 100644--- a/packages/example/src/Video.tsx+++ b/packages/example/src/Video.tsx@@ -680,7 +680,11 @@ export const Index: React.FC = () => {.refine((v) => ['car', 'bus', 'truck'].includes(v)),other: z.string(),abc: z.object({- union: z.null().or(z.string()),+ union: z.null().or(+ z.object({+ abc: z.string(),+ })+ ),jkl: z.string(),def: z.object({unionArray: z.array(z.null().or(z.string())),