fix(traitForm): trait form will crash if the trait has a property that is not in its spec

This commit is contained in:
Bowen Tan 2022-07-25 17:12:43 +08:00
parent 0ea1a9edbd
commit 570380c5a0

View File

@ -3,7 +3,6 @@ import { ComponentSchema, TraitSchema } from '@sunmao-ui/core';
import { HStack, IconButton, VStack } from '@chakra-ui/react';
import { generateDefaultValueFromSpec } from '@sunmao-ui/shared';
import { CloseIcon } from '@chakra-ui/icons';
import { TSchema } from '@sinclair/typebox';
import { formWrapperCSS } from '../style';
import { EditorServices } from '../../../types';
import { SpecWidget } from '@sunmao-ui/editor-sdk';
@ -28,7 +27,10 @@ export const GeneralTraitForm: React.FC<Props> = props => {
);
const fields = Object.keys(properties || []).map((key: string) => {
const value = trait.properties[key];
const propertySpec = (tImpl.spec.properties as TSchema).properties?.[key];
const propertySpec = tImpl.spec.properties.properties?.[key];
if (!propertySpec) return undefined;
const onChange = (newValue: any) => {
const operation = genOperation(registry, 'modifyTraitProperty', {
componentId: component.id,
@ -41,15 +43,14 @@ export const GeneralTraitForm: React.FC<Props> = props => {
eventBus.send('operation', operation);
};
const specObj = propertySpec === true ? {} : propertySpec;
return (
<SpecWidget
key={key}
level={1}
path={[key]}
spec={{
...propertySpec,
title: propertySpec.title || key,
}}
spec={{ ...specObj, title: specObj.title || key }}
value={value}
services={services}
component={component}