@remotion/studio
Version:
APIs for interacting with the Remotion Studio
47 lines (46 loc) • 2.58 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ZodOrNullishEditor = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const react_2 = require("react");
const colors_1 = require("../../../helpers/colors");
const Checkbox_1 = require("../../Checkbox");
const get_zod_if_possible_1 = require("../../get-zod-if-possible");
const layout_1 = require("../../layout");
const create_zod_values_1 = require("./create-zod-values");
const Fieldset_1 = require("./Fieldset");
const SchemaLabel_1 = require("./SchemaLabel");
const zod_schema_type_1 = require("./zod-schema-type");
const ZodSwitch_1 = require("./ZodSwitch");
const labelStyle = {
fontFamily: 'sans-serif',
fontSize: 14,
color: colors_1.LIGHT_TEXT,
};
const checkBoxWrapper = {
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
marginTop: '5px',
};
const ZodOrNullishEditor = ({ jsonPath, schema, setValue, value, onRemove, nullishValue, mayPad, innerSchema, }) => {
const z = (0, get_zod_if_possible_1.useZodIfPossible)();
if (!z) {
throw new Error('expected zod');
}
const zodTypes = (0, get_zod_if_possible_1.useZodTypesIfPossible)();
const isChecked = value === nullishValue;
const zodValidation = (0, react_1.useMemo)(() => (0, zod_schema_type_1.zodSafeParse)(schema, value), [schema, value]);
const onCheckBoxChange = (0, react_2.useCallback)((e) => {
const val = e.target.checked
? nullishValue
: (0, create_zod_values_1.createZodValues)(innerSchema, z, zodTypes);
setValue(() => val, { shouldSave: true });
}, [innerSchema, nullishValue, setValue, z, zodTypes]);
return (jsx_runtime_1.jsxs(Fieldset_1.Fieldset, { shouldPad: mayPad, children: [value === nullishValue ? (jsx_runtime_1.jsx(SchemaLabel_1.SchemaLabel, { handleClick: null, jsonPath: jsonPath, onRemove: onRemove, valid: zodValidation.success, suffix: null })) : (jsx_runtime_1.jsx(ZodSwitch_1.ZodSwitch, { value: value, setValue: setValue, jsonPath: jsonPath, schema: innerSchema, onRemove: onRemove, mayPad: false })), jsx_runtime_1.jsxs("div", { style: checkBoxWrapper, children: [
jsx_runtime_1.jsx(Checkbox_1.Checkbox, { checked: isChecked, onChange: onCheckBoxChange, disabled: false, name: jsonPath.join('.') }), jsx_runtime_1.jsx(layout_1.Spacing, { x: 1 }), jsx_runtime_1.jsx("div", { style: labelStyle, children: String(nullishValue) })
] })
] }));
};
exports.ZodOrNullishEditor = ZodOrNullishEditor;