vanjs-jsf 0.0.10 → 0.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,7 +3,8 @@ import { VanJSComponent } from "./VanJSComponent";
3
3
  export interface Option {
4
4
  label: string;
5
5
  value: string;
6
- description: string;
6
+ description?: string;
7
+ img?: string;
7
8
  }
8
9
  export type MultiType = string | number | boolean;
9
10
  export declare class VanJsfField extends VanJSComponent {
@@ -1,12 +1,13 @@
1
1
  import van from "vanjs-core";
2
2
  import { VanJSComponent } from "./VanJSComponent";
3
3
  import pikaday from "pikaday";
4
- const { div, p, input, label, textarea, legend, link, fieldset, span } = van.tags;
4
+ const { div, p, input, label, textarea, legend, link, fieldset, span, select, option } = van.tags;
5
5
  var FieldType;
6
6
  (function (FieldType) {
7
7
  FieldType["text"] = "text";
8
8
  FieldType["number"] = "number";
9
9
  FieldType["textarea"] = "textarea";
10
+ FieldType["select"] = "select";
10
11
  FieldType["radio"] = "radio";
11
12
  FieldType["date"] = "date";
12
13
  FieldType["fieldset"] = "fieldset";
@@ -92,6 +93,16 @@ export class VanJsfField extends VanJSComponent {
92
93
  oninput: (e) => this.handleChange(this, e.target.value),
93
94
  }));
94
95
  break;
96
+ //TODO: Add select component
97
+ case FieldType.select:
98
+ el = div(props, label({ for: this.name, style: "margin-right: 5px;", class: this.titleClass ? this.titleClass : '' }, this.label), this.description &&
99
+ div({ id: `${this.name}-description`, class: this.descriptionClass ? this.descriptionClass : '' }, this.description), select({
100
+ id: this.name,
101
+ name: this.name,
102
+ class: this.class ? this.class : null,
103
+ oninput: (e) => this.handleChange(this, e.target.value),
104
+ }, this.options?.map((opt) => option({ class: this.class ? this.class : null, value: opt.value }, opt.label, opt.description))));
105
+ break;
95
106
  case FieldType.date:
96
107
  const calendarInput = input({
97
108
  id: this.name,
@@ -5,16 +5,18 @@ const { form } = van.tags;
5
5
  class VanJsfForm {
6
6
  schema;
7
7
  config;
8
+ isValid;
8
9
  headlessForm;
9
10
  formFields;
10
11
  formValues;
11
- constructor(jsonSchema, config) {
12
+ constructor(jsonSchema, config, isValid) {
12
13
  // Bind methods to instance. Needed to pass functions as props to child components
13
14
  //this.handleSubmit = this.handleSubmit.bind(this);
14
15
  this.handleFieldChange = this.handleFieldChange.bind(this);
15
16
  // Receive parameters
16
17
  this.schema = jsonSchema;
17
18
  this.config = config;
19
+ this.isValid = isValid || undefined;
18
20
  // Working with parameters
19
21
  this.headlessForm = createHeadlessForm(jsonSchema, config);
20
22
  // Read documentation about `getFieldsAndValuedFromJsf` method below
@@ -64,6 +66,15 @@ class VanJsfForm {
64
66
  f.isVisible = f.field.isVisible;
65
67
  f.error = formErrors?.[f.name] ?? "";
66
68
  });
69
+ if (this.isValid) {
70
+ if (formErrors) {
71
+ if (this.isValid)
72
+ this.isValid.val = false;
73
+ }
74
+ else {
75
+ this.isValid.val = true;
76
+ }
77
+ }
67
78
  }
68
79
  processFields(fields, initialValues, formValues, parentPath = "") {
69
80
  return fields.map((field) => {
@@ -89,6 +100,7 @@ export function jsform(attributes, ...children) {
89
100
  throw new Error("JSON Schema is required");
90
101
  }
91
102
  let config = attributes.config;
103
+ let isValid = attributes.isValid;
92
104
  if (!config) {
93
105
  config = { initialValues: {}, formValues: {} };
94
106
  }
@@ -98,7 +110,7 @@ export function jsform(attributes, ...children) {
98
110
  else if (!config.formValues) {
99
111
  config.formValues = {};
100
112
  }
101
- const vanJsfForm = new VanJsfForm(attributes.schema, config);
113
+ const vanJsfForm = new VanJsfForm(attributes.schema, config, isValid);
102
114
  console.log(vanJsfForm);
103
115
  const fields = vanJsfForm.formFields.map((field) => field.render());
104
116
  const childrenWithFields = [...fields, ...children]; // Concatenate fields with other children
@@ -108,8 +120,11 @@ export function jsform(attributes, ...children) {
108
120
  config.formValues = vanJsfForm.formValues;
109
121
  originalOnSubmit && originalOnSubmit(e);
110
122
  };
123
+ const originalOnChange = attributes.onchange;
111
124
  const handleChange = (e) => {
125
+ e.preventDefault();
112
126
  config.formValues = vanJsfForm.formValues;
127
+ originalOnChange && originalOnChange(vanJsfForm, e);
113
128
  };
114
129
  attributes.onsubmit = handleSubmit;
115
130
  attributes.onchange = handleChange;