From 009b42a3b3eaaad27b1e088657e5cb7715286439 Mon Sep 17 00:00:00 2001 From: "Zhang, Xueqian" Date: Wed, 12 Jun 2024 15:22:32 +0800 Subject: [PATCH] allow some attribute of dom be modified. --- src/proxyObject.ts | 12 +++++++++++- tests/proxyObject.test.ts | 11 +++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/proxyObject.ts b/src/proxyObject.ts index 5c8d1b49..3a11d320 100644 --- a/src/proxyObject.ts +++ b/src/proxyObject.ts @@ -3,7 +3,7 @@ */ export default function proxyObject< Obj extends object, - ExtendObj extends object + ExtendObj extends object, >(obj: Obj, extendProps: ExtendObj): Obj & ExtendObj { if (typeof Proxy !== 'undefined' && obj) { return new Proxy(obj, { @@ -18,6 +18,16 @@ export default function proxyObject< ? originProp.bind(target) : originProp; }, + set(target, prop, v) { + let value = v; + if (typeof prop === 'string' && ['value'].includes(prop)) { + if (typeof value !== 'string') { + value = '' + value; + } + target[prop] = value; + return true; + } + }, }) as Obj & ExtendObj; } diff --git a/tests/proxyObject.test.ts b/tests/proxyObject.test.ts index 6292ef0d..2857492e 100644 --- a/tests/proxyObject.test.ts +++ b/tests/proxyObject.test.ts @@ -20,4 +20,15 @@ describe('proxyObject', () => { expect(proxyA).toBe(null); }); + + it('change', () => { + const div = document.createElement('div'); + div.innerHTML = ''; + const a = div.firstChild as HTMLInputElement; + + const proxyA = proxyObject(a, {}); + proxyA.value = '321'; + + expect(proxyA.value).toBe('321'); + }); });