import {computed, reactive, useModel} from "@peryl/react-compose"; export function createInputPopperAttrs( { getValue, emitChange, isValidInputValue, emit, popper, disableHideOnBlur, handleInputChange, }: { getValue: () => T, emitChange: (val: T) => void, isValidInputValue?: (val: any) => boolean, emit: { onFocus: (e: FocusEvent) => void, onBlur: (e: FocusEvent) => void, }, popper: { methods: { show: () => void, hide: () => void, toggle: () => void, }, }, disableHideOnBlur?: boolean, handleInputChange?: (val: string | null | undefined) => void, }) { const model = useModel(getValue, emitChange, { onChange: (val: any) => {state.inputValue = val;} }); const state = reactive({ inputValue: model.value, }); const handler = { onInputChange: (val: any) => { state.inputValue = val; if (!isValidInputValue) { model.value = val; } else { if (isValidInputValue(val)) { model.value = val; } } handleInputChange?.(val); }, onInputFocus: (e: FocusEvent) => {emit.onFocus(e);}, onInputBlur: (e: FocusEvent) => { state.inputValue = model.value as any; emit.onBlur(e); !disableHideOnBlur && popper.methods.hide(); }, onInputEnter: () => {popper.methods.toggle;}, onInputEsc: () => popper.methods.hide(), }; const inputAttrs = computed(() => ({ modelValue: state.inputValue, onChange: handler.onInputChange, onEnter: handler.onInputEnter, onBlur: handler.onInputBlur, onFocus: handler.onInputFocus, onEsc: handler.onInputEsc, })); return { inputAttrs, model, state }; }