package.react-utils.useFieldSubmit.useFieldSubmit.js.map Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of neeto-commons-frontend Show documentation
Show all versions of neeto-commons-frontend Show documentation
A package encapsulating common code across neeto projects including initializers, utility functions, common components and hooks and so on.
{"version":3,"file":"useFieldSubmit.js","names":["useCallback","useEffect","useRef","useFieldSubmit","onSubmit","ref","keyDownHandler","event","key","shiftKey","stopPropagation","preventDefault","element","current","undefined","addEventListener","removeEventListener"],"sources":["../../../src/react-utils/useFieldSubmit/useFieldSubmit.js"],"sourcesContent":["import { useCallback, useEffect, useRef } from \"react\";\n\nconst useFieldSubmit = onSubmit => {\n /** @type {{ current: HTMLInputElement & HTMLTextAreaElement }} */\n // @ts-ignore\n const ref = useRef(null);\n\n const keyDownHandler = useCallback(event => {\n if (event.key !== \"Enter\") return;\n\n if (event.shiftKey) {\n event.stopPropagation();\n } else {\n onSubmit();\n event.preventDefault();\n }\n }, []);\n\n useEffect(() => {\n const element = ref.current;\n if (!element) return undefined;\n\n element.addEventListener(\"keydown\", keyDownHandler);\n\n return () => element.removeEventListener(\"keydown\", keyDownHandler);\n }, []);\n\n return ref;\n};\n\nexport default useFieldSubmit;\n"],"mappings":"AAAA,SAASA,WAAW,EAAEC,SAAS,EAAEC,MAAM,QAAQ,OAAO;AAEtD,IAAMC,cAAc,GAAG,SAAjBA,cAAcA,CAAGC,QAAQ,EAAI;EACjC;EACA;EACA,IAAMC,GAAG,GAAGH,MAAM,CAAC,IAAI,CAAC;EAExB,IAAMI,cAAc,GAAGN,WAAW,CAAC,UAAAO,KAAK,EAAI;IAC1C,IAAIA,KAAK,CAACC,GAAG,KAAK,OAAO,EAAE;IAE3B,IAAID,KAAK,CAACE,QAAQ,EAAE;MAClBF,KAAK,CAACG,eAAe,CAAC,CAAC;IACzB,CAAC,MAAM;MACLN,QAAQ,CAAC,CAAC;MACVG,KAAK,CAACI,cAAc,CAAC,CAAC;IACxB;EACF,CAAC,EAAE,EAAE,CAAC;EAENV,SAAS,CAAC,YAAM;IACd,IAAMW,OAAO,GAAGP,GAAG,CAACQ,OAAO;IAC3B,IAAI,CAACD,OAAO,EAAE,OAAOE,SAAS;IAE9BF,OAAO,CAACG,gBAAgB,CAAC,SAAS,EAAET,cAAc,CAAC;IAEnD,OAAO;MAAA,OAAMM,OAAO,CAACI,mBAAmB,CAAC,SAAS,EAAEV,cAAc,CAAC;IAAA;EACrE,CAAC,EAAE,EAAE,CAAC;EAEN,OAAOD,GAAG;AACZ,CAAC;AAED,eAAeF,cAAc","ignoreList":[]}