From ed18e7454986ed104fc73b77e88573b3c1df8566 Mon Sep 17 00:00:00 2001 From: Romain Hamel Date: Mon, 16 Sep 2024 11:37:38 +0200 Subject: [PATCH] feat(Button): loading-auto (#2198) --- .../button/ButtonLoadingAutoExample.vue | 11 +++ .../button/ButtonLoadingAutoFormExample.vue | 23 +++++++ .../examples/form/FormExampleElements.vue | 4 +- .../examples/form/FormExampleNested.vue | 2 +- .../examples/form/FormExampleOnError.vue | 8 ++- .../content/examples/form/FormExampleZod.vue | 2 +- docs/content/3.components/button.md | 9 ++- .../app/components/FormNestedExample.vue | 4 +- playground/app/pages/components/button.vue | 6 +- playground/app/pages/components/form.vue | 4 +- src/runtime/components/Button.vue | 38 +++++++++-- src/runtime/components/Form.vue | 37 ++++++---- src/runtime/composables/useFormField.ts | 1 + test/components/Button.spec.ts | 68 ++++++++++++++++++- test/components/Form.spec.ts | 9 +-- 15 files changed, 191 insertions(+), 35 deletions(-) create mode 100644 docs/app/components/content/examples/button/ButtonLoadingAutoExample.vue create mode 100644 docs/app/components/content/examples/button/ButtonLoadingAutoFormExample.vue diff --git a/docs/app/components/content/examples/button/ButtonLoadingAutoExample.vue b/docs/app/components/content/examples/button/ButtonLoadingAutoExample.vue new file mode 100644 index 00000000..05458636 --- /dev/null +++ b/docs/app/components/content/examples/button/ButtonLoadingAutoExample.vue @@ -0,0 +1,11 @@ + + + diff --git a/docs/app/components/content/examples/button/ButtonLoadingAutoFormExample.vue b/docs/app/components/content/examples/button/ButtonLoadingAutoFormExample.vue new file mode 100644 index 00000000..cefd2a55 --- /dev/null +++ b/docs/app/components/content/examples/button/ButtonLoadingAutoFormExample.vue @@ -0,0 +1,23 @@ + + + diff --git a/docs/app/components/content/examples/form/FormExampleElements.vue b/docs/app/components/content/examples/form/FormExampleElements.vue index 671eee7a..b7283ffe 100644 --- a/docs/app/components/content/examples/form/FormExampleElements.vue +++ b/docs/app/components/content/examples/form/FormExampleElements.vue @@ -103,11 +103,11 @@ async function onSubmit(event: FormSubmitEvent) {
- + Submit - + Clear
diff --git a/docs/app/components/content/examples/form/FormExampleNested.vue b/docs/app/components/content/examples/form/FormExampleNested.vue index 932eb34f..dd291084 100644 --- a/docs/app/components/content/examples/form/FormExampleNested.vue +++ b/docs/app/components/content/examples/form/FormExampleNested.vue @@ -29,7 +29,7 @@ async function onSubmit(event: FormSubmitEvent) { :state="state" :schema="schema" class="gap-4 flex flex-col w-60" - @submit="(event) => onSubmit(event)" + @submit="onSubmit" > diff --git a/docs/app/components/content/examples/form/FormExampleOnError.vue b/docs/app/components/content/examples/form/FormExampleOnError.vue index 26b0cf6a..8bf1a8ee 100644 --- a/docs/app/components/content/examples/form/FormExampleOnError.vue +++ b/docs/app/components/content/examples/form/FormExampleOnError.vue @@ -20,9 +20,11 @@ async function onSubmit(event: FormSubmitEvent) { } async function onError(event: FormErrorEvent) { - const element = document.getElementById(event.errors[0].id) - element?.focus() - element?.scrollIntoView({ behavior: 'smooth', block: 'center' }) + if (event?.errors?.[0]?.id) { + const element = document.getElementById(event.errors[0].id) + element?.focus() + element?.scrollIntoView({ behavior: 'smooth', block: 'center' }) + } } diff --git a/docs/app/components/content/examples/form/FormExampleZod.vue b/docs/app/components/content/examples/form/FormExampleZod.vue index f838729b..d88ed2ad 100644 --- a/docs/app/components/content/examples/form/FormExampleZod.vue +++ b/docs/app/components/content/examples/form/FormExampleZod.vue @@ -9,7 +9,7 @@ const schema = z.object({ type Schema = z.output -const state = reactive({ +const state = reactive>({ email: undefined, password: undefined }) diff --git a/docs/content/3.components/button.md b/docs/content/3.components/button.md index a0bbb2e3..ddcf1588 100644 --- a/docs/content/3.components/button.md +++ b/docs/content/3.components/button.md @@ -140,7 +140,6 @@ props: slots: default: Button --- - Button :: @@ -148,6 +147,14 @@ Button You can customize this icon globally in your `app.config.ts` under `ui.icons.loading` key. :: +Use the `loading-auto` prop to show the loading icon automatically while the `@click` promise is pending. + +:component-example{name="button-loading-auto-example"} + +This also works with the [Form](/components/form) component. + +:component-example{name="button-loading-auto-form-example"} + ### Disabled Use the `disabled` prop to disable the Button. diff --git a/playground/app/components/FormNestedExample.vue b/playground/app/components/FormNestedExample.vue index a6edd428..5ef7d259 100644 --- a/playground/app/components/FormNestedExample.vue +++ b/playground/app/components/FormNestedExample.vue @@ -35,8 +35,8 @@ function onError(event: any) { :state="state" :schema="schema" class="gap-4 flex flex-col w-60" - @submit="(event) => onSubmit(event)" - @error="(event) => onError(event)" + @submit="onSubmit" + @error="onError" > diff --git a/playground/app/pages/components/button.vue b/playground/app/pages/components/button.vue index 8b1ff50f..02587d19 100644 --- a/playground/app/pages/components/button.vue +++ b/playground/app/pages/components/button.vue @@ -4,6 +4,10 @@ import theme from '#build/ui/button' const sizes = Object.keys(theme.variants.size) as Array const variants = Object.keys(theme.variants.variant) as Array + +function onClick() { + return new Promise(res => setTimeout(res, 5000)) +}