Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow suffix and prefix #152

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ class Field extends Widget {
this._sum = value;
}

get suffix(): string {
return this._parsedWidgetProps.suffix || "";
}

get prefix(): string {
return this._parsedWidgetProps.prefix || "";
}

/**
* Values and keys
*/
Expand Down
11 changes: 10 additions & 1 deletion src/Form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import Container from "./Container";
import ContainerWidget from "./ContainerWidget";
import Widget from "./Widget";
import { ParsedNode } from "./helpers/nodeParser";
import { evaluateAttributes, replaceEntities } from "./helpers/attributeParser";
import {
evaluateAttributes,
replaceEntities,
parseWidgetProps,
} from "./helpers/attributeParser";
import { evaluateStates, evaluateButtonStates } from "./helpers/stateParser";
import { parseContext } from "./helpers/contextParser";
import { parseOnChange } from "./helpers/onChangeParser";
Expand Down Expand Up @@ -195,6 +199,11 @@ class Form {
);
}
widgetType = this._fields[name].type;
// Merge _fields[widget_props] with attributes[widget_props]
attributes.widget_props = {
...parseWidgetProps(attributes.widget_props),
...(this._fields[name].widget_props || {}),
};
}
tagAttributes = {
...this._fields[name],
Expand Down
14 changes: 2 additions & 12 deletions src/Widget.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { replaceEntities } from "./helpers/attributeParser";
import { replaceEntities, parseWidgetProps } from "./helpers/attributeParser";

abstract class Widget {
/**
Expand Down Expand Up @@ -161,17 +161,7 @@ abstract class Widget {
}
}
if (props.widget_props) {
if (typeof props.widget_props === "string") {
try {
this._parsedWidgetProps = JSON.parse(
props.widget_props.replace(/'/g, '"'),
);
} catch (err) {
console.error("Error parsing widget_props");
}
} else {
this._parsedWidgetProps = props.widget_props;
}
this._parsedWidgetProps = parseWidgetProps(props.widget_props);
}
if (props.key) {
this._key = props.key;
Expand Down
16 changes: 16 additions & 0 deletions src/helpers/attributeParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,22 @@ const parseAttributes = ({
return newAttributes;
};

export const parseWidgetProps = (widget_props: string | object): object => {
if (widget_props === undefined) {
return {};
}
if (typeof widget_props === "string") {
try {
return JSON.parse(widget_props.replace(/'/g, '"'));
} catch (err) {
console.error("Error parsing widget_props");
return {};
}
} else {
return widget_props;
}
};

export const parseJsonAttributes = ({
attrs,
values,
Expand Down
30 changes: 30 additions & 0 deletions src/spec/Form.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6015,4 +6015,34 @@ describe("A Form", () => {
expect(field_char?.type).toBe("arrow_steps");
expect(field_char?.id).toBe("field_char");
});
describe("If the field has widget_props", () => {
it("should merge widget_props from fields definition and xml", () => {
const fields = {
field_integer: {
readonly: 1,
string: "Power",
type: "integer",
widget_props: {
suffix: "kW",
},
},
};

const xmlViewForm = `<?xml version="1.0"?>
<form string="Form1">
<field name="field_integer" widget_props="{'prefix': 'Wow'}" />
</form>`;

const form = new Form(fields);
form.parse(xmlViewForm, {
values: {
field_integer: 10,
},
});
const field = form.findById("field_integer") as Field;
expect(field).toBeDefined();
expect(field.suffix).toBe("kW");
expect(field.prefix).toBe("Wow");
});
});
});
Loading