refactor(module): the type of events is rolled back to string[]

This commit is contained in:
xzdry 2023-01-12 10:26:37 +08:00
parent 766b9bf0a4
commit 43703bb64e
3 changed files with 12 additions and 25 deletions

View File

@ -15,9 +15,7 @@ export type Module = {
type ModuleSpec = {
properties: JSONSchema7;
events: {
name: string;
}[];
events: string[];
stateMap: Record<string, string>;
};
@ -45,12 +43,6 @@ export function createModule(options: CreateModuleOptions): RuntimeModule {
exampleProperties: options.metadata.exampleProperties || {},
},
spec: {
// `json-schema-editor` has a readonly root object by default,
// it provides two schema formats,array({type:'array'}) and object({type:'object'}).
// In sunmao, we only use the object schema, so we need to specify a default value here
// and silently fail when root selects array.
// This is a bit obscure, so should remove the array type of root from the json-schema-editor later
// TODO remove the array type of root from the json-schema-editor
properties: { type: 'object' },
events: [],
stateMap: {},

View File

@ -22,9 +22,7 @@ export type ModuleMetaDataFormData = {
version: string;
stateMap: Record<string, string>;
properties: JSONSchema7;
events: {
name: string;
}[];
events: string[];
exampleProperties: JSONSchema7Object;
};
@ -34,12 +32,11 @@ type ModuleMetaDataFormProps = {
onSubmit?: (value: ModuleMetaDataFormData) => void;
};
const genEventsName = (events: { name: string }[]) => {
const genEventsName = (events: string[]) => {
let count = events.length;
let name = `event${count}`;
const eventNames = events.map(({ name }) => name);
while (eventNames.includes(name)) {
while (events.includes(name)) {
name = `event${++count}`;
}
@ -48,7 +45,7 @@ const genEventsName = (events: { name: string }[]) => {
const EventInput: React.FC<{
name: string;
events: { name: string }[];
events: string[];
index: number;
onChange: (value: string) => void;
}> = ({ name: defaultName, onChange, events, index }) => {
@ -69,7 +66,7 @@ const EventInput: React.FC<{
onBlur={() => {
const newEvents = [...events];
newEvents.splice(index, 1);
if (newEvents.find(e => e.name === name)) {
if (newEvents.find(eventName => eventName === name)) {
setIsRepeated(true);
return;
}
@ -173,20 +170,20 @@ export const ModuleMetaDataForm: React.FC<ModuleMetaDataFormProps> = observer(
</FormControl>
<FormControl>
<FormLabel>Events</FormLabel>
{formik.values.events.map((event, i) => {
{formik.values.events.map((eventName, i) => {
return (
<HStack m="10px 0 10px 0" alignItems="normal" key={event.name}>
<HStack m="10px 0 10px 0" alignItems="normal" key={eventName}>
<EventInput
events={formik.values.events}
index={i}
onChange={newName => {
const newEvents = produce(formik.values.events, draft => {
draft[i].name = newName;
draft[i] = newName;
});
formik.setFieldValue('events', newEvents);
formik.submitForm();
}}
name={event.name}
name={eventName}
/>
<IconButton
aria-label="remove row"
@ -207,9 +204,7 @@ export const ModuleMetaDataForm: React.FC<ModuleMetaDataFormProps> = observer(
<Button
onClick={() => {
const newEvents = produce(formik.values.events, draft => {
draft.push({
name: genEventsName(draft),
});
draft.push(genEventsName(draft));
});
formik.setFieldValue('events', newEvents);
formik.submitForm();

View File

@ -123,7 +123,7 @@ export class AppStorage {
stateMap: Record<string, string>;
properties: JSONSchema7;
exampleProperties: JSONSchema7Object;
events: { name: string }[];
events: string[];
}
) {
const i = this.modules.findIndex(