Skip to content

Commit

Permalink
feat: Moved DropDown to its own package as example of registering inp…
Browse files Browse the repository at this point in the history
…ut controls outside core
  • Loading branch information
pksorensen committed Mar 5, 2024
1 parent 9f1452f commit 0f35c8e
Show file tree
Hide file tree
Showing 30 changed files with 553 additions and 85 deletions.
60 changes: 53 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@
"link": "concurrently \"npm:link-*\"",
"link-core": "npm --workspace packages/core link",
"link-designer": "npm --workspace packages/designer link",
"link-inputs": "npm run link --workspace packages/inputs",

"publish": "npm-run-all --continue-on-error publish-*",
"publish-core": "npm --workspace packages/core publish --access public",
"publish-designer": "npm --workspace packages/designer publish --access public",

"release": "npm-run-all --continue-on-error release-*",
"release-core": "npm run release --workspace packages/core",
"release-designer": "npm run release --workspace packages/designer",

"eavfw-link": "npm link @eavfw/apps @eavfw/next @eavfw/expressions @eavfw/manifest @eavfw/hooks @eavfw/forms @eavfw/utils @eavfw/designer @eavfw/designer-core @eavfw/designer-nodes",

"playground": "npm --workspace packages/playground run dev"
},
"config": {
Expand All @@ -31,7 +36,8 @@
"private": true,
"workspaces": {
"packages": [
"packages/*"
"packages/*",
"packages/inputs/*"
]
},
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/components/question/input-types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// export * from './LastNameInput';
// export * from './PhoneInput';
// export * from './RoleInput';
export * from './dropdown/DropDownInput';
//export * from './dropdown/DropDownInput';
export * from './text/TextInput';
export * from './multiline/MultilineInput';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

"use client";
import { useEffect, useState } from "react";
import { useQuickForm } from "../../../state";

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { useHandleEnterKeypress} from "./useHandleEnterKeypress";
export { useHandleScroll } from "./useHandleScroll";
export { useDelayedClickListener } from "./useDelayedClickListenerCallback";
export { useKeyPressHandler } from "./useKeyPressHandler";
3 changes: 2 additions & 1 deletion packages/core/src/hooks/useKeyPressHandler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useEffect } from "react";
"use client"
import { useEffect } from "react";


export function useKeyPressHandler(keys: string[], onclick: (e: KeyboardEvent, key: string) => void) {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./components";
export * from "./state";
export * from "./services";
export * from "./hooks";
export type { QuickFormDefinition, InputProps } from "./model";
15 changes: 4 additions & 11 deletions packages/core/src/model/InputType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export type InputProps<TProps = InputPropertiesTypes> = {
questionModel: QuestionModel<TProps>;
}

const Dropdown = "dropdown";
//const Dropdown = "dropdown";
const Email = "email";
const Multilinetext = "multilinetext";
const Radio = "radio";
Expand All @@ -14,7 +14,7 @@ const Buttons = "buttons";

export interface InputTypeMap {
[Buttons]: ButtonsProperties;
[Dropdown]: DropDownProperties;

[Email]: EmailProperties;
[Multilinetext]: MultilineProperties;
[Radio]: RadioProperties;
Expand All @@ -24,7 +24,7 @@ export interface InputTypeMap {

export type InputPropertiesTypes =
ButtonsProperties |
DropDownProperties |

EmailProperties |
MultilineProperties |
RadioProperties |
Expand All @@ -39,14 +39,7 @@ export type ButtonsProperties = {
}
}

export type DropDownProperties = {
inputType: typeof Dropdown;
maxItems?: number;
minItems?: number;
options: {
[key: string]: string;
}
}


export type EmailProperties = {
inputType: typeof Email;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/model/QuestionModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { InputPropertiesTypes, InputTypeMap } from "./InputType";

export type QuestionModel<TProps = InputPropertiesTypes> = {
logicalName: string;
inputType: keyof InputTypeMap | "text";
inputType: keyof InputTypeMap | "text" | string;
text: string;
placeholder: string;
paragraph: string;
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/model/json-definitions/JsonDataModels.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ButtonsProperties, DropDownProperties, EmailProperties, MultilineProperties, RadioProperties, SliderProperties, TextProperties } from "../InputType";
import { ButtonsProperties, EmailProperties, MultilineProperties, RadioProperties, SliderProperties, TextProperties } from "../InputType";

type QuickFormQuestionDefinition = {

Expand Down Expand Up @@ -67,7 +67,7 @@ type QuickFormQuestionDefinition = {
export type QuestionJsonModel =
QuickFormQuestionDefinition |
QuickFormQuestionDefinition & ButtonsProperties |
QuickFormQuestionDefinition & DropDownProperties |
// QuickFormQuestionDefinition & DropDownProperties |
QuickFormQuestionDefinition & EmailProperties |
QuickFormQuestionDefinition & MultilineProperties |
QuickFormQuestionDefinition & RadioProperties |
Expand Down
40 changes: 31 additions & 9 deletions packages/core/src/services/defaults/DefaultInputTypeResolver.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,48 @@
import { FC } from "react";
import { DropDownProperties, RadioProperties, SliderProperties, ButtonsProperties, InputPropertiesTypes, InputProps } from "../../model";
import { RadioProperties, SliderProperties, ButtonsProperties, InputPropertiesTypes, InputProps } from "../../model";
import { QuestionJsonModel } from "../../model/json-definitions/JsonDataModels";
import { registerQuickFormService } from "../QuickFormServices";
//import { TextInput, MultilineInput, DropDownInput } from "../../components/question/input-types/index";

function getDefaultValue(schema: JSONSchema7Definition) {
if (typeof schema === "boolean")
return undefined;
if (typeof schema === "string")
return undefined;
if (typeof schema === "object")
return schema.default;
}
function parseInputProperties(questionJsonModel: QuestionJsonModel): InputPropertiesTypes {
if (!questionJsonModel.inputType)
return {};

const comp = resolveInputComponent(questionJsonModel.inputType)?.quickform?.schema?.properties;
if (comp) {
return Object.fromEntries(Object.entries(comp)
.filter(([k, _]) => !(k === "text" || k === "paragraph" || k === "placeholder"))
.map(([k, schema]) => [k, questionJsonModel[k as keyof (QuestionJsonModel)] ?? getDefaultValue(schema)])) as InputPropertiesTypes;
}

let inputProperties: InputPropertiesTypes;


switch (questionJsonModel.inputType) {

case "buttons":
inputProperties = {
inputType: questionJsonModel.inputType,
options: (questionJsonModel as (QuestionJsonModel & ButtonsProperties)).options
};
break;

case "dropdown":
inputProperties = {
inputType: questionJsonModel.inputType,
options: (questionJsonModel as (QuestionJsonModel & DropDownProperties)).options,
minItems: (questionJsonModel as (QuestionJsonModel & DropDownProperties)).minItems ?? 1,
maxItems: (questionJsonModel as (QuestionJsonModel & DropDownProperties)).maxItems ?? 1,
};
break;
//case "dropdown":
// inputProperties = {
// inputType: questionJsonModel.inputType,
// options: (questionJsonModel as (QuestionJsonModel & DropDownProperties)).options,
// minItems: (questionJsonModel as (QuestionJsonModel & DropDownProperties)).minItems ?? 1,
// maxItems: (questionJsonModel as (QuestionJsonModel & DropDownProperties)).maxItems ?? 1,
// };
// break;

case "radio":
inputProperties = {
Expand All @@ -42,6 +62,8 @@ function parseInputProperties(questionJsonModel: QuestionJsonModel): InputProper
break;

default:


inputProperties = {}
;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/services/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./defaults";
export * from "./defaults";
export * from "./QuickFormServices";
9 changes: 8 additions & 1 deletion packages/designer/src/Components/Drawers/NavDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { QuestionJsonModel } from "@eavfw/quickform-core/src/model/json-definiti
import { CaretUpFilled, CaretDownFilled } from "@fluentui/react-icons"
const useNavDrawerStyles = makeStyles({
actions: {
backgroundColor: tokens.colorBrandBackground,
backgroundColor: tokens.colorNeutralBackground1Hover,
position: "absolute",
right:0
}
Expand Down Expand Up @@ -144,6 +144,9 @@ export const NavDrawer = ({
?? old.questions[Object.keys(old.questions)[index - 1]];
oq.order = index;
const q = old.questions[key]; q.order = index - 1;

old.questions = Object.fromEntries(Object.entries(old.questions).map(([k, q], i) => [k, q, q.order ?? i] as [string, QuestionJsonModel, number]).sort(([k, a, i], [k1, b, j]) => i - j))

return { ...old };
})
}}
Expand All @@ -158,6 +161,10 @@ export const NavDrawer = ({
?? old.questions[Object.keys(old.questions)[index + 1]];
oq.order = index;
const q = old.questions[key]; q.order = index + 1;

old.questions = Object.fromEntries(Object.entries(old.questions).map(([k, q], i) => [k, q, q.order ?? i] as [string, QuestionJsonModel, number]).sort(([k, a, i], [k1, b, j]) => i - j))


return { ...old };
})
}}
Expand Down
Loading

0 comments on commit 0f35c8e

Please sign in to comment.