diff --git a/packages/core/src/model/QuickFormModel.ts b/packages/core/src/model/QuickFormModel.ts index 04a1dd1..ab16bdb 100644 --- a/packages/core/src/model/QuickFormModel.ts +++ b/packages/core/src/model/QuickFormModel.ts @@ -1,6 +1,13 @@ import { EndingModel, IntroModel, SlideModel, SubmitModel } from "./index"; export type QuickFormModel = { + validation?: { + messages?: { + "NOT_ALL_QUESTIONS_ANSWERED"?: string, + "SOME_QUESTIONS_HAS_EMPTY_ANSWER"?: string, + "SOME_QUESTIONS_HAVE_FAILED_VALIDATION"?: string; + } + }, intro?: IntroModel; slides: SlideModel[]; submit: SubmitModel; diff --git a/packages/core/src/model/json-definitions/QuickFormDefinition.ts b/packages/core/src/model/json-definitions/QuickFormDefinition.ts index 188f950..946e461 100644 --- a/packages/core/src/model/json-definitions/QuickFormDefinition.ts +++ b/packages/core/src/model/json-definitions/QuickFormDefinition.ts @@ -5,6 +5,13 @@ import { QuickFormSubmitDefinition } from "./QuickFormSubmitDefinition"; import { LayoutDefinition } from "./Layout"; export type QuickFormDefinition = { + validation?: { + messages?: { + "NOT_ALL_QUESTIONS_ANSWERED"?: string, + "SOME_QUESTIONS_HAS_EMPTY_ANSWER"?: string, + "SOME_QUESTIONS_HAVE_FAILED_VALIDATION"?: string; + } + }, intro?: IntroModel; questions: QuickFormQuestionsDefinition, submit: QuickFormSubmitDefinition; diff --git a/packages/core/src/services/defaults/DefaultModelTransformer.ts b/packages/core/src/services/defaults/DefaultModelTransformer.ts index 259a851..086d9cc 100644 --- a/packages/core/src/services/defaults/DefaultModelTransformer.ts +++ b/packages/core/src/services/defaults/DefaultModelTransformer.ts @@ -218,6 +218,8 @@ const transformJSONInput: QuickFormModelTransformer = (definition, payload): Qui } return { + + validation: definition.validation, intro: definition.intro, ending: definition.ending, submit: handleSubmit(definition.submit, payload?.submitFields), diff --git a/packages/core/src/state/action-handlers/NavigationActionHandler.ts b/packages/core/src/state/action-handlers/NavigationActionHandler.ts index 0006f2f..eee4d1e 100644 --- a/packages/core/src/state/action-handlers/NavigationActionHandler.ts +++ b/packages/core/src/state/action-handlers/NavigationActionHandler.ts @@ -53,7 +53,7 @@ export class NavigationActionHandler { } // Check all visible questions for answered and validity - const validationResult = this.validateQuestions(visibleQuestions); + const validationResult = this.validateQuestions(visibleQuestions, state); console.log("reducer validationResult", validationResult) if (validationResult.isValid) { return this.computeProgress(NavigationActionHandler.handleSlideChange({ ...state, errorMsg: "" }, 'next')); @@ -62,13 +62,13 @@ export class NavigationActionHandler { } }; - static validateQuestions = (questions: QuestionModel[]) => { + static validateQuestions = (questions: QuestionModel[], state: QuickformState) => { const logger = resolveQuickFormService("logger"); if (questions.some((q: { answered: boolean; }) => q.answered === false)) { - return { isValid: false, errorMsg: "Not all questions have been answered." }; + return { isValid: false, errorMsg: state.data.validation?.messages?.NOT_ALL_QUESTIONS_ANSWERED ?? "Not all questions have been answered." }; } if (questions.some((q: { output: any; }) => q.output === '' || typeof q.output === "undefined")) { - return { isValid: false, errorMsg: "Some questions are missing outputs." }; + return { isValid: false, errorMsg: state.data.validation?.messages?.SOME_QUESTIONS_HAS_EMPTY_ANSWER ?? "Some questions are missing outputs." }; } if (questions.some(q => !q.validationResult || (!q.validationResult?.isValidating && q.validationResult?.isValid === false))) { //TODO, if its validating, should that block us from moving to next? @@ -76,7 +76,7 @@ export class NavigationActionHandler { //I dont think its critical that it allow to move on. Right now this function is only called when validating if it can go to next slide //it has nothing to do with actually validation of the form. logger.log("Some questions have failed validation. {questions}", [questions, JSON.stringify(questions)]); - return { isValid: false, errorMsg: "Some questions have failed validation." }; + return { isValid: false, errorMsg: state.data.validation?.messages?.SOME_QUESTIONS_HAVE_FAILED_VALIDATION ?? "Some questions have failed validation." }; } return { isValid: true, errorMsg: "" }; diff --git a/packages/designer/src/Components/Views/QuickFormSettingsView.tsx b/packages/designer/src/Components/Views/QuickFormSettingsView.tsx index 7d707dd..b4c8cbd 100644 --- a/packages/designer/src/Components/Views/QuickFormSettingsView.tsx +++ b/packages/designer/src/Components/Views/QuickFormSettingsView.tsx @@ -50,18 +50,30 @@ const quickformSettingsSchema = { } } } -} as { label: string, uiSchema: any, schema: JSONSchema7 }; +} as { label: string, uiSchema: any, schema: JSONSchema7 & { properties: { tokens: JSONSchema7 } } }; -export function registerToken(key: keyof typeof defaultQuickFormTokens, title: string, description: string, widget: string) { +export function registerToken(key: keyof TokenRegistry, title: string, description: string, widget: string, defaultValue?: any, defaultTokenKey?: keyof TokenRegistry) { const tokensSchema = quickformSettingsSchema.schema.properties?.tokens! as JSONSchema7; const uiSchema = quickformSettingsSchema.uiSchema.tokens; if (tokensSchema && tokensSchema.properties) { - tokensSchema.properties[key] = { + let defaultTokenValue = defaultTokenKey && defaultTokenKey in defaultQuickFormTokens ? + defaultQuickFormTokens[defaultTokenKey as keyof typeof defaultQuickFormTokens] ?? defaultValue + : defaultValue + + tokensSchema.properties[key as string] = { title, description, type: "string", - default: defaultQuickFormTokens[key] + + default: + key in defaultQuickFormTokens ? + defaultQuickFormTokens[key as keyof typeof defaultQuickFormTokens] ?? defaultTokenValue + : defaultTokenValue + } + if (defaultTokenKey) { + //@ts-ignore + tokensSchema.properties[key]["x-default-token"] = defaultTokenKey; } } if (widget) { @@ -106,15 +118,30 @@ export const QuickFormSettingsView = () => { const styles = useViewStyles(); const quickformSettingsStyles = useQuickformSettingsStyles(); const PreviewComponent = Controls["QuickFormSettingsViewPreviewComponent"]; - console.log("quickformpayloadV1", JSON.stringify(quickformpayload.layout)); - console.log("ENUM", Object.values(IconEnum)); + + const formData = { + ...quickformpayload.layout, + tokens: { + ...(Object.fromEntries(Object.entries(quickformSettingsSchema.schema.properties.tokens.properties ?? {}) + .filter(([key, value]) => typeof value === "object" && "x-default-token" in value) //@ts-ignore + .map(([key, value]) => [key, quickformpayload.layout?.tokens[value["x-default-token"]]]) + )), + ...quickformpayload.layout?.tokens ?? {} + } + + + + } + + console.log("Settings Data", [quickformSettingsSchema.schema.properties.tokens.properties, JSON.stringify(formData, null, 4)]); + return (
{ console.log("change", [a, b]); dispatch(old => {