@primer/react
Version:
An implementation of GitHub's Primer Design System using React
57 lines (53 loc) • 1.96 kB
JavaScript
import { c } from 'react-compiler-runtime';
import { useContext, createContext } from 'react';
const FormControlContext = /*#__PURE__*/createContext(null);
const FormControlContextProvider = FormControlContext.Provider;
/** This is the private/internal interface for subcomponents of `FormControl`. */
function useFormControlContext() {
var _useContext;
return (_useContext = useContext(FormControlContext)) !== null && _useContext !== void 0 ? _useContext : {};
}
/**
* Make any component compatible with `FormControl`'s automatic wiring up of accessibility attributes & validation by
* reading the props from this hook and merging them with the passed-in props. If used outside of `FormControl`, this
* hook has no effect.
*
* @param externalProps The external props passed to this component. If provided, these props will be merged with the
* `FormControl` props, with external props taking priority.
*/
function useFormControlForwardedProps(externalProps) {
const $ = c(9);
const context = useContext(FormControlContext);
if (!context) {
return externalProps;
}
let t0;
if ($[0] !== context.captionId || $[1] !== context.validationMessageId) {
t0 = [context.validationMessageId, context.captionId].filter(Boolean).join(" ") || undefined;
$[0] = context.captionId;
$[1] = context.validationMessageId;
$[2] = t0;
} else {
t0 = $[2];
}
let t1;
if ($[3] !== context.disabled || $[4] !== context.id || $[5] !== context.required || $[6] !== externalProps || $[7] !== t0) {
t1 = {
disabled: context.disabled,
id: context.id,
required: context.required,
"aria-describedby": t0,
...externalProps
};
$[3] = context.disabled;
$[4] = context.id;
$[5] = context.required;
$[6] = externalProps;
$[7] = t0;
$[8] = t1;
} else {
t1 = $[8];
}
return t1;
}
export { FormControlContextProvider, useFormControlContext, useFormControlForwardedProps };