@shopify/app-bridge-core
Version:
**[Join our team and work on libraries like this one.](https://www.shopify.ca/careers)**
95 lines (92 loc) • 2.71 kB
JavaScript
import { getEventNameSpace, actionWrapper, getMergedProps } from '../helper.js';
import { ActionSet } from '../ActionSet.js';
import { ComponentType, Group } from '../types.js';
var Action;
(function (Action) {
Action["CLICK"] = "CLICK";
Action["UPDATE"] = "UPDATE";
})(Action || (Action = {}));
var Icon;
(function (Icon) {
Icon["Print"] = "print";
})(Icon || (Icon = {}));
var Style;
(function (Style) {
Style["Danger"] = "danger";
})(Style || (Style = {}));
function clickButton(group, component, payload) {
const { id } = component;
const action = getEventNameSpace(group, Action.CLICK, component);
const buttonPayload = {
id,
payload,
};
return actionWrapper({ type: action, group, payload: buttonPayload });
}
function update(group, component, props) {
const { id } = component;
const { label } = props;
const action = getEventNameSpace(group, Action.UPDATE, component);
const buttonPayload = {
...props,
id,
label,
};
return actionWrapper({ type: action, group, payload: buttonPayload });
}
class Button extends ActionSet {
label;
disabled = false;
icon;
style;
loading = false;
plain = false;
constructor(app, options) {
super(app, ComponentType.Button, Group.Button);
this.set(options, false);
}
get options() {
return {
disabled: this.disabled,
icon: this.icon,
label: this.label,
style: this.style,
loading: this.loading,
plain: this.plain,
};
}
get payload() {
return {
...this.options,
id: this.id,
};
}
set(options, shouldUpdate = true) {
const mergedOptions = getMergedProps(this.options, options);
const { label, disabled, icon, style, loading, plain } = mergedOptions;
this.label = label;
this.disabled = Boolean(disabled);
this.icon = icon;
this.style = style;
this.loading = Boolean(loading);
this.plain = Boolean(plain);
if (shouldUpdate) {
this.dispatch(Action.UPDATE);
}
return this;
}
dispatch(action, payload) {
switch (action) {
case Action.CLICK:
this.app.dispatch(clickButton(this.group, this.component, payload));
break;
case Action.UPDATE: {
const updateAction = update(this.group, this.component, this.payload);
this.app.dispatch(updateAction);
break;
}
}
return this;
}
}
export { Action, Button, Icon, Style, clickButton, update };