Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/vue/src/components/IonInput.ts
Original file line number Diff line number Diff line change
@@ -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<JSX.IonInput, JSX.IonInput["value"]>;
17 changes: 17 additions & 0 deletions packages/vue/src/components/IonInputOtp.ts
Original file line number Diff line number Diff line change
@@ -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<JSX.IonInputOtp, JSX.IonInputOtp["value"]>;
17 changes: 17 additions & 0 deletions packages/vue/src/components/IonRange.ts
Original file line number Diff line number Diff line change
@@ -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<JSX.IonRange, JSX.IonRange["value"]>;
20 changes: 20 additions & 0 deletions packages/vue/src/components/IonSearchbar.ts
Original file line number Diff line number Diff line change
@@ -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"]
>;
17 changes: 17 additions & 0 deletions packages/vue/src/components/IonTextarea.ts
Original file line number Diff line number Diff line change
@@ -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<JSX.IonTextarea, JSX.IonTextarea["value"]>;
5 changes: 5 additions & 0 deletions packages/vue/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
87 changes: 87 additions & 0 deletions packages/vue/src/utils/lazy-model.ts
Original file line number Diff line number Diff line change
@@ -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<T extends Component>(
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<string, boolean> | undefined)?.lazy
);

return () => {
const instance = getCurrentInstance();
const hasModelValue =
instance?.vnode.props != null && "modelValue" in instance.vnode.props;
const userCommitHandler = (attrs as Record<string, unknown>)[
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
);
};
},
});
}
121 changes: 121 additions & 0 deletions packages/vue/test/base/tests/unit/v-model-lazy.spec.ts
Original file line number Diff line number Diff line change
@@ -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<typeof mount>[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"
);
});
});