{"version":3,"file":"use-semi-controlled-input.mjs","names":[],"sources":["../../../src/_common/use-semi-controlled-input.ts"],"sourcesContent":["/*!\n *\n *  Copyright (c) \"Neo4j\"\n *  Neo4j Sweden AB [http://neo4j.com]\n *\n *  This file is part of Neo4j.\n *\n *  Neo4j is free software: you can redistribute it and/or modify\n *  it under the terms of the GNU General Public License as published by\n *  the Free Software Foundation, either version 3 of the License, or\n *  (at your option) any later version.\n *\n *  This program is distributed in the hope that it will be useful,\n *  but WITHOUT ANY WARRANTY; without even the implied warranty of\n *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n *  GNU General Public License for more details.\n *\n *  You should have received a copy of the GNU General Public License\n *  along with this program.  If not, see <http://www.gnu.org/licenses/>.\n */\n\nimport { useCallback, useState } from 'react';\n\nimport { type HtmlAttributes } from './types';\n\ntype UseSemiControlledInputProps<T> = {\n  state: T;\n  onChange?: (event: React.ChangeEvent<HTMLInputElement>) => void;\n  isControlled?: boolean;\n  inputType: HtmlAttributes<'input'>['type'];\n};\n\nexport function useSemiControlledInput<T>({\n  state,\n  onChange,\n  isControlled,\n  inputType = 'text',\n}: UseSemiControlledInputProps<T>): [\n  T,\n  (event: React.ChangeEvent<HTMLInputElement>) => void,\n] {\n  const [internalState, setInternalState] = useState<T>(state);\n\n  const current = isControlled === true ? state : internalState;\n\n  const setStateFromEvent = useCallback(\n    (event: React.ChangeEvent<HTMLInputElement>) => {\n      let resolved: T;\n\n      if (['checkbox', 'radio', 'switch'].includes(inputType)) {\n        resolved = event.target.checked as T;\n      } else {\n        resolved = event.target.value as T;\n      }\n\n      if (isControlled !== true) {\n        setInternalState(resolved);\n      }\n\n      onChange?.(event);\n    },\n    [isControlled, onChange, inputType],\n  );\n\n  return [current, setStateFromEvent];\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAgCA,SAAgB,uBAA0B,EACxC,OACA,UACA,cACA,YAAY,UAIZ;CACA,MAAM,CAAC,eAAe,oBAAoB,SAAY,KAAK;CAuB3D,OAAO,CArBS,iBAAiB,OAAO,QAAQ,eAEtB,aACvB,UAA+C;EAC9C,IAAI;EAEJ,IAAI;GAAC;GAAY;GAAS;EAAQ,CAAC,CAAC,SAAS,SAAS,GACpD,WAAW,MAAM,OAAO;OAExB,WAAW,MAAM,OAAO;EAG1B,IAAI,iBAAiB,MACnB,iBAAiB,QAAQ;EAG3B,aAAA,QAAA,aAAA,KAAA,KAAA,SAAW,KAAK;CAClB,GACA;EAAC;EAAc;EAAU;CAAS,CAGH,CAAC;AACpC"}