import { ID } from "../../types/index.mjs"; import { RegistryContext, RegistryOptions, RegistryTicket, RegistryTicketInput } from "../createRegistry/index.mjs"; import { ComputedRef, MaybeRefOrGetter, Reactive, Ref } from "vue"; //#region src/composables/createModel/index.d.ts /** * Input type for model tickets - what users provide to register(). * * @template V The type of the ticket value. */ interface ModelTicketInput extends RegistryTicketInput { /** Disabled state of the ticket (optional on input, defaults to false) */ disabled?: MaybeRefOrGetter; } /** * Output type for model tickets - what users receive from get(). * * @template Z The input ticket type that extends ModelTicketInput. */ type ModelTicket = RegistryTicket & Z & { /** Disabled state of the ticket (guaranteed to exist on output) */ disabled: MaybeRefOrGetter; /** Whether the ticket is currently selected */ isSelected: Readonly>; }; /** * Context returned by createModel. * * @template Z The input ticket type (what users provide to register). * @template E The output ticket type (what users receive from get). Defaults to ModelTicket. */ interface ModelContext = ModelTicket> extends Omit, 'register'> { /** * Set of currently selected ticket IDs * * @remarks Reactive via `shallowReactive(new Set())`. Mutating this Set directly is supported — * `selectedItems` and `selectedValues` will update accordingly. Ghost IDs (IDs not in the registry) * are silently filtered out by the computed properties. * * @example * ```ts * const model = createModel() * model.register({ id: 'a', value: 'Apple' }) * console.log(model.selectedIds.has('a')) // true (enrolled on register) * ``` */ selectedIds: Reactive>; /** * Computed Set of selected ticket instances * * @remarks Derived from `selectedIds`. Maps each ID to its ticket via `registry.get()` and * filters out any IDs that no longer exist in the registry. * * @example * ```ts * const model = createModel() * model.register({ id: 'a', value: 'Apple' }) * model.select('a') * const items = Array.from(model.selectedItems.value) * console.log(items[0].id) // 'a' * ``` */ selectedItems: ComputedRef>; /** * Computed Set of selected ticket values * * @remarks Derived from `selectedItems`. Unwraps ref values via `toValue()` — if a ticket's * value is `ref('Apple')`, the Set contains `'Apple'`, not the Ref wrapper. * * @example * ```ts * import { shallowRef } from 'vue' * import { createModel } from '@vuetify/v0' * * const model = createModel() * model.register({ id: 'a', value: shallowRef('Apple') }) * model.select('a') * console.log([...model.selectedValues.value]) // ['Apple'] * ``` */ selectedValues: ComputedRef ? U : E['value']>>; /** * Disabled state for the entire model instance * * @remarks When truthy, all selection operations (`select`, `unselect`, `toggle`) are silently * skipped. Accepts a static boolean, a ref, or a getter for reactive disabled state. * * @see https://0.vuetifyjs.com/composables/selection/create-model */ disabled: MaybeRefOrGetter; /** * Reset selection state without destroying the registry * * @remarks Clears `selectedIds` only — registered tickets are preserved. * Use `clear()` to destroy both selection state and the registry. * * @example * ```ts * const model = createModel() * model.register({ id: 'a', value: 'Apple' }) * model.select('a') * model.reset() * console.log(model.size) // 1 (tickets preserved) * console.log(model.selectedIds.size) // 0 * ``` */ reset: () => void; /** * Select a ticket by ID * * @param id The ID of the ticket to select. * @remarks When `multiple` is `false` (default), clears `selectedIds` before adding the new ID. * When `multiple` is `true`, accumulates without clearing. * No-op if the model instance is disabled, the ticket doesn't exist, or the ticket is disabled. * * @see https://0.vuetifyjs.com/composables/selection/create-model * * @example * ```ts * const model = createModel() * model.register({ id: 'a', value: 'Apple' }) * model.register({ id: 'b', value: 'Banana' }) * model.select('a') * model.select('b') // clears 'a', selects 'b' * console.log(model.selectedIds.size) // 1 * ``` */ select: (id: ID) => void; /** * Unselect a ticket by ID * * @param id The ID of the ticket to unselect. * @remarks Removes the ID from `selectedIds`. * No-op if the model instance is disabled, the ticket doesn't exist, or the ticket is disabled. * Wholesale operations (`apply`, `reset`, `clear`) bypass this guard so disabled ids can always drain. * * @example * ```ts * model.select('a') * model.unselect('a') * console.log(model.selectedIds.size) // 0 * ``` */ unselect: (id: ID) => void; /** * Toggle a ticket's selection state * * @param id The ID of the ticket to toggle. * @remarks Calls `select(id)` if not selected, `unselect(id)` if already selected. * No-op if the model instance is disabled or the ticket is disabled. * * @example * ```ts * model.toggle('a') // selects * model.toggle('a') // unselects * ``` */ toggle: (id: ID) => void; /** * Check if a ticket is currently selected * * @param id The ID of the ticket to check. * @returns `true` if the ID is in `selectedIds`. * * @example * ```ts * model.select('a') * console.log(model.selected('a')) // true * console.log(model.selected('b')) // false * ``` */ selected: (id: ID) => boolean; /** * Apply external values to the model * * @param values Array of values to apply. Only `values[0]` is used (single-value store). * @param options Optional. Accepted for interface compatibility with `createSelection`; ignored at this layer. * @remarks Used internally by `useProxyModel` to sync a ref with the model. Two paths: * 1. **Ref write**: Writes `values[0]` to any selected ticket whose value is a ref. * The write is the application — the selection is kept and browse is skipped * (the catalog indexes the ref object, not the unwrapped value). An `undefined` * value leaves the ref untouched. * 2. **Browse resolution**: Otherwise clears `selectedIds`, resolves `values[0]` * via `registry.browse()`, and selects the match. * * @see https://0.vuetifyjs.com/composables/selection/create-model * * @example * ```ts * import { shallowRef } from 'vue' * import { createModel } from '@vuetify/v0' * * const value = shallowRef('Apple') * const model = createModel() * model.register({ id: 'fruit', value }) // enrolled automatically * * model.apply(['Banana']) // value.value is now 'Banana' * ``` */ apply: (values: unknown[], options?: { multiple?: boolean; }) => void; /** * Register a new ticket * * @param registration Partial ticket data. ID is auto-generated if not provided. Disabled defaults to `false`. * @returns The registered ticket with `isSelected` computed ref attached. * @remarks Delegates to `registry.register()` after adding model-specific fields (`disabled`, `isSelected`). * * @see https://0.vuetifyjs.com/composables/selection/create-model * * @example * ```ts * import { shallowRef } from 'vue' * import { createModel } from '@vuetify/v0' * * const model = createModel() * const ticket = model.register({ id: 'fruit', value: shallowRef('Apple') }) * * console.log(ticket.id) // 'fruit' * console.log(ticket.isSelected.value) // true (enrolled by default) * console.log(ticket.disabled) // false * ``` */ register: (registration?: Partial) => E; /** * Onboard multiple tickets at once * * @param registrations An array of partial ticket data to register. * @returns The registered tickets with model-specific fields attached. * @remarks Delegates to the model's `register` (not the registry's) so each ticket * receives `disabled` and `isSelected`. Runs inside `registry.batch()` for performance. */ onboard: (registrations: Partial[]) => E[]; } interface ModelOptions extends RegistryOptions { /** * Disabled state for the entire model instance * * @default false * @remarks When truthy, all selection operations are silently skipped. Accepts a static * boolean, a ref, or a getter for reactive disabled state. * * @example * ```ts * import { createModel } from '@vuetify/v0' * * // Static * const model = createModel({ disabled: true }) * * // Reactive * const disabled = ref(false) * const model = createModel({ disabled }) * ``` */ disabled?: MaybeRefOrGetter; /** * Auto-select tickets on registration * * @default true * @remarks When truthy, newly registered tickets are automatically selected via `select()`, * subject to disabled guards (both instance-level and ticket-level). With single-value * semantics, only the most recently registered ticket remains active. * * `createSelection` overrides this to `false` and handles enrollment with its own * `multiple`-aware logic. */ enroll?: MaybeRefOrGetter; /** * Allow multiple tickets to be selected simultaneously * * @default false * @remarks When `true`, `select()` accumulates IDs instead of clearing before adding. * Used by createSlider where all thumbs must stay selected. */ multiple?: MaybeRefOrGetter; } /** * Creates a new model instance for storing a single value. * * @param options The options for the model instance. * @template Z The input ticket type. Extend {@link ModelTicketInput} to add custom properties. * @template E The output ticket type. Automatically includes `disabled` and `isSelected`. * @template R The context type. Defaults to {@link ModelContext}. * @returns A new model instance with selection tracking and disabled guards. * * @remarks * Extends createRegistry with value tracking via a reactive Set of selected IDs. * Provides the shared model-value concept used by both Selection and Slider. * * **Enrollment**: Tickets are auto-selected on registration by default (`enroll: true`). * With single-value semantics, only the most recently registered ticket is active. * Pass `enroll: false` to opt out. `createSelection` overrides this to `false`. * * **Apply bridge**: `useProxyModel` calls `apply()` to sync a ref with the model. * When the active ticket's value is a ref, `apply` writes to it directly. Otherwise, * it resolves via `registry.browse()`. * * @see https://0.vuetifyjs.com/composables/selection/create-model * * @example * ```ts * import { shallowRef } from 'vue' * import { createModel, useProxyModel } from '@vuetify/v0' * * const value = shallowRef('Apple') * const model = createModel({ events: true }) * * model.register({ id: 'fruit', value }) * useProxyModel(model, value) * * // value and model stay in sync bidirectionally * ``` * * @example * ```ts * // Disabled model — all selection operations are no-ops * const model = createModel({ disabled: true }) * model.register({ id: 'a', value: shallowRef('Apple') }) * model.select('a') // no-op * ``` */ declare function createModel = ModelTicket, R extends ModelContext = ModelContext>(_options?: ModelOptions): R; //#endregion export { ModelContext, ModelOptions, ModelTicket, ModelTicketInput, createModel };