-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(validated-button): yield customizable button
- Loading branch information
1 parent
a83c7bf
commit 30d9c27
Showing
6 changed files
with
291 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,55 @@ | ||
import { action } from "@ember/object"; | ||
import Component from "@glimmer/component"; | ||
import { tracked } from "@glimmer/tracking"; | ||
import { resolve } from "rsvp"; | ||
|
||
import themedComponent from "../-private/themed-component"; | ||
|
||
const ON_CLICK = "on-click"; | ||
const ON_INVALID_CLICK = "on-invalid-click"; | ||
export default class ValidatedButtonComponent extends Component { | ||
@themedComponent("validated-button/button") buttonComponent; | ||
|
||
@tracked _loading; | ||
|
||
get loading() { | ||
return this.args.loading || this._loading; | ||
} | ||
|
||
@action | ||
async click(event) { | ||
// handle only clicks for custom buttons | ||
// everything else is handled by the validated form itself | ||
if (this.args.type !== "button") { | ||
return this.args.action(event); | ||
} | ||
|
||
event.preventDefault(); | ||
const model = this.args.model; | ||
|
||
if (!model || !model.validate) { | ||
this.runCallback(ON_CLICK); | ||
return; | ||
} | ||
|
||
await model.validate(); | ||
|
||
if (model.get("isInvalid")) { | ||
this.runCallback(ON_INVALID_CLICK); | ||
} else { | ||
this.runCallback(ON_CLICK); | ||
} | ||
} | ||
|
||
runCallback(callbackProp) { | ||
const callback = this.args[callbackProp]; | ||
if (typeof callback !== "function") { | ||
return; | ||
} | ||
|
||
this._loading = true; | ||
resolve(callback(this.args.model)).finally(() => { | ||
this._loading = false; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.