Match-id-ff7da73b1c97697d74f286c293358c5bf2790678

This commit is contained in:
* 2022-08-26 11:31:28 +08:00 committed by *
commit 0759f07766
2 changed files with 24 additions and 1 deletions

View File

@ -16,7 +16,7 @@ const noop = (): void => {};
// 兼容IE浏览器无法修改Event属性 // 兼容IE浏览器无法修改Event属性
export class WrappedEvent { export class WrappedEvent {
customEventName: string; customEventName: string;
nativeEvent: Event; nativeEvent: AnyNativeEvent;
nativeEventType: string; nativeEventType: string;
type: string; type: string;
key: string; key: string;
@ -25,12 +25,18 @@ export class WrappedEvent {
stopPropagation: () => void; stopPropagation: () => void;
preventDefault: () => void; preventDefault: () => void;
// 适配Keyboard键盘事件该函数不能由合成事件调用
getModifierState?: (keyArgs: string) => boolean;
// 适配老版本事件api // 适配老版本事件api
persist = noop; persist = noop;
constructor(customEventName: string, nativeEvtName: string, nativeEvent: AnyNativeEvent) { constructor(customEventName: string, nativeEvtName: string, nativeEvent: AnyNativeEvent) {
for (const name in nativeEvent) { for (const name in nativeEvent) {
this[name] = nativeEvent[name]; this[name] = nativeEvent[name];
if(name === 'getModifierState') {
const keyBoardEvent = nativeEvent as KeyboardEvent;
this.getModifierState = (keyArg) => keyBoardEvent.getModifierState(keyArg);
}
} }
// stopPropagation和preventDefault 必须通过Event实例调用 // stopPropagation和preventDefault 必须通过Event实例调用
this.stopPropagation = () => nativeEvent.stopPropagation(); this.stopPropagation = () => nativeEvent.stopPropagation();

View File

@ -146,5 +146,22 @@ describe('MouseEvent Test', () => {
}; };
test('mouseover', <div onMouseOver={onMouseOver} onMouseOverCapture={onMouseOverCapture} />); test('mouseover', <div onMouseOver={onMouseOver} onMouseOverCapture={onMouseOverCapture} />);
}); });
it('KeyboardEvent.getModifierState should not fail', () => {
const input = Horizon.render(<input
onMouseDown={(e) => {
e.getModifierState('CapsLock');
}}
/>, container);
const event = new MouseEvent('mousedown', {
relatedTarget: null,
bubbles: true,
screenX: 1,
});
expect(() => {
input.dispatchEvent(event);
}).not.toThrow();
});
}); });
}); });