-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
165 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import {StringField, StringFieldType} from "../primitives/StringField"; | ||
import {JsonSchema} from "../../types"; | ||
import {produce} from "immer"; | ||
|
||
export type SelectFieldType = StringFieldType & { | ||
options: { | ||
enum: string; | ||
enumNames: string; | ||
}[] | ||
}; | ||
|
||
export class SelectField extends StringField { | ||
protected enum?: string[]; | ||
|
||
protected enumNames?: string[]; | ||
|
||
constructor(name: string) { | ||
super(name); | ||
} | ||
|
||
setEnum(_enum: string[]): this { | ||
this.enum = _enum; | ||
return this; | ||
} | ||
|
||
setEnumNames(enumNames: string[]): this { | ||
this.enumNames = enumNames; | ||
return this; | ||
} | ||
|
||
getBuilderSchema(): JsonSchema { | ||
const enumSchema: Record<string, JsonSchema> = { | ||
options: { | ||
type: "array", | ||
title: "Options", | ||
minItems: 1, | ||
description: "Here you can add options for the select field", | ||
items: { | ||
type: "object", | ||
properties: { | ||
enum: { | ||
type: "string", | ||
title: "Value of the option", | ||
description: "The value that is going to be in the form", | ||
}, | ||
enumNames: { | ||
type: "string", | ||
title: "Title of the option", | ||
description: "The title that is going to be shown to user", | ||
}, | ||
}, | ||
required: ["enum", "enumNames"], | ||
} | ||
} | ||
} | ||
|
||
return produce(super.getBuilderSchema(), (draft: JsonSchema) => { | ||
Object.keys(enumSchema).forEach(key => { | ||
if (draft.properties) draft.properties[key] = enumSchema[key]; | ||
}) | ||
}); | ||
} | ||
|
||
public getSchema(): JsonSchema { | ||
return { | ||
...super.getSchema(), | ||
enum: this.enum, | ||
enumNames: this.enumNames, | ||
} | ||
} | ||
|
||
public setSchema(schema: SelectFieldType): void { | ||
super.setSchema(schema); | ||
|
||
const options = { | ||
enum: schema.options?.map((option) => option.enum) || [], | ||
enumNames: schema.options?.map((option) => option.enumNames) || [], | ||
} | ||
|
||
if (options.enum?.length > 0) this.setEnum(options.enum) | ||
if (options.enumNames?.length > 0) this.setEnumNames(options.enumNames) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters