@gisce/ooui 2.23.0-rc.1 → 2.23.0

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gisce/ooui",
3
- "version": "2.23.0-rc.1",
3
+ "version": "2.23.0",
4
4
  "engines": {
5
5
  "node": "20.5.0"
6
6
  },
package/src/Field.ts CHANGED
@@ -105,6 +105,14 @@ class Field extends Widget {
105
105
  this._sum = value;
106
106
  }
107
107
 
108
+ get suffix(): string {
109
+ return this._parsedWidgetProps.suffix || "";
110
+ }
111
+
112
+ get prefix(): string {
113
+ return this._parsedWidgetProps.prefix || "";
114
+ }
115
+
108
116
  /**
109
117
  * Values and keys
110
118
  */
package/src/Form.ts CHANGED
@@ -3,7 +3,11 @@ import Container from "./Container";
3
3
  import ContainerWidget from "./ContainerWidget";
4
4
  import Widget from "./Widget";
5
5
  import { ParsedNode } from "./helpers/nodeParser";
6
- import { evaluateAttributes, replaceEntities } from "./helpers/attributeParser";
6
+ import {
7
+ evaluateAttributes,
8
+ replaceEntities,
9
+ parseWidgetProps,
10
+ } from "./helpers/attributeParser";
7
11
  import { evaluateStates, evaluateButtonStates } from "./helpers/stateParser";
8
12
  import { parseContext } from "./helpers/contextParser";
9
13
  import { parseOnChange } from "./helpers/onChangeParser";
@@ -195,6 +199,11 @@ class Form {
195
199
  );
196
200
  }
197
201
  widgetType = this._fields[name].type;
202
+ // Merge _fields[widget_props] with attributes[widget_props]
203
+ attributes.widget_props = {
204
+ ...parseWidgetProps(attributes.widget_props),
205
+ ...(this._fields[name].widget_props || {}),
206
+ };
198
207
  }
199
208
  tagAttributes = {
200
209
  ...this._fields[name],
package/src/Widget.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { replaceEntities } from "./helpers/attributeParser";
1
+ import { replaceEntities, parseWidgetProps } from "./helpers/attributeParser";
2
2
 
3
3
  abstract class Widget {
4
4
  /**
@@ -172,17 +172,7 @@ abstract class Widget {
172
172
  this._fieldType = props.fieldsWidgetType;
173
173
  }
174
174
  if (props.widget_props) {
175
- if (typeof props.widget_props === "string") {
176
- try {
177
- this._parsedWidgetProps = JSON.parse(
178
- props.widget_props.replace(/'/g, '"'),
179
- );
180
- } catch (err) {
181
- console.error("Error parsing widget_props");
182
- }
183
- } else {
184
- this._parsedWidgetProps = props.widget_props;
185
- }
175
+ this._parsedWidgetProps = parseWidgetProps(props.widget_props);
186
176
  }
187
177
  if (props.key) {
188
178
  this._key = props.key;
@@ -184,6 +184,22 @@ const parseAttributes = ({
184
184
  return newAttributes;
185
185
  };
186
186
 
187
+ export const parseWidgetProps = (widget_props: string | object): object => {
188
+ if (widget_props === undefined) {
189
+ return {};
190
+ }
191
+ if (typeof widget_props === "string") {
192
+ try {
193
+ return JSON.parse(widget_props.replace(/'/g, '"'));
194
+ } catch (err) {
195
+ console.error("Error parsing widget_props");
196
+ return {};
197
+ }
198
+ } else {
199
+ return widget_props;
200
+ }
201
+ };
202
+
187
203
  export const parseJsonAttributes = ({
188
204
  attrs,
189
205
  values,
@@ -6015,4 +6015,34 @@ describe("A Form", () => {
6015
6015
  expect(field_char?.type).toBe("arrow_steps");
6016
6016
  expect(field_char?.id).toBe("field_char");
6017
6017
  });
6018
+ describe("If the field has widget_props", () => {
6019
+ it("should merge widget_props from fields definition and xml", () => {
6020
+ const fields = {
6021
+ field_integer: {
6022
+ readonly: 1,
6023
+ string: "Power",
6024
+ type: "integer",
6025
+ widget_props: {
6026
+ suffix: "kW",
6027
+ },
6028
+ },
6029
+ };
6030
+
6031
+ const xmlViewForm = `<?xml version="1.0"?>
6032
+ <form string="Form1">
6033
+ <field name="field_integer" widget_props="{'prefix': 'Wow'}" />
6034
+ </form>`;
6035
+
6036
+ const form = new Form(fields);
6037
+ form.parse(xmlViewForm, {
6038
+ values: {
6039
+ field_integer: 10,
6040
+ },
6041
+ });
6042
+ const field = form.findById("field_integer") as Field;
6043
+ expect(field).toBeDefined();
6044
+ expect(field.suffix).toBe("kW");
6045
+ expect(field.prefix).toBe("Wow");
6046
+ });
6047
+ });
6018
6048
  });