Allow custom classes for any widget

This commit is contained in:
Robin Ward 2016-02-22 14:09:53 -05:00
parent 54904133a0
commit 65553d7b70
2 changed files with 28 additions and 14 deletions

View File

@ -0,0 +1,21 @@
import Connector from 'discourse/widgets/connector';
import { h } from 'virtual-dom';
class DecoratorHelper {
constructor(widget, attrs, state) {
this.widget = widget;
this.attrs = attrs;
this.state = state;
}
connect(details) {
return new Connector(this.widget, details);
}
getModel() {
return this.widget.findAncestorModel();
}
}
DecoratorHelper.prototype.h = h;
export default DecoratorHelper;

View File

@ -1,6 +1,6 @@
import { WidgetClickHook, WidgetClickOutsideHook } from 'discourse/widgets/click-hook';
import { h } from 'virtual-dom';
import Connector from 'discourse/widgets/connector';
import DecoratorHelper from 'discourse/widgets/decorator-helper';
function emptyContent() { }
@ -17,19 +17,6 @@ export function renderedKey(key) {
const _decorators = {};
class DecoratorHelper {
constructor(container, attrs, state) {
this.container = container;
this.attrs = attrs;
this.state = state;
}
connect(details) {
return new Connector(this.container, details);
}
}
DecoratorHelper.prototype.h = h;
export function decorateWidget(widgetName, cb) {
_decorators[widgetName] = _decorators[widgetName] || [];
_decorators[widgetName].push(cb);
@ -52,6 +39,12 @@ function drawWidget(builder, attrs, state) {
if (this.buildClasses) {
let classes = this.buildClasses(attrs, state) || [];
if (!Array.isArray(classes)) { classes = [classes]; }
const customClasses = applyDecorators(this, 'classNames', attrs, state);
if (customClasses && customClasses.length) {
classes = classes.concat(customClasses);
}
if (classes.length) {
properties.className = classes.join(' ');
}