Skip to content

🎥 Make videos programmatically with React

clickable SchemaLabel

Patric Salvisberg471ff89d91e7
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ClickableSchemaLabel.tsx b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ClickableSchemaLabel.tsx
new file mode 100644
index 0000000000..502da360a7
--- /dev/null
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ClickableSchemaLabel.tsx
@@ -0,0 +1,72 @@
+import React, {useMemo} from 'react';
+import {FAIL_COLOR, LIGHT_TEXT} from '../../../helpers/colors';
+import {Flex} from '../../layout';
+import {InlineRemoveButton} from '../InlineRemoveButton';
+import {getSchemaLabel} from './get-schema-label';
+import {SchemaResetButton} from './SchemaResetButton';
+import {SchemaSaveButton} from './SchemaSaveButton';
+import type {JSONPath} from './zod-types';
+
+const compactStyles: React.CSSProperties = {
+ fontSize: 15,
+ color: LIGHT_TEXT,
+ fontFamily: 'sans-serif',
+ display: 'flex',
+ flexDirection: 'row',
+ alignItems: 'center',
+ flex: 1,
+};
+
+export const ClickableSchemaLabel: React.FC<{
+ jsonPath: JSONPath;
+ isDefaultValue: boolean;
+ onReset: () => void;
+ onSave: () => void;
+ onRemove: null | (() => void);
+ showSaveButton: boolean;
+ saving: boolean;
+ valid: boolean;
+ saveDisabledByParent: boolean;
+ suffix: string | null;
+ handleClick: () => void;
+}> = ({
+ jsonPath,
+ isDefaultValue,
+ onReset,
+ onSave,
+ showSaveButton,
+ onRemove,
+ saving,
+ valid,
+ saveDisabledByParent,
+ suffix,
+ handleClick,
+}) => {
+ const disableSave = saving || !valid || saveDisabledByParent;
+ const labelStyle: React.CSSProperties = useMemo(() => {
+ return {
+ cursor: 'pointer',
+ fontFamily: 'monospace',
+ fontSize: 14,
+ color: valid ? LIGHT_TEXT : FAIL_COLOR,
+ lineHeight: '24px',
+ };
+ }, [valid]);
+
+ return (
+ <div style={compactStyles}>
+ <button type="button" onClick={handleClick} style={{border: 'none'}}>
+ <span style={labelStyle}>
+ {getSchemaLabel(jsonPath)} {suffix ? suffix : null}
+ </span>
+ </button>
+
+ <Flex />
+ {isDefaultValue ? null : <SchemaResetButton onClick={onReset} />}
+ {isDefaultValue ? null : showSaveButton ? (
+ <SchemaSaveButton onClick={onSave} disabled={disableSave} />
+ ) : null}
+ {onRemove ? <InlineRemoveButton onClick={onRemove} /> : null}
+ </div>
+ );
+};
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodArrayEditor.tsx b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodArrayEditor.tsx
index feda337a68..6b3e7ceb4f 100644
--- a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodArrayEditor.tsx
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodArrayEditor.tsx
@@ -1,4 +1,4 @@
-import React, {useCallback, useMemo} from 'react';
+import React, {useCallback, useMemo, useState} from 'react';
import type {z} from 'zod';
import {Plus} from '../../../icons/plus';
import {
@@ -8,11 +8,11 @@ import {
import type {RenderInlineAction} from '../../InlineAction';
import {InlineAction} from '../../InlineAction';
import {fieldsetLabel} from '../layout';
+import {ClickableSchemaLabel} from './ClickableSchemaLabel';
import {createZodValues} from './create-zod-values';
import {deepEqual} from './deep-equal';
import {Fieldset} from './Fieldset';
import {useLocalState} from './local-state';
-import {SchemaLabel} from './SchemaLabel';
import {SchemaSeparationLine} from './SchemaSeparationLine';
import {SchemaVerticalGuide} from './SchemaVerticalGuide';
import type {JSONPath} from './zod-types';
@@ -52,8 +52,13 @@ export const ZodArrayEditor: React.FC<{
defaultValue,
});
+ const [expanded, setExpanded] = useState(true);
+
const def = schema._def as z.ZodArrayDef;
+ const suffix = useMemo(() => {
+ return expanded ? ' [' : ' [...] ';
+ }, [expanded]);
const z = useZodIfPossible();
if (!z) {
throw new Error('expected zod');
@@ -86,47 +91,58 @@ export const ZodArrayEditor: React.FC<{
return (
<Fieldset shouldPad={mayPad} success={localValue.zodValidation.success}>
- <SchemaLabel
- onReset={reset}
- isDefaultValue={isDefaultValue}
- jsonPath={jsonPath}
- onRemove={onRemove}
- suffix={' ['}
- onSave={() => {
- onSave(() => localValue.value, false, false);
+ <div
+ style={{
+ display: 'flex',
+ flexDirection: 'row',
}}
- saveDisabledByParent={saveDisabledByParent}
- saving={saving}
- showSaveButton={showSaveButton}
- valid={localValue.zodValidation.success}
- />
- <RevisionContextProvider>
- <SchemaVerticalGuide isRoot={false}>
- {localValue.value.map((child, i) => {
- return (
- // eslint-disable-next-line react/no-array-index-key
- <React.Fragment key={`${i}${localValue.keyStabilityRevision}`}>
- <ZodArrayItemEditor
- onChange={onChange}
- value={child}
- def={def}
- index={i}
- jsonPath={jsonPath}
- defaultValue={defaultValue[i] ?? child}
- onSave={onSave}
- showSaveButton={showSaveButton}
- saving={saving}
- saveDisabledByParent={saveDisabledByParent}
- mayPad={mayPad}
- />
- {i === localValue.value.length - 1 ? null : (
- <SchemaSeparationLine />
- )}
- </React.Fragment>
- );
- })}
- </SchemaVerticalGuide>
- </RevisionContextProvider>
+ >
+ <ClickableSchemaLabel
+ onReset={reset}
+ isDefaultValue={isDefaultValue}
+ jsonPath={jsonPath}
+ onRemove={onRemove}
+ suffix={suffix}
+ onSave={() => {
+ onSave(() => localValue.value, false, false);
+ }}
+ saveDisabledByParent={saveDisabledByParent}
+ saving={saving}
+ showSaveButton={showSaveButton}
+ valid={localValue.zodValidation.success}
+ handleClick={() => setExpanded(!expanded)}
+ />
+ </div>
+
+ {expanded ? (
+ <RevisionContextProvider>
+ <SchemaVerticalGuide isRoot={false}>
+ {localValue.value.map((child, i) => {
+ return (
+ // eslint-disable-next-line react/no-array-index-key
+ <React.Fragment key={`${i}${localValue.keyStabilityRevision}`}>
+ <ZodArrayItemEditor
+ onChange={onChange}
+ value={child}
+ def={def}
+ index={i}
+ jsonPath={jsonPath}
+ defaultValue={defaultValue[i] ?? child}
+ onSave={onSave}
+ showSaveButton={showSaveButton}
+ saving={saving}
+ saveDisabledByParent={saveDisabledByParent}
+ mayPad={mayPad}
+ />
+ {i === localValue.value.length - 1 ? null : (
+ <SchemaSeparationLine />
+ )}
+ </React.Fragment>
+ );
+ })}
+ </SchemaVerticalGuide>
+ </RevisionContextProvider>
+ ) : null}
<div
style={{
@@ -135,8 +151,12 @@ export const ZodArrayEditor: React.FC<{
display: 'flex',
}}
>
- {']'}
- <InlineAction onClick={onAdd} renderAction={renderAddButton} />
+ {expanded ? (
+ <>
+ {']'}
+ <InlineAction onClick={onAdd} renderAction={renderAddButton} />
+ </>
+ ) : null}
</div>
<ZodFieldValidation path={jsonPath} localValue={localValue} />
</Fieldset>