Design system.
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

event.ts 1.0 KiB

12345678910111213141516171819202122232425262728293031323334353637
  1. export const delegateTriggerEvent = <T extends HTMLElement>(
  2. eventName: string,
  3. target: T,
  4. value?: unknown,
  5. ) => {
  6. if (typeof window === 'undefined') {
  7. return;
  8. }
  9. const TAG_NAME_ELEMENT_CONSTRUCTOR = {
  10. 'INPUT': window.HTMLInputElement,
  11. 'SELECT': window.HTMLSelectElement,
  12. 'TEXTAREA': window.HTMLTextAreaElement,
  13. } as const;
  14. const {
  15. [target.tagName as keyof typeof TAG_NAME_ELEMENT_CONSTRUCTOR]: elementCtor,
  16. } = TAG_NAME_ELEMENT_CONSTRUCTOR;
  17. if (!elementCtor) {
  18. return;
  19. }
  20. if (eventName === 'change') {
  21. const nativeInputValueSetter = Object.getOwnPropertyDescriptor(elementCtor.prototype, 'value')?.set;
  22. if (nativeInputValueSetter) {
  23. if (
  24. (target.tagName === 'INPUT' && (target as unknown as HTMLInputElement).type !== 'file')
  25. || target.tagName !== 'INPUT'
  26. ) {
  27. nativeInputValueSetter.call(target, value);
  28. }
  29. const simulatedEvent = new Event(eventName, {bubbles: true});
  30. target.dispatchEvent(simulatedEvent);
  31. }
  32. }
  33. };