From f9d07c62a091bf44a36b38454c5b1ba86559270a Mon Sep 17 00:00:00 2001 From: mm <25961416+mlmoravek@users.noreply.github.com> Date: Tue, 23 Apr 2024 13:34:10 +0200 Subject: [PATCH] fix(field): fix slot invoked outside render function (#906) --- packages/docs/components/Field.md | 37 ++--- packages/oruga/src/components/field/Field.vue | 135 +++++++++++------- .../oruga/src/components/field/FieldBody.vue | 63 -------- .../src/components/field/examples/index.vue | 2 +- .../src/components/field/fieldInjection.ts | 3 +- packages/oruga/src/utils/helpers.ts | 11 ++ 6 files changed, 119 insertions(+), 132 deletions(-) delete mode 100644 packages/oruga/src/components/field/FieldBody.vue diff --git a/packages/docs/components/Field.md b/packages/docs/components/Field.md index 8d7af153f..b87ff70bd 100644 --- a/packages/docs/components/Field.md +++ b/packages/docs/components/Field.md @@ -37,27 +37,28 @@ title: Field ### Props -| Prop name | Description | Type | Values | Default | -| ---------------- | ------------------------------------------------------------------------------------------------------------------- | ------- | ------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- | -| addons | Field automatically attach controls together | boolean | - | true | -| groupMultiline | Allow controls to fill up multiple lines, making it responsive | boolean | - | false | -| grouped | Direct child components/elements of Field will be grouped horizontally
(see which ones at the top of the page). | boolean | - | false | -| horizontal | Group label and control on the same line for horizontal forms | boolean | - | false | -| label | Field label | string | - | | -| labelFor | Same as native for set on the label | string | - | | -| labelSize | Vertical size of input | string | `small`, `medium`, `large` |
From config:
field: {
  labelsize: undefined
}
| -| message | Help message text | string | - | | -| mobileBreakpoint | Mobile breakpoint as max-width value | string | - |
From config:
field: {
  mobileBreakpoint: undefined
}
| -| override | Override existing theme classes completely | boolean | - | | -| variant | Color of the field and help message, also adds a matching icon.
Used by Input, Select and Autocomplete. | string | `primary`, `info`, `success`, `warning`, `danger`, `and any other custom color` | | +| Prop name | Description | Type | Values | Default | +| ---------------- | ------------------------------------------------------------------------------------------------------------------- | ---------------- | ------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- | +| addons | Field automatically attach controls together | boolean | - | true | +| groupMultiline | Allow controls to fill up multiple lines, making it responsive | boolean | - | false | +| grouped | Direct child components/elements of Field will be grouped horizontally
(see which ones at the top of the page). | boolean | - | false | +| horizontal | Group label and control on the same line for horizontal forms | boolean | - | false | +| label | Field label | string | - | | +| labelFor | Same as native for set on the label | string | - | | +| labelSize | Vertical size of input | string | `small`, `medium`, `large` |
From config:
field: {
  labelsize: undefined
}
| +| message | Help message text | string | - | | +| messageTag | | DynamicComponent | - |
From config:
field: {
  messageTag: "p"
}
| +| mobileBreakpoint | Mobile breakpoint as max-width value | string | - |
From config:
field: {
  mobileBreakpoint: undefined
}
| +| override | Override existing theme classes completely | boolean | - | | +| variant | Color of the field and help message, also adds a matching icon.
Used by Input, Select and Autocomplete. | string | `primary`, `info`, `success`, `warning`, `danger`, `and any other custom color` | | ### Slots -| Name | Description | Bindings | -| ------- | -------------------- | -------- | -| label | Override the label | | -| default | Default content | | -| message | Override the message | | +| Name | Description | Bindings | +| ------- | -------------------- | ------------------------------------ | +| label | Override the label | **label** `string` - label property | +| message | Override the message | **message** `string` - field message | +| default | Default content | | diff --git a/packages/oruga/src/components/field/Field.vue b/packages/oruga/src/components/field/Field.vue index 1e8f22669..93e80e877 100644 --- a/packages/oruga/src/components/field/Field.vue +++ b/packages/oruga/src/components/field/Field.vue @@ -1,14 +1,20 @@ @@ -332,27 +345,45 @@ const innerFieldClasses = defineClasses( - - - - +
+ +
-
+
- {{ fieldMessage }} -

+ + {{ fieldMessage }} + +
diff --git a/packages/oruga/src/components/field/FieldBody.vue b/packages/oruga/src/components/field/FieldBody.vue deleted file mode 100644 index ca8dccc94..000000000 --- a/packages/oruga/src/components/field/FieldBody.vue +++ /dev/null @@ -1,63 +0,0 @@ - diff --git a/packages/oruga/src/components/field/examples/index.vue b/packages/oruga/src/components/field/examples/index.vue index 237c4fb98..ed35b8e2b 100644 --- a/packages/oruga/src/components/field/examples/index.vue +++ b/packages/oruga/src/components/field/examples/index.vue @@ -28,6 +28,6 @@ import SlotsCode from "./slots.vue?raw";

Horizontal

-

Slots

+

Slots

diff --git a/packages/oruga/src/components/field/fieldInjection.ts b/packages/oruga/src/components/field/fieldInjection.ts index 5685861ff..593d2b0ba 100644 --- a/packages/oruga/src/components/field/fieldInjection.ts +++ b/packages/oruga/src/components/field/fieldInjection.ts @@ -18,6 +18,7 @@ type FieldData = { hasMessage: boolean; fieldVariant: string; fieldMessage: string; + addInnerField: () => void; setInputId: (value: string) => void; setFocus: (value: boolean) => void; setFilled: (value: boolean) => void; @@ -41,7 +42,7 @@ export function provideField(data: ProvidedField): void { /** Inject parent field component if used inside one. **/ export function injectField() { - const parentField = inject($FieldKey); + const parentField = inject($FieldKey, undefined); /** * Get the type prop from parent if it's a Field. diff --git a/packages/oruga/src/utils/helpers.ts b/packages/oruga/src/utils/helpers.ts index 579918d55..dcb058db8 100644 --- a/packages/oruga/src/utils/helpers.ts +++ b/packages/oruga/src/utils/helpers.ts @@ -1,3 +1,5 @@ +import { Comment, Fragment, Text } from "vue"; + /** * Generates a random string */ @@ -228,6 +230,15 @@ export function removeDiacriticsFromString(value: string): string { return value.normalize("NFD").replace(/[\u0300-\u036f]/g, ""); } +/** checks if a vue vnode is empty */ +export function isVNodeEmpty(vnode): boolean { + if (!vnode) return true; + if (vnode.type === Comment) return true; + if (vnode.type === Text && !vnode.children.trim()) return true; + if (vnode.type === Fragment && isVNodeEmpty(vnode.children)) return true; + return false; +} + /** * Mobile detection * https://www.abeautifulsite.net/detecting-mobile-devices-with-javascript