|
| 1 | +import { camelToDashCase } from './case'; |
| 2 | + |
| 3 | +export const attachProps = (node: HTMLElement, newProps: any, oldProps: any = {}) => { |
| 4 | + // some test frameworks don't render DOM elements, so we test here to make sure we are dealing with DOM first |
| 5 | + if (node instanceof Element) { |
| 6 | + // add any classes in className to the class list |
| 7 | + const className = getClassName(node.classList, newProps, oldProps); |
| 8 | + if (className !== '') { |
| 9 | + node.className = className; |
| 10 | + } |
| 11 | + |
| 12 | + Object.keys(newProps).forEach((name) => { |
| 13 | + if ( |
| 14 | + name === 'children' || |
| 15 | + name === 'style' || |
| 16 | + name === 'ref' || |
| 17 | + name === 'class' || |
| 18 | + name === 'className' || |
| 19 | + name === 'forwardedRef' |
| 20 | + ) { |
| 21 | + return; |
| 22 | + } |
| 23 | + if (name.indexOf('on') === 0 && name[2] === name[2].toUpperCase()) { |
| 24 | + const eventName = name.substring(2); |
| 25 | + const eventNameLc = eventName[0].toLowerCase() + eventName.substring(1); |
| 26 | + |
| 27 | + if (!isCoveredByReact(eventNameLc)) { |
| 28 | + syncEvent(node, eventNameLc, newProps[name]); |
| 29 | + } |
| 30 | + } else { |
| 31 | + (node as any)[name] = newProps[name]; |
| 32 | + const propType = typeof newProps[name]; |
| 33 | + if (propType === 'string') { |
| 34 | + node.setAttribute(camelToDashCase(name), newProps[name]); |
| 35 | + } |
| 36 | + } |
| 37 | + }); |
| 38 | + } |
| 39 | +}; |
| 40 | + |
| 41 | +export const getClassName = (classList: DOMTokenList, newProps: any, oldProps: any) => { |
| 42 | + const newClassProp: string = newProps.className || newProps.class; |
| 43 | + const oldClassProp: string = oldProps.className || oldProps.class; |
| 44 | + // map the classes to Maps for performance |
| 45 | + const currentClasses = arrayToMap(classList); |
| 46 | + const incomingPropClasses = arrayToMap(newClassProp ? newClassProp.split(' ') : []); |
| 47 | + const oldPropClasses = arrayToMap(oldClassProp ? oldClassProp.split(' ') : []); |
| 48 | + const finalClassNames: string[] = []; |
| 49 | + // loop through each of the current classes on the component |
| 50 | + // to see if it should be a part of the classNames added |
| 51 | + currentClasses.forEach((currentClass) => { |
| 52 | + if (incomingPropClasses.has(currentClass)) { |
| 53 | + // add it as its already included in classnames coming in from newProps |
| 54 | + finalClassNames.push(currentClass); |
| 55 | + incomingPropClasses.delete(currentClass); |
| 56 | + } else if (!oldPropClasses.has(currentClass)) { |
| 57 | + // add it as it has NOT been removed by user |
| 58 | + finalClassNames.push(currentClass); |
| 59 | + } |
| 60 | + }); |
| 61 | + incomingPropClasses.forEach((s) => finalClassNames.push(s)); |
| 62 | + return finalClassNames.join(' '); |
| 63 | +}; |
| 64 | + |
| 65 | +/** |
| 66 | + * Transforms a React event name to a browser event name. |
| 67 | + */ |
| 68 | +export const transformReactEventName = (eventNameSuffix: string) => { |
| 69 | + switch (eventNameSuffix) { |
| 70 | + case 'doubleclick': |
| 71 | + return 'dblclick'; |
| 72 | + } |
| 73 | + return eventNameSuffix; |
| 74 | +}; |
| 75 | + |
| 76 | +/** |
| 77 | + * Checks if an event is supported in the current execution environment. |
| 78 | + * @license Modernizr 3.0.0pre (Custom Build) | MIT |
| 79 | + */ |
| 80 | +export const isCoveredByReact = (eventNameSuffix: string) => { |
| 81 | + if (typeof document === 'undefined') { |
| 82 | + return true; |
| 83 | + } else { |
| 84 | + const eventName = 'on' + transformReactEventName(eventNameSuffix); |
| 85 | + let isSupported = eventName in document; |
| 86 | + |
| 87 | + if (!isSupported) { |
| 88 | + const element = document.createElement('div'); |
| 89 | + element.setAttribute(eventName, 'return;'); |
| 90 | + isSupported = typeof (element as any)[eventName] === 'function'; |
| 91 | + } |
| 92 | + |
| 93 | + return isSupported; |
| 94 | + } |
| 95 | +}; |
| 96 | + |
| 97 | +export const syncEvent = ( |
| 98 | + node: Element & { __events?: { [key: string]: ((e: Event) => any) | undefined } }, |
| 99 | + eventName: string, |
| 100 | + newEventHandler?: (e: Event) => any |
| 101 | +) => { |
| 102 | + const eventStore = node.__events || (node.__events = {}); |
| 103 | + const oldEventHandler = eventStore[eventName]; |
| 104 | + |
| 105 | + // Remove old listener so they don't double up. |
| 106 | + if (oldEventHandler) { |
| 107 | + node.removeEventListener(eventName, oldEventHandler); |
| 108 | + } |
| 109 | + |
| 110 | + // Bind new listener. |
| 111 | + node.addEventListener( |
| 112 | + eventName, |
| 113 | + (eventStore[eventName] = function handler(e: Event) { |
| 114 | + if (newEventHandler) { |
| 115 | + newEventHandler.call(this, e); |
| 116 | + } |
| 117 | + }) |
| 118 | + ); |
| 119 | +}; |
| 120 | + |
| 121 | +const arrayToMap = (arr: string[] | DOMTokenList) => { |
| 122 | + const map = new Map<string, string>(); |
| 123 | + (arr as string[]).forEach((s: string) => map.set(s, s)); |
| 124 | + return map; |
| 125 | +}; |
0 commit comments