{"version":3,"file":"use-form-field.mjs","names":[],"sources":["../../../../src/_common/form-control/use-form-field.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 { useId } from 'react';\n\nimport {\n  type FormFieldControlProps,\n  type FormFieldMessageSeverity,\n  type FormFieldProps,\n  type FormFieldState,\n} from './form-control-types';\n\ntype UseFormFieldOptions = FormFieldProps & {\n  /**\n   * Base for the generated ids, so a component that mints sibling ids of its\n   * own (like the Combobox listbox and panel ids) can share one base.\n   * Defaults to a generated id.\n   */\n  baseId?: string;\n  /**\n   * Overrides the generated control id, for controls that already name their\n   * id (the Combobox trigger is `${baseId}-trigger`).\n   */\n  controlId?: string;\n  /**\n   * Ids referenced by `aria-describedby` before the message id, skipping\n   * falsy entries (such as the TextInput clear/loading hint).\n   */\n  extraDescribedByIds?: (string | false | undefined)[];\n};\n\ntype UseFormFieldResult = {\n  field: FormFieldState;\n  /** ARIA attributes to spread onto the control the field describes. */\n  controlProps: FormFieldControlProps;\n};\n\n/**\n * Resolves the shared form field state: id generation, which of help and\n * error is displayed - an error always replaces the help text - and the ARIA\n * wiring of the control. The parts are rendered by `FormFieldLabel` and\n * `FormFieldMessage`.\n */\nexport const useFormField = ({\n  baseId,\n  controlId,\n  label,\n  helpText,\n  errorText,\n  isRequired = false,\n  hasRequiredIndicator = false,\n  hasVisuallyHiddenLabel = false,\n  extraDescribedByIds = [],\n}: UseFormFieldOptions): UseFormFieldResult => {\n  const generatedBaseId = useId();\n  const base = baseId ?? generatedBaseId;\n  const labelId = `${base}-label`;\n  const helpTextId = `${base}-help-text`;\n  const errorTextId = `${base}-error-text`;\n  const resolvedControlId = controlId ?? `${base}-control`;\n\n  const hasError = Boolean(errorText);\n  const hasHelpText = Boolean(helpText) && !hasError;\n  const messageSeverity: FormFieldMessageSeverity | undefined = hasError\n    ? 'error'\n    : hasHelpText\n      ? 'help'\n      : undefined;\n  const message = hasError ? errorText : hasHelpText ? helpText : undefined;\n  const messageId = hasError\n    ? errorTextId\n    : hasHelpText\n      ? helpTextId\n      : undefined;\n\n  const describedByIds = [...extraDescribedByIds, messageId].filter(\n    (id): id is string => typeof id === 'string' && id !== '',\n  );\n\n  return {\n    controlProps: {\n      'aria-describedby':\n        describedByIds.length > 0 ? describedByIds.join(' ') : undefined,\n      'aria-invalid': hasError ? true : undefined,\n    },\n    field: {\n      controlId: resolvedControlId,\n      errorText,\n      errorTextId,\n      hasError,\n      hasRequiredIndicator,\n      hasVisuallyHiddenLabel,\n      helpText,\n      helpTextId,\n      isRequired,\n      label,\n      labelId,\n      message,\n      messageId,\n      messageSeverity,\n    },\n  };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6DA,MAAa,gBAAgB,EAC3B,QACA,WACA,OACA,UACA,WACA,aAAa,OACb,uBAAuB,OACvB,yBAAyB,OACzB,sBAAsB,CAAC,QACsB;CAC7C,MAAM,kBAAkB,MAAM;CAC9B,MAAM,OAAO,WAAA,QAAA,WAAA,KAAA,IAAA,SAAU;CACvB,MAAM,UAAU,GAAG,KAAK;CACxB,MAAM,aAAa,GAAG,KAAK;CAC3B,MAAM,cAAc,GAAG,KAAK;CAC5B,MAAM,oBAAoB,cAAA,QAAA,cAAA,KAAA,IAAA,YAAa,GAAG,KAAK;CAE/C,MAAM,WAAW,QAAQ,SAAS;CAClC,MAAM,cAAc,QAAQ,QAAQ,KAAK,CAAC;CAC1C,MAAM,kBAAwD,WAC1D,UACA,cACE,SACA,KAAA;CACN,MAAM,UAAU,WAAW,YAAY,cAAc,WAAW,KAAA;CAChE,MAAM,YAAY,WACd,cACA,cACE,aACA,KAAA;CAEN,MAAM,iBAAiB,CAAC,GAAG,qBAAqB,SAAS,CAAC,CAAC,QACxD,OAAqB,OAAO,OAAO,YAAY,OAAO,EACzD;CAEA,OAAO;EACL,cAAc;GACZ,oBACE,eAAe,SAAS,IAAI,eAAe,KAAK,GAAG,IAAI,KAAA;GACzD,gBAAgB,WAAW,OAAO,KAAA;EACpC;EACA,OAAO;GACL,WAAW;GACX;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;EACF;CACF;AACF"}