UNPKG

@muban/muban

Version:

Writing components for server-rendered HTML

26 lines (25 loc) 990 B
import { unref, watchEffect } from '@vue/runtime-core'; import { checkInitialBindingState } from '../utils/checkInitialBindingState'; export function textInputBinding(target, model, bindingHelpers) { const updateHtml = () => { let modelValue = unref(model); if (modelValue === null || modelValue === undefined) { modelValue = ''; } target.value = String(modelValue); }; const updateModel = function () { model.value = target.value; }; if (checkInitialBindingState('textInput', target.value, model.value, unref(bindingHelpers.getBinding('initialValueSource'))) === 'binding') { updateModel(); } const unwatch = watchEffect(updateHtml); target.addEventListener('input', updateModel); target.addEventListener('change', updateModel); return () => { unwatch(); target.removeEventListener('input', updateModel); target.removeEventListener('change', updateModel); }; }