Skip to content

🎥 Make videos programmatically with React

Insert array element at any position

Patric Salvisberg218a55c8c5b5
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodArrayItemEditor.tsx b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodArrayItemEditor.tsx
index 45eed48cde..e8105742a9 100644
--- a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodArrayItemEditor.tsx
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodArrayItemEditor.tsx
@@ -1,4 +1,11 @@
import {useCallback, useMemo} from 'react';
+import {Plus} from '../../../icons/plus';
+import {
+ useZodIfPossible,
+ useZodTypesIfPossible,
+} from '../../get-zod-if-possible';
+import {InlineAction, type RenderInlineAction} from '../../InlineAction';
+import {createZodValues} from './create-zod-values';
import type {JSONPath} from './zod-types';
import type {UpdaterFunction} from './ZodSwitch';
import {ZodSwitch} from './ZodSwitch';
@@ -29,6 +36,12 @@ export const ZodArrayItemEditor: React.FC<{
saveDisabledByParent,
mayPad,
}) => {
+ const z = useZodIfPossible();
+ if (!z) {
+ throw new Error('expected zod');
+ }
+
+ const zodTypes = useZodTypesIfPossible();
const onRemove = useCallback(() => {
onChange(
(oldV) => [...oldV.slice(0, index), ...oldV.slice(index + 1)],
@@ -37,6 +50,20 @@ export const ZodArrayItemEditor: React.FC<{
);
}, [index, onChange]);
+ const onAdd2 = useCallback(() => {
+ onChange(
+ (oldV) => {
+ return [
+ ...oldV.slice(0, index + 1),
+ createZodValues(def.type, z, zodTypes),
+ ...oldV.slice(index + 1),
+ ];
+ },
+ false,
+ true,
+ );
+ }, [def.type, index, onChange, z, zodTypes]);
+
const setValue = useCallback(
(val: ((newV: unknown) => unknown) | unknown) => {
onChange(
@@ -69,19 +96,28 @@ export const ZodArrayItemEditor: React.FC<{
[index, onSaveObject],
);
+ const renderAddButton: RenderInlineAction = useCallback((color) => {
+ return <Plus color={color} style={{height: 12}} />;
+ }, []);
+
return (
- <ZodSwitch
- jsonPath={newJsonPath}
- schema={def.type}
- value={value}
- setValue={setValue}
- defaultValue={defaultValue}
- onSave={onSave}
- showSaveButton={showSaveButton}
- onRemove={onRemove}
- saving={saving}
- saveDisabledByParent={saveDisabledByParent}
- mayPad={mayPad}
- />
+ <>
+ <ZodSwitch
+ jsonPath={newJsonPath}
+ schema={def.type}
+ value={value}
+ setValue={setValue}
+ defaultValue={defaultValue}
+ onSave={onSave}
+ showSaveButton={showSaveButton}
+ onRemove={onRemove}
+ saving={saving}
+ saveDisabledByParent={saveDisabledByParent}
+ mayPad={mayPad}
+ />
+ <div>
+ <InlineAction onClick={onAdd2} renderAction={renderAddButton} />
+ </div>
+ </>
);
};
diff --git a/packages/example/src/Root.tsx b/packages/example/src/Root.tsx
index 1eff4da0ba..81699e08e7 100644
--- a/packages/example/src/Root.tsx
+++ b/packages/example/src/Root.tsx
@@ -1110,7 +1110,7 @@ export const Index: React.FC = () => {
title: 'sdasdsd',
delay: 5.2,
color: '#df822a',
- list: ['Sample Item'],
+ list: ['second', 'lol', 'first', '', 'yo'],
description: 'Sample description \nOn multiple lines',
}}
/>