UNPKG

koval-ui

Version:

React components collection with minimalistic design. Supports theming, layout, and input validation.

1 lines 3.13 kB
{"version":3,"file":"FormField.cjs","sources":["../../../../src/lib/FormField/FormField.tsx"],"sourcesContent":["import {Children, cloneElement, forwardRef, useMemo} from 'react';\nimport type {ReactElement} from 'react';\nimport classNames from 'classnames';\n\nimport type {LibraryProps, DataAttributes} from '@/internal/LibraryAPI';\nimport {useInternalId} from '@/internal/hooks/useInternalId.ts';\n\nimport classes from './FormField.module.css';\n\nexport type Props = DataAttributes &\n LibraryProps & {\n children: ReactElement;\n /** Set text label */\n label: string;\n /** Set hint text to be displayed below the input */\n hint?: string;\n /**\n * Renders * character after label indicating required input status.\n * Also, set automatically when required input is provided.\n */\n required?: boolean;\n };\n\nexport const FormField = forwardRef<HTMLDivElement, Props>(\n ({className, children, label, hint, required, ...nativeProps}, ref) => {\n const inputProps = Children.only(children).props;\n const id = useInternalId(inputProps.id);\n const hintId = `${id}-hint`;\n const childrenWithProps = useMemo(\n () =>\n cloneElement(Children.only(children), {\n id,\n ...(hint && {'aria-describedby': hintId}),\n }),\n [children, hint, hintId, id]\n );\n return (\n <div {...nativeProps} ref={ref} className={classNames(classes.wrapper, className)}>\n <label\n className={classNames(classes.label, {\n [classes.required]: inputProps.required || required,\n })}\n htmlFor={id}>\n {label}\n </label>\n {childrenWithProps}\n {hint && (\n <div className={classes.hint} id={hintId}>\n {hint}\n </div>\n )}\n </div>\n );\n }\n);\n\nFormField.displayName = 'FormField';\n"],"names":["FormField","forwardRef","className","children","label","hint","required","nativeProps","ref","inputProps","Children","id","useInternalId","hintId","childrenWithProps","useMemo","cloneElement","jsxs","classNames","classes","jsx"],"mappings":"yQAuBaA,EAAYC,EAAA,WACrB,CAAC,CAAC,UAAAC,EAAW,SAAAC,EAAU,MAAAC,EAAO,KAAAC,EAAM,SAAAC,EAAU,GAAGC,CAAW,EAAGC,IAAQ,CACnE,MAAMC,EAAaC,EAAA,SAAS,KAAKP,CAAQ,EAAE,MACrCQ,EAAKC,EAAAA,cAAcH,EAAW,EAAE,EAChCI,EAAS,GAAGF,CAAE,QACdG,EAAoBC,EAAA,QACtB,IACIC,eAAaN,EAAAA,SAAS,KAAKP,CAAQ,EAAG,CAClC,GAAAQ,EACA,GAAIN,GAAQ,CAAC,mBAAoBQ,CAAM,CAAA,CAC1C,EACL,CAACV,EAAUE,EAAMQ,EAAQF,CAAE,CAC/B,EAEI,OAAAM,OAAC,MAAK,CAAA,GAAGV,EAAa,IAAAC,EAAU,UAAWU,EAAWC,EAAAA,QAAQ,QAASjB,CAAS,EAC5E,SAAA,CAAAkB,EAAA,IAAC,QAAA,CACG,UAAWF,EAAWC,EAAA,QAAQ,MAAO,CACjC,CAACA,UAAQ,QAAQ,EAAGV,EAAW,UAAYH,CAAA,CAC9C,EACD,QAASK,EACR,SAAAP,CAAA,CACL,EACCU,EACAT,SACI,MAAI,CAAA,UAAWc,EAAAA,QAAQ,KAAM,GAAIN,EAC7B,SACLR,CAAA,CAAA,CAAA,EAER,CAAA,CAGZ,EAEAL,EAAU,YAAc"}