meilisearch-admin/app/components/button.js

82 lines
1.5 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';
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 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() {
2022-04-27 11:07:57 +00:00
return this.args[`${this.state}Text`] ?? this.args.text;
2022-03-27 13:04:08 +00:00
}
@action
async handleClick() {
if (typeof this.args.onClick !== 'function') {
return;
}
try {
2022-04-27 11:07:57 +00:00
const maybePromise = this.args.onClick(this.args.value);
2022-03-27 13:04:08 +00:00
2022-04-27 11:07:57 +00:00
if (typeof maybePromise?.then === 'function') {
this.state = 'pending';
await maybePromise;
if (!this.isDestroyed) {
this.state = 'fulfilled';
}
2022-03-27 13:04:08 +00:00
}
} catch (error) {
if (!this.isDestroyed) {
this.state = 'rejected';
}
throw error;
}
}
}