UNPKG

@base-ui/react

Version:

Base UI is a library of headless ('unstyled') React components and low-level hooks. You gain complete control over your app's CSS and accessibility features.

30 lines (29 loc) 919 B
'use client'; import * as React from 'react'; import { useIsoLayoutEffect } from '@base-ui/utils/useIsoLayoutEffect'; import { useFieldRootContext } from "../field-root-context/FieldRootContext.js"; export function useRegisterFieldControl(controlRef, id, value, getFormValueOverride, enabled = true) { const { registerFieldControl } = useFieldRootContext(); const sourceRef = React.useRef(null); if (!sourceRef.current) { sourceRef.current = Symbol(); } useIsoLayoutEffect(() => { const source = sourceRef.current; if (!source || !enabled) { return undefined; } const registration = { controlRef, getValue: getFormValueOverride, id, value }; registerFieldControl(source, registration); return () => { registerFieldControl(source, undefined); }; }, [controlRef, enabled, getFormValueOverride, id, registerFieldControl, value]); }