{"version":3,"file":"Field-B5pzw-hV.cjs","sources":["../src/components/Field/Field.tsx"],"sourcesContent":["'use client';\nimport { Children, cloneElement, isValidElement, useId } from 'react';\nimport type { HTMLAttributes, ReactElement, ReactNode } from 'react';\nimport { cx } from '../../utils/cx';\nimport { withSx } from '../../utils/withSx';\nimport type { SxProp } from '../Layout/sx';\n\nexport type FieldValidationState = 'default' | 'danger' | 'warning' | 'success';\n\nexport interface FieldProps extends Omit<HTMLAttributes<HTMLDivElement>, 'children'> {\n  /** Label text. */\n  label?: ReactNode;\n  /** Helper copy shown under the control. */\n  hint?: ReactNode;\n  /** Error message — overrides `hint` and applies danger styling to the control. */\n  error?: ReactNode;\n  /** Show a red asterisk next to the label. */\n  required?: boolean;\n  /** Show a muted \"(optional)\" tag next to the label. */\n  optional?: boolean;\n  /** Character counter — pass the current length. Pair with `maxLength`. */\n  counter?: number;\n  /** Max characters; when `counter` >= this, the counter flips to danger. */\n  maxLength?: number;\n  /** Override validation styling explicitly. */\n  state?: FieldValidationState;\n  /**\n   * One-off CSS, same as `Box`'s `sx`: each value may be a scalar or a\n   * responsive breakpoint map (`{ base, sm, md, lg, xl }`). Scalar-only sx\n   * merges into inline style; responsive sx injects a scoped class. Applied\n   * to the outer field wrapper.\n   */\n  sx?: SxProp;\n  /** Single form control element. The `id` is threaded to the label and aria-describedby. */\n  children: ReactElement;\n}\n\n/**\n * Field — the canonical wrapper for form controls. Handles:\n *  • Label + required asterisk / optional tag\n *  • Helper / error text (error overrides hint)\n *  • Character counter with auto-danger when > maxLength\n *  • ARIA wiring: `htmlFor`, `aria-describedby`, `aria-invalid`\n *  • Validation state propagation to the child control via className\n */\nexport function Field({\n  label,\n  hint,\n  error,\n  required = false,\n  optional = false,\n  counter,\n  maxLength,\n  state,\n  className,\n  style,\n  sx,\n  children,\n  ...rest\n}: FieldProps) {\n  const generatedId = useId();\n  // The control is the first element child. Whitespace/text/conditional siblings\n  // (`{cond && <X/>}`, MDX line breaks) are tolerated instead of throwing.\n  const nodes = Children.toArray(children);\n  const child = nodes.find(isValidElement) as ReactElement | undefined;\n  if (!child) throw new Error('<Field> needs a form control as a child.');\n  const controlId = (child.props as { id?: string }).id ?? generatedId;\n\n  const hasError = error != null && error !== false;\n  const resolvedState: FieldValidationState =\n    state ?? (hasError ? 'danger' : 'default');\n\n  const hintId = hint || error ? `${controlId}-hint` : undefined;\n  const labelId = label != null ? `${controlId}-label` : undefined;\n  const childProps = child.props as { 'aria-label'?: string; 'aria-labelledby'?: string };\n\n  // Thread id, state className, and aria wiring into the child.\n  const cloned = cloneElement(child, {\n    id: controlId,\n    // Non-native controls (MultiSelect, Select) cannot be reached by <label for>.\n    'aria-labelledby': childProps['aria-labelledby'] ?? (childProps['aria-label'] ? undefined : labelId),\n    'aria-invalid': hasError || (child.props as { 'aria-invalid'?: boolean })['aria-invalid'] || undefined,\n    'aria-describedby':\n      cx((child.props as { 'aria-describedby'?: string })['aria-describedby'], hintId) || undefined,\n    className: cx(\n      (child.props as { className?: string }).className,\n      resolvedState !== 'default' && resolvedState,\n    ),\n  } as Partial<typeof child.props>);\n\n  const counterState =\n    maxLength != null && counter != null && counter > maxLength ? 'danger' : 'default';\n\n  const { className: classes, style: mergedStyle, styleTag } = withSx(\n    sx, style, 'cny-field', className,\n  );\n\n  return (\n    <div className={classes} style={mergedStyle} {...rest}>\n      {styleTag}\n      {(label != null || counter != null) && (\n        <div className=\"cny-field-label-row\">\n          {label != null ? (\n            <label id={labelId} htmlFor={controlId} className=\"cny-field-label\">\n              {label}\n              {required && <span className=\"cny-field-required\" aria-hidden=\"true\">*</span>}\n              {optional && !required && <span className=\"cny-field-optional\">optional</span>}\n            </label>\n          ) : (\n            <span />\n          )}\n          {counter != null && (\n            <span\n              className={cx('cny-field-counter', counterState === 'danger' && 'danger')}\n              aria-live=\"polite\"\n            >\n              {maxLength != null ? `${counter}/${maxLength}` : counter}\n            </span>\n          )}\n        </div>\n      )}\n\n      {nodes.map((n) => (n === child ? cloned : n))}\n\n      {(error || hint) && (\n        <span\n          id={hintId}\n          className={cx('cny-field-hint', resolvedState !== 'default' && resolvedState)}\n          // Errors are announced as soon as they appear; hints stay passive.\n          role={hasError ? 'alert' : undefined}\n        >\n          {error ?? hint}\n        </span>\n      )}\n    </div>\n  );\n}\n"],"names":["Field","label","hint","error","required","optional","counter","maxLength","state","className","style","sx","children","rest","generatedId","useId","nodes","Children","child","isValidElement","controlId","hasError","resolvedState","hintId","labelId","childProps","cloned","cloneElement","cx","counterState","classes","mergedStyle","styleTag","withSx","jsxs","jsx","n"],"mappings":"qHA6CO,SAASA,EAAM,CACpB,MAAAC,EACA,KAAAC,EACA,MAAAC,EACA,SAAAC,EAAW,GACX,SAAAC,EAAW,GACX,QAAAC,EACA,UAAAC,EACA,MAAAC,EACA,UAAAC,EACA,MAAAC,EACA,GAAAC,EACA,SAAAC,EACA,GAAGC,CACL,EAAe,CACb,MAAMC,EAAcC,EAAAA,MAAA,EAGdC,EAAQC,EAAAA,SAAS,QAAQL,CAAQ,EACjCM,EAAQF,EAAM,KAAKG,gBAAc,EACvC,GAAI,CAACD,EAAO,MAAM,IAAI,MAAM,0CAA0C,EACtE,MAAME,EAAaF,EAAM,MAA0B,IAAMJ,EAEnDO,EAAWlB,GAAS,MAAQA,IAAU,GACtCmB,EACJd,IAAUa,EAAW,SAAW,WAE5BE,EAASrB,GAAQC,EAAQ,GAAGiB,CAAS,QAAU,OAC/CI,EAAUvB,GAAS,KAAO,GAAGmB,CAAS,SAAW,OACjDK,EAAaP,EAAM,MAGnBQ,EAASC,EAAAA,aAAaT,EAAO,CACjC,GAAIE,EAEJ,kBAAmBK,EAAW,iBAAiB,IAAMA,EAAW,YAAY,EAAI,OAAYD,GAC5F,eAAgBH,GAAaH,EAAM,MAAuC,cAAc,GAAK,OAC7F,mBACEU,EAAAA,GAAIV,EAAM,MAA0C,kBAAkB,EAAGK,CAAM,GAAK,OACtF,UAAWK,EAAAA,GACRV,EAAM,MAAiC,UACxCI,IAAkB,WAAaA,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