Skip to content

Commit

Permalink
Allow Telepath's widget rendering to take options
Browse files Browse the repository at this point in the history
This will allow us supply attributes to be included in the widgets'
rendered html. Similar how to Django widgets can take a `attrs`
argument.
  • Loading branch information
Stormheg committed Jul 27, 2023
1 parent dec8ec4 commit 915fcdd
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions client/src/entrypoints/admin/telepath/widgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@
import { gettext } from '../../../utils/gettext';

class BoundWidget {
constructor(element, name, idForLabel, initialState, parentCapabilities) {
constructor(
element,
name,
idForLabel,
initialState,
parentCapabilities,
options,
) {
var selector = ':input[name="' + name + '"]';
this.input = element.find(selector).addBack(selector); // find, including element itself
this.idForLabel = idForLabel;
this.setState(initialState);
this.parentCapabilities = parentCapabilities || new Map();
this.options = options;
}

getValue() {
Expand Down Expand Up @@ -49,10 +57,21 @@ class Widget {

boundWidgetClass = BoundWidget;

render(placeholder, name, id, initialState, parentCapabilities) {
render(placeholder, name, id, initialState, parentCapabilities, options) {
var html = this.html.replace(/__NAME__/g, name).replace(/__ID__/g, id);
var idForLabel = this.idPattern.replace(/__ID__/g, id);
var dom = $(html);

// Add any extra attributes we received to the HTML of the widget
if (
options &&
'attributes' in options &&
typeof options.attributes === 'object'
) {
Object.entries(options.attributes).forEach(([key, value]) => {
dom.attr(key, value);
});
}
$(placeholder).replaceWith(dom);
// eslint-disable-next-line new-cap
return new this.boundWidgetClass(
Expand All @@ -61,6 +80,7 @@ class Widget {
idForLabel,
initialState,
parentCapabilities,
options,
);
}
}
Expand Down

0 comments on commit 915fcdd

Please sign in to comment.