{"version":3,"file":"use-semi-controlled-state.mjs","names":[],"sources":["../../../src/_common/use-semi-controlled-state.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\ntype SetStateAction<T> = T | ((prev: T) => T);\n\ntype UseSemiControlledStateProps<T> = {\n  state: T;\n  onChange?: (next: T) => void;\n  isControlled?: boolean;\n};\n\nexport function useSemicontrolledState<T>({\n  state,\n  onChange,\n  isControlled,\n}: UseSemiControlledStateProps<T>): [\n  T,\n  (setState: SetStateAction<T>) => void,\n  (state: T) => void,\n] {\n  const [internal, setInternal] = useState<T>(state);\n\n  const current = isControlled === true ? state : internal;\n\n  const setState = useCallback(\n    (next: SetStateAction<T>) => {\n      const resolved =\n        typeof next === 'function' ? (next as (prev: T) => T)(current) : next;\n\n      if (isControlled !== true) {\n        setInternal(resolved);\n      }\n      onChange?.(resolved);\n    },\n    [isControlled, current, onChange],\n  );\n\n  const resetInternalState = useCallback(\n    (nextState: T) => {\n      if (isControlled !== true) {\n        setInternal(nextState);\n      }\n    },\n    [isControlled],\n  );\n\n  return [current, setState, resetInternalState];\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AA+BA,SAAgB,uBAA0B,EACxC,OACA,UACA,gBAKA;CACA,MAAM,CAAC,UAAU,eAAe,SAAY,KAAK;CAEjD,MAAM,UAAU,iBAAiB,OAAO,QAAQ;CAwBhD,OAAO;EAAC;EAtBS,aACd,SAA4B;GAC3B,MAAM,WACJ,OAAO,SAAS,aAAc,KAAwB,OAAO,IAAI;GAEnE,IAAI,iBAAiB,MACnB,YAAY,QAAQ;GAEtB,aAAA,QAAA,aAAA,KAAA,KAAA,SAAW,QAAQ;EACrB,GACA;GAAC;GAAc;GAAS;EAAQ,CAYV;EATG,aACxB,cAAiB;GAChB,IAAI,iBAAiB,MACnB,YAAY,SAAS;EAEzB,GACA,CAAC,YAAY,CAG6B;CAAC;AAC/C"}