Browse Source

Fix event delegation

Event delegation only occurs on client-side, check if window exists to
work with server-side (noop).
pull/1/head
TheoryOfNekomata 1 year ago
parent
commit
85100e724e
3 changed files with 12 additions and 6 deletions
  1. +1
    -0
      packages/web/kitchen-sink/react-next/src/pages/categories/blob/index.tsx
  2. +1
    -0
      packages/web/kitchen-sink/react-next/src/pages/categories/number/index.tsx
  3. +10
    -6
      packages/web/kitchen-sink/react-next/src/utils/event.ts

+ 1
- 0
packages/web/kitchen-sink/react-next/src/pages/categories/blob/index.tsx View File

@@ -974,6 +974,7 @@ const BlobPage: NextPage = () => {
label="Primary Image"
hint="Select any files here"
block
onChange={(e) => console.log(e.currentTarget.files)}
/>
)
}


+ 1
- 0
packages/web/kitchen-sink/react-next/src/pages/categories/number/index.tsx View File

@@ -210,6 +210,7 @@ const NumberPage: NextPage = () => {
max={100}
tickMarks={[{ label: 'low', value: 25, }, 50]}
enhanced
onChange={(e) => console.log(e.currentTarget.value)}
/>
</div>
</section>


+ 10
- 6
packages/web/kitchen-sink/react-next/src/utils/event.ts View File

@@ -1,10 +1,14 @@
const TAG_NAME_ELEMENT_CONSTRUCTOR = {
'INPUT': window.HTMLInputElement,
'SELECT': window.HTMLSelectElement,
'TEXTAREA': window.HTMLTextAreaElement,
} as const;

export const delegateTriggerChangeEvent = <T extends HTMLElement>(target: T, value?: unknown) => {
if (typeof window === 'undefined') {
return;
}

const TAG_NAME_ELEMENT_CONSTRUCTOR = {
'INPUT': window.HTMLInputElement,
'SELECT': window.HTMLSelectElement,
'TEXTAREA': window.HTMLTextAreaElement,
} as const;

const { [target.tagName as keyof typeof TAG_NAME_ELEMENT_CONSTRUCTOR]: elementCtor } = TAG_NAME_ELEMENT_CONSTRUCTOR;

if (!elementCtor) {


Loading…
Cancel
Save