import type { EventListenerFunction } from "../types/dom"; import type { RMLTemplateExpression } from "../types/internal"; import type { Source } from '../types/source'; import { autoValue } from '../utils/auto-value'; export type ObjectKey = string | number; export type TargetObject = object | Array; // Record; export type ObjectSourceExpression = [key: keyof T, target: T]; /** * @deprecated don't use **/ export const isObjectSource = (expression: RMLTemplateExpression): expression is ObjectSourceExpression => Array.isArray(expression) && expression.length == 2 ; /** * A data source that updates an object's property from an <input> element when * a certain event occurs * @param key an ['property', object] or [index, array] pair to update * @returns A data source * @example * @example * @example */ export const ObjectSource = (key: keyof T, targetObject?: T) => { const handler = ((targetObject: T, e: E) => { targetObject[key] = autoValue(e.target as HTMLInputElement) as T[keyof T]; }); return (targetObject ? handler.bind(null, targetObject) as EventListenerFunction : (t2: T)=>(handler.bind(null, t2) as EventListenerFunction) ); } ; /** * An Event Adapter that uses an event's underlying <input> element * to updates an object's property or an array item. * @param property A property to update in the given object or an index to update in the given array * @param object The object or array to update * @returns An event handler */ export const Update = (property: keyof T, object?: T): ((t2: T) => EventListenerFunction) | EventListenerFunction | Source => ObjectSource(property, object) ; export const asObjectSource = ObjectSource; export const AsObjectSource = ObjectSource;