-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathreact-input-handler.esm.js.map
1 lines (1 loc) · 3.53 KB
/
react-input-handler.esm.js.map
1
{"version":3,"file":"react-input-handler.esm.js","sources":["../src/ReactInputHandler.js"],"sourcesContent":["'use strict'\n\nimport set from 'lodash.set'\n\n/**\n * Export React Input Handler.\n */\n\nexport default handler\n\n/**\n * The React Input Handler function.\n * @param {Object} event The event.\n * @param {Function} callback The callback function.\n */\nfunction handler(event, callback) {\n\n if (!event) {\n throw new Error('event must be defined')\n }\n\n if (typeof event !== 'object') {\n throw new Error('event must be an object')\n }\n\n if (typeof this.setState !== 'function') {\n throw new Error('react-input-handler must be bound to the component instance')\n }\n\n if (typeof callback !== 'undefined' && typeof callback !== 'function') {\n throw new Error('the 2nd argument of react-input-handler must be a function')\n }\n\n const target = event.target\n const name = target.name\n\n if (!name) {\n throw new Error('all input must have a name prop')\n }\n\n // If the name prop ends with `[]` then the prop is an array.\n const usingArrayNotation = name.substr(-2) === '[]'\n\n if (usingArrayNotation) {\n const arrayNotationName = name.substr(0, name.length - 2)\n const adding = getValue(target)\n const array = this.state[arrayNotationName] || []\n const value = target.value\n\n // Add the target value to the array.\n if (adding) {\n\n // Add the new value to the array and persist into the state.\n if (array.indexOf(value) === -1) {\n this.setState((prevState) => ({\n [arrayNotationName]: array.concat(value)\n }))\n }\n } else {\n\n // Remove the target value from the array.\n const indexToRemove = array.indexOf(value)\n if (indexToRemove > -1) {\n array.splice(indexToRemove, 1)\n }\n\n // Persist the changed array into the state.\n this.setState(prevState => (\n set(prevState, arrayNotationName, array)\n ), callback)\n }\n } else {\n\n // Modify the state.\n this.setState(prevState => (\n set(prevState, name, getValue(target))\n ), callback)\n }\n}\n\n/**\n * Utility functions.\n */\n\n/**\n * Return the value of a DOM element.\n * @param {Object} element The element.\n * @param {Boolean|String} The DOM element value.\n */\nfunction getValue(element) {\n switch(element.type) {\n case 'checkbox':\n return element.checked\n case 'select-multiple':\n return [ ...element.querySelectorAll(':checked') ]\n .map((checked) => checked.value)\n default:\n return element.value\n }\n}\n"],"names":["handler","event","callback","Error","this","setState","target","name","substr","arrayNotationName","length","adding","getValue","array","state","value","indexOf","prevState","concat","indexToRemove","splice","set","element","type","checked","querySelectorAll","map"],"mappings":"igBAeA,SAASA,QAAQC,EAAOC,OAEjBD,QACG,IAAIE,MAAM,4BAGG,qBAAVF,sBAAAA,UACH,IAAIE,MAAM,8BAGW,mBAAlBC,KAAKC,eACR,IAAIF,MAAM,uEAGM,IAAbD,GAAgD,mBAAbA,QACtC,IAAIC,MAAM,kEAGZG,EAASL,EAAMK,OACfC,EAAOD,EAAOC,SAEfA,QACG,IAAIJ,MAAM,sCAI6B,OAApBI,EAAKC,QAAQ,GAEhB,KAChBC,EAAoBF,EAAKC,OAAO,EAAGD,EAAKG,OAAS,GACjDC,EAASC,SAASN,GAClBO,EAAQT,KAAKU,MAAML,OACnBM,EAAQT,EAAOS,SAGjBJ,GAG4B,IAA1BE,EAAMG,QAAQD,SACXV,SAAS,SAACY,4BACZR,EAAoBI,EAAMK,OAAOH,UAGjC,KAGCI,EAAgBN,EAAMG,QAAQD,GAChCI,GAAiB,KACbC,OAAOD,EAAe,QAIzBd,SAAS,mBACZgB,IAAIJ,EAAWR,EAAmBI,IACjCX,cAKAG,SAAS,mBACZgB,IAAIJ,EAAWV,EAAMK,SAASN,KAC7BJ,GAaP,SAASU,SAASU,UACTA,EAAQC,UACR,kBACID,EAAQE,YACZ,oDACSF,EAAQG,iBAAiB,cAClCC,IAAI,SAACF,UAAYA,EAAQT,uBAErBO,EAAQP"}