Skip to content

Commit

Permalink
Allow Telepath's widget rendering to take attributes
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 20, 2023
1 parent b94f16e commit 1dd6dc3
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions client/src/entrypoints/admin/telepath/widgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
import { gettext } from '../../../utils/gettext';

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

Expand Down Expand Up @@ -49,17 +50,25 @@ class Widget {

boundWidgetClass = BoundWidget;

render(placeholder, name, id, initialState, parentCapabilities) {
render(placeholder, name, id, initialState, extraAttributes, parentCapabilities) {
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 (typeof extraAttributes === "object") {
Object.entries(extraAttributes).forEach(([key, value]) => {
dom.attr(key, value)
})
}
$(placeholder).replaceWith(dom);
// eslint-disable-next-line new-cap
return new this.boundWidgetClass(
dom,
name,
idForLabel,
initialState,
extraAttributes,
parentCapabilities,
);
}
Expand Down Expand Up @@ -349,7 +358,7 @@ class DraftailRichTextArea {
this.options = options;
}

render(container, name, id, initialState, parentCapabilities) {
render(container, name, id, initialState, extraAttributes, parentCapabilities) {
const input = document.createElement('input');
input.type = 'hidden';
input.id = id;
Expand Down

0 comments on commit 1dd6dc3

Please sign in to comment.