{"version":3,"file":"useControlledHostValueProps.mjs","names":[],"sources":["../../../../../../../src/lib/remote/useControlledHostValueProps.ts"],"sourcesContent":["import { useLayoutEffect, useState } from \"react\";\nimport { controlledRemoteValueMarker } from \"./useControlledRemoteValueProps\";\nimport type { FieldProps } from \"@/lib/remote/types\";\n\n/**\n * Returns props with controlled value handling for remote components. It works\n * by omitting values resulting from a remotely executed event handler. These\n * values are marked by the `controlledRemoteValueMarker`.\n *\n * This hook is only necessary for text inputs. If not used the controlled input\n * value may be corrupted by interleaved host inputs and remote events.\n *\n * @param emptyValue What the field reports while nothing has been entered —\n *   `\"\"` for a text input, `NaN` for a number, `null` for a date range. It is\n *   the sentinel that keeps the field controlled from the first render; see\n *   that convention in the package's AGENTS.md.\n */\nexport const useControlledHostValueProps = <T, P>(\n  props: FieldProps<T, P>,\n  emptyValue: T,\n) => {\n  const {\n    value: valueFromProps,\n    onChange: onChangeFromProps,\n    defaultValue,\n  } = props;\n\n  const regularValue =\n    valueFromProps === controlledRemoteValueMarker ? undefined : valueFromProps;\n\n  /*\n   * Only `undefined` falls back, because only `undefined` reads as\n   * uncontrolled: a caller-supplied `null` controls a `DateRangePicker` with no\n   * range selected, so `??` would swallow it. The marker cannot appear on the\n   * first render, so `regularValue` is the caller's own value here.\n   */\n  const [value, setValue] = useState(\n    regularValue !== undefined\n      ? regularValue\n      : defaultValue !== undefined\n        ? defaultValue\n        : emptyValue,\n  );\n\n  useLayoutEffect(() => {\n    if (regularValue !== undefined) {\n      setValue(regularValue);\n    }\n  }, [valueFromProps]);\n\n  // eslint-disable-next-line @typescript-eslint/no-explicit-any\n  const onChange = (v: any) => {\n    setValue(v);\n    onChangeFromProps?.(v);\n  };\n\n  return {\n    ...props,\n    value,\n    onChange,\n  };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAiBA,IAAa,+BACX,OACA,eACG;CACH,MAAM,EACJ,OAAO,gBACP,UAAU,mBACV,iBACE;CAEJ,MAAM,eACJ,mBAAA,oCAAiD,KAAA,IAAY;CAQ/D,MAAM,CAAC,OAAO,YAAY,SACxB,iBAAiB,KAAA,IACb,eACA,iBAAiB,KAAA,IACf,eACA,UACR;CAEA,sBAAsB;EACpB,IAAI,iBAAiB,KAAA,GACnB,SAAS,YAAY;CAEzB,GAAG,CAAC,cAAc,CAAC;CAGnB,MAAM,YAAY,MAAW;EAC3B,SAAS,CAAC;EACV,oBAAoB,CAAC;CACvB;CAEA,OAAO;EACL,GAAG;EACH;EACA;CACF;AACF"}