-
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 (#705)
* feat(validated-button): yield customizable button * chore(deps): upgrade to fix CI issues with babel * doc: delete legacy {{link-to}} ref * test: rewrite spare tests to octane/glimmer syntax * docs: glimmer syntax in form demo
- Loading branch information
1 parent
a83c7bf
commit 18d141e
Showing
34 changed files
with
2,668 additions
and
1,104 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
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
62 changes: 48 additions & 14 deletions
62
tests/dummy/app/templates/docs/components/validated-button.md
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,32 +1,66 @@ | ||
# Validated button | ||
|
||
`{{validated-form}}` also yields a submit button component that can be | ||
accessed with `{{f.submit}}`. You can also use it as a block style component | ||
`{{#f.submit}}Test{{/f.submit}}` if you don't want to pass the label as a | ||
property. It takes the following properties: | ||
`{{validated-form}}` yields two kinds of button components: | ||
- `{{f.submit}}`: a submit button for the form | ||
- `{{f.button}}`: a customizable button without HTML-form specific functionality. | ||
|
||
**label `<String>`** | ||
You can use them as a block style component `{{#f.submit}}Test{{/f.submit}}` if you don't want to pass the label as a | ||
property. | ||
|
||
Both take the following properties: | ||
|
||
**label `<String>`** | ||
The label of the form button. | ||
|
||
**type `<String>`** | ||
Type of the button. Default: `button`. | ||
**type `<String>`** | ||
Type of the button. Default for submit: `submit` and for standard button: `button`. | ||
*Watch out:* If you define `type=submit` then the `on-submit` handler of the form will be triggered. | ||
|
||
**disabled `<Boolean>`** | ||
**disabled `<Boolean>`** | ||
Specifies if the button is disabled. | ||
|
||
**loading `<Boolean>`** | ||
**loading `<Boolean>`** | ||
Specifies if the button is loading. Default: Automatic integration of `ember-concurrency`. | ||
|
||
<!-- prettier-ignore-start --> | ||
{{#docs-demo as |demo|}} | ||
{{#demo.example name='button-template.hbs'}} | ||
{{#validated-form on-submit=(action (mut saved) true) as |f|}} | ||
{{f.submit label='Save'}} | ||
{{#f.submit}}Still save but in block style...{{/f.submit}} | ||
{{if saved 'Saved!'}} | ||
{{/validated-form}} | ||
<ValidatedForm @on-submit={{fn (mut saved) true}} as |f|> | ||
{{#let f.submit as |Submit|}} | ||
<Submit @label={{"Save"}} /> | ||
<Submit>Save button in block style...</Submit> | ||
{{/let}} | ||
{{if saved 'Saved!'}} | ||
</ValidatedForm> | ||
{{/demo.example}} | ||
|
||
{{demo.snippet 'button-template.hbs'}} | ||
{{/docs-demo}} | ||
<!-- prettier-ignore-end --> | ||
|
||
|
||
Further you can leverage the `{{f.button}}` component for custom actions. The model of the wrapping form component will get passed to the on-click handler as first argument. | ||
|
||
Passing a custom on click function is possible on the `{{f.buttton}}` via: | ||
|
||
**on-click `<Function>`** | ||
Passes an on-click function to the button component. | ||
|
||
**on-invalid-click `<Function>`** | ||
Passes a function which is triggered after clicking on the button and when the validation proved the contents to be invalid. | ||
|
||
<!-- prettier-ignore-start --> | ||
{{#docs-demo as |demo|}} | ||
{{#demo.example name='button-advanced-template.hbs'}} | ||
<ValidatedForm as |f|> | ||
{{#let f.button as |CustomButton|}} | ||
<CustomButton @label="Real Custom" @on-click={{fn (mut triggered) true}}/> | ||
<CustomButton @on-click={{fn (mut triggered) true}}>Custom action button in block style...</CustomButton> | ||
{{/let}} | ||
{{if triggered 'Action triggered!'}} | ||
</ValidatedForm> | ||
{{/demo.example}} | ||
|
||
{{demo.snippet 'button-advanced-template.hbs'}} | ||
{{/docs-demo}} | ||
<!-- prettier-ignore-end --> |
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,5 +1,4 @@ | ||
# Quickstart | ||
|
||
You'll find a basic example in [this | ||
twiddle](https://ember-twiddle.com/95b040c96b7dc60dc4d0bb2dc5f5de26?openFiles=templates.application.hbs%2C) or | ||
{{link-to 'here' 'index'}}. | ||
twiddle](https://ember-twiddle.com/95b040c96b7dc60dc4d0bb2dc5f5de26?openFiles=templates.application.hbs%2C) |
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.