diff --git a/packages/vue/src/components/IonInput.ts b/packages/vue/src/components/IonInput.ts new file mode 100644 index 00000000000..027ebd0a0d8 --- /dev/null +++ b/packages/vue/src/components/IonInput.ts @@ -0,0 +1,17 @@ +import type { JSX } from "@ionic/core"; +import type { StencilVueComponent } from "@stencil/vue-output-target/runtime"; + +import { IonInput as IonInputProxy } from "../proxies"; +import { withLazyModel } from "../utils/lazy-model"; + +/** + * `ion-input` with `v-model.lazy` support. + * + * The generated proxy always updates the `v-model` binding on `ionInput`, + * so `.lazy` is ignored. This wrapper commits the value on `ionChange` + * instead when `modelModifiers.lazy` is set, and otherwise behaves exactly + * like the generated proxy. See `../utils/lazy-model.ts`. + */ +export const IonInput = withLazyModel( + IonInputProxy +) as unknown as StencilVueComponent; diff --git a/packages/vue/src/components/IonInputOtp.ts b/packages/vue/src/components/IonInputOtp.ts new file mode 100644 index 00000000000..c4294eef2a0 --- /dev/null +++ b/packages/vue/src/components/IonInputOtp.ts @@ -0,0 +1,17 @@ +import type { JSX } from "@ionic/core"; +import type { StencilVueComponent } from "@stencil/vue-output-target/runtime"; + +import { IonInputOtp as IonInputOtpProxy } from "../proxies"; +import { withLazyModel } from "../utils/lazy-model"; + +/** + * `ion-input-otp` with `v-model.lazy` support. + * + * The generated proxy always updates the `v-model` binding on `ionInput`, + * so `.lazy` is ignored. This wrapper commits the value on `ionChange` + * instead when `modelModifiers.lazy` is set, and otherwise behaves exactly + * like the generated proxy. See `../utils/lazy-model.ts`. + */ +export const IonInputOtp = withLazyModel( + IonInputOtpProxy +) as unknown as StencilVueComponent; diff --git a/packages/vue/src/components/IonRange.ts b/packages/vue/src/components/IonRange.ts new file mode 100644 index 00000000000..fab7bd298c2 --- /dev/null +++ b/packages/vue/src/components/IonRange.ts @@ -0,0 +1,17 @@ +import type { JSX } from "@ionic/core"; +import type { StencilVueComponent } from "@stencil/vue-output-target/runtime"; + +import { IonRange as IonRangeProxy } from "../proxies"; +import { withLazyModel } from "../utils/lazy-model"; + +/** + * `ion-range` with `v-model.lazy` support. + * + * The generated proxy always updates the `v-model` binding on `ionInput`, + * so `.lazy` is ignored. This wrapper commits the value on `ionChange` + * instead when `modelModifiers.lazy` is set, and otherwise behaves exactly + * like the generated proxy. See `../utils/lazy-model.ts`. + */ +export const IonRange = withLazyModel( + IonRangeProxy +) as unknown as StencilVueComponent; diff --git a/packages/vue/src/components/IonSearchbar.ts b/packages/vue/src/components/IonSearchbar.ts new file mode 100644 index 00000000000..3e18886fdb6 --- /dev/null +++ b/packages/vue/src/components/IonSearchbar.ts @@ -0,0 +1,20 @@ +import type { JSX } from "@ionic/core"; +import type { StencilVueComponent } from "@stencil/vue-output-target/runtime"; + +import { IonSearchbar as IonSearchbarProxy } from "../proxies"; +import { withLazyModel } from "../utils/lazy-model"; + +/** + * `ion-searchbar` with `v-model.lazy` support. + * + * The generated proxy always updates the `v-model` binding on `ionInput`, + * so `.lazy` is ignored. This wrapper commits the value on `ionChange` + * instead when `modelModifiers.lazy` is set, and otherwise behaves exactly + * like the generated proxy. See `../utils/lazy-model.ts`. + */ +export const IonSearchbar = withLazyModel( + IonSearchbarProxy +) as unknown as StencilVueComponent< + JSX.IonSearchbar, + JSX.IonSearchbar["value"] +>; diff --git a/packages/vue/src/components/IonTextarea.ts b/packages/vue/src/components/IonTextarea.ts new file mode 100644 index 00000000000..8997fe48980 --- /dev/null +++ b/packages/vue/src/components/IonTextarea.ts @@ -0,0 +1,17 @@ +import type { JSX } from "@ionic/core"; +import type { StencilVueComponent } from "@stencil/vue-output-target/runtime"; + +import { IonTextarea as IonTextareaProxy } from "../proxies"; +import { withLazyModel } from "../utils/lazy-model"; + +/** + * `ion-textarea` with `v-model.lazy` support. + * + * The generated proxy always updates the `v-model` binding on `ionInput`, + * so `.lazy` is ignored. This wrapper commits the value on `ionChange` + * instead when `modelModifiers.lazy` is set, and otherwise behaves exactly + * like the generated proxy. See `../utils/lazy-model.ts`. + */ +export const IonTextarea = withLazyModel( + IonTextareaProxy +) as unknown as StencilVueComponent; diff --git a/packages/vue/src/index.ts b/packages/vue/src/index.ts index fd19ff6a199..82d0b306371 100644 --- a/packages/vue/src/index.ts +++ b/packages/vue/src/index.ts @@ -21,6 +21,11 @@ export { IonTabBar } from "./components/IonTabBar"; export { IonNav } from "./components/IonNav"; export { IonIcon } from "./components/IonIcon"; export { IonApp } from "./components/IonApp"; +export { IonInput } from "./components/IonInput"; +export { IonInputOtp } from "./components/IonInputOtp"; +export { IonRange } from "./components/IonRange"; +export { IonSearchbar } from "./components/IonSearchbar"; +export { IonTextarea } from "./components/IonTextarea"; export * from "./components/Overlays"; diff --git a/packages/vue/src/utils/lazy-model.ts b/packages/vue/src/utils/lazy-model.ts new file mode 100644 index 00000000000..4915c3a2b71 --- /dev/null +++ b/packages/vue/src/utils/lazy-model.ts @@ -0,0 +1,87 @@ +import { defineComponent, getCurrentInstance, h } from "vue"; +import type { Component } from "vue"; + +/** + * Wraps a generated Stencil Vue proxy so `v-model.lazy` is honoured. + * + * The proxies generated by @stencil/vue-output-target always update the + * `v-model` binding on the configured model event (`ionInput` for the input + * components). Vue passes `v-model.lazy` through as a `modelModifiers` + * prop (`{ lazy: true }`), which the generated proxy never reads, so the + * bound value updates on every keystroke even with `.lazy`. + * + * The wrapper keeps the default behaviour (update on the inner proxy's + * `update:modelValue`, which fires on `ionInput`) and only changes it when + * `.lazy` is present: then it drops those updates and commits the value on + * `ionChange` instead, which fires at the same moments as a native `change` + * event. The user's own `ionChange` listener, if any, is still called. + * + * `modelValue` is only forwarded to the inner proxy when the parent actually + * provided it (checked via the vnode props, so `v-model` bound to + * `undefined` still clears the input). Otherwise the inner proxy falls back + * to the `value` prop, exactly as it does without the wrapper, so + * `:value="..."` without `v-model` keeps working. + */ +export function withLazyModel( + component: T, + commitEvent = "ionChange" +) { + const commitHandlerKey = `on${commitEvent[0].toUpperCase()}${commitEvent.slice( + 1 + )}`; + + return defineComponent({ + name: (component as unknown as { name?: string }).name, + props: { + modelValue: { + type: [String, Number, Boolean, Object, Array], + default: undefined, + }, + modelModifiers: { + type: Object, + default: () => ({}), + }, + }, + emits: ["update:modelValue"], + setup(props, { attrs, slots, emit }) { + const isLazy = () => + Boolean( + (props.modelModifiers as Record | undefined)?.lazy + ); + + return () => { + const instance = getCurrentInstance(); + const hasModelValue = + instance?.vnode.props != null && "modelValue" in instance.vnode.props; + const userCommitHandler = (attrs as Record)[ + commitHandlerKey + ] as ((e: CustomEvent) => void) | undefined; + + return h( + component as never, + { + ...attrs, + ...(hasModelValue ? { modelValue: props.modelValue } : {}), + "onUpdate:modelValue": (value: unknown) => { + if (!isLazy()) { + emit("update:modelValue", value); + } + }, + [commitHandlerKey]: (e: CustomEvent) => { + if (typeof userCommitHandler === "function") { + userCommitHandler(e); + } + if (isLazy()) { + emit( + "update:modelValue", + (e.target as { value?: unknown } | null)?.value + ); + } + }, + }, + slots + ); + }; + }, + }); +} diff --git a/packages/vue/test/base/tests/unit/v-model-lazy.spec.ts b/packages/vue/test/base/tests/unit/v-model-lazy.spec.ts new file mode 100644 index 00000000000..3887ccb8e38 --- /dev/null +++ b/packages/vue/test/base/tests/unit/v-model-lazy.spec.ts @@ -0,0 +1,121 @@ +import { mount } from "@vue/test-utils"; +import { nextTick } from "vue"; +import { describe, expect, it, vi } from "vitest"; +import { + IonicVue, + IonInput, + IonInputOtp, + IonRange, + IonSearchbar, + IonTextarea, +} from "@ionic/vue"; + +/** + * Regression tests for ionic-team/ionic-framework#31761: + * `v-model.lazy` is ignored by ion-input, ion-textarea, ion-searchbar, + * ion-input-otp and ion-range — the model updates on every `ionInput` + * instead of committing on `ionChange`. + * + * The components are mounted directly (rather than through a parent + * template) so the `modelModifiers` prop and the emitted + * `update:modelValue` events are observed without template compilation + * in the way. + */ + +type Component = Parameters[0]; + +const fireValue = (el: HTMLElement, value: unknown, event: string) => { + (el as unknown as { value: unknown }).value = value; + el.dispatchEvent(new CustomEvent(event, { bubbles: true, composed: true })); +}; + +describe("v-model.lazy", () => { + const cases: Array<{ + name: string; + component: Component; + typedValue: unknown; + }> = [ + { name: "ion-input", component: IonInput, typedValue: "typed" }, + { name: "ion-textarea", component: IonTextarea, typedValue: "typed" }, + { name: "ion-searchbar", component: IonSearchbar, typedValue: "typed" }, + { name: "ion-input-otp", component: IonInputOtp, typedValue: "typed" }, + { name: "ion-range", component: IonRange, typedValue: 42 }, + ]; + + it.each(cases)( + "$name: does not update on ionInput, commits on ionChange", + async ({ component, typedValue }) => { + const onUpdate = vi.fn(); + const wrapper = mount(component as never, { + props: { + modelValue: "initial", + modelModifiers: { lazy: true }, + "onUpdate:modelValue": onUpdate, + }, + global: { plugins: [IonicVue] }, + }); + await nextTick(); + const el = wrapper.element as HTMLElement; + + fireValue(el, typedValue, "ionInput"); + await nextTick(); + expect(onUpdate).not.toHaveBeenCalled(); + + fireValue(el, typedValue, "ionChange"); + await nextTick(); + expect(onUpdate).toHaveBeenCalledTimes(1); + expect(onUpdate).toHaveBeenCalledWith(typedValue); + } + ); + + it("ion-input: updates on ionInput without .lazy", async () => { + const onUpdate = vi.fn(); + const wrapper = mount(IonInput as never, { + props: { + modelValue: "initial", + "onUpdate:modelValue": onUpdate, + }, + global: { plugins: [IonicVue] }, + }); + await nextTick(); + const el = wrapper.element as HTMLElement; + + fireValue(el, "typed", "ionInput"); + await nextTick(); + expect(onUpdate).toHaveBeenCalledTimes(1); + expect(onUpdate).toHaveBeenCalledWith("typed"); + }); + + it("ion-input: still calls the user ionChange listener with .lazy", async () => { + const onUpdate = vi.fn(); + const onChange = vi.fn(); + const wrapper = mount(IonInput as never, { + props: { + modelValue: "initial", + modelModifiers: { lazy: true }, + "onUpdate:modelValue": onUpdate, + onIonChange: onChange, + }, + global: { plugins: [IonicVue] }, + }); + await nextTick(); + const el = wrapper.element as HTMLElement; + + fireValue(el, "typed", "ionChange"); + await nextTick(); + expect(onChange).toHaveBeenCalled(); + expect(onUpdate).toHaveBeenCalledTimes(1); + expect(onUpdate).toHaveBeenCalledWith("typed"); + }); + + it("ion-input: :value without v-model still renders the value", async () => { + const wrapper = mount(IonInput as never, { + props: { value: "initial" }, + global: { plugins: [IonicVue] }, + }); + await nextTick(); + expect((wrapper.element as unknown as { value: unknown }).value).toBe( + "initial" + ); + }); +});