meilisearch-admin/app/components/button.js

91 lines
1.8 KiB
JavaScript
Raw Normal View History

2022-03-27 13:04:08 +00:00
import Component from '@glimmer/component';
import { action } from '@ember/object';
import { tracked } from '@glimmer/tracking';
2022-04-27 08:44:49 +00:00
import { ClockOutline, Check, AlertCircleOutline } from 'ember-mdi/icons';
2022-03-27 13:04:08 +00:00
export default class ButtonComponent extends Component {
@tracked _state = 'default';
2022-03-30 06:58:35 +00:00
get isActive() {
return (
this.args.group &&
this.args.value &&
this.args.group.args.value === this.args.value
);
}
2022-03-27 13:04:08 +00:00
get disabled() {
if (this.args.disabled !== undefined) {
return this.args.disabled;
}
return this.isPending;
}
get type() {
return this.args.type ?? 'button';
}
get icon() {
2022-04-26 10:28:40 +00:00
if (this.isPending) {
return this.args.pendingIcon ?? ClockOutline;
} else if (this.isFulfilled) {
return this.args.fulfilledIcon ?? Check;
} else if (this.isRejected) {
return this.args.rejectedIcon ?? AlertCircleOutline;
2022-03-27 13:04:08 +00:00
}
return this.args.icon ?? null;
}
get state() {
return this.args.state ?? this._state;
}
set state(state) {
this._state = state;
}
get isPending() {
return this.state === 'pending';
}
get isFulfilled() {
return this.state === 'fulfilled';
}
get isRejected() {
return this.state === 'rejected';
}
get isSettled() {
return this.isFulfilled || this.isRejected;
}
get text() {
return this.args[`${this.state}Text`] || this.args.text;
}
@action
async handleClick() {
if (typeof this.args.onClick !== 'function') {
return;
}
this.state = 'pending';
try {
2022-03-30 06:58:35 +00:00
await this.args.onClick(this.args.value);
2022-03-27 13:04:08 +00:00
if (!this.isDestroyed) {
this.state = 'fulfilled';
}
} catch (error) {
if (!this.isDestroyed) {
this.state = 'rejected';
}
throw error;
}
}
}