@base-ui-components/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.
111 lines (110 loc) • 3.59 kB
JavaScript
'use client';
import * as React from 'react';
import PropTypes from 'prop-types';
import { useComponentRenderer } from '../../utils/useComponentRenderer.js';
import { useFieldControl } from './useFieldControl.js';
import { useFieldRootContext } from '../root/FieldRootContext.js';
import { STYLE_HOOK_MAPPING } from '../utils/constants.js';
/**
* The form control to label and validate.
* Renders an `<input>` element.
*
* You can omit this part and use any Base UI input component instead. For example,
* [Input](https://base-ui.com/react/components/input), [Checkbox](https://base-ui.com/react/components/checkbox),
* or [Select](https://base-ui.com/react/components/select), among others, will work with Field out of the box.
*
* Documentation: [Base UI Field](https://base-ui.com/react/components/field)
*/
const FieldControl = /*#__PURE__*/React.forwardRef(function FieldControl(props, forwardedRef) {
const {
render,
className,
id,
name: nameProp,
value,
disabled: disabledProp = false,
onValueChange,
defaultValue,
...otherProps
} = props;
const {
state: fieldState,
name: fieldName,
disabled: fieldDisabled
} = useFieldRootContext();
const disabled = fieldDisabled || disabledProp;
const name = fieldName ?? nameProp;
const state = React.useMemo(() => ({
...fieldState,
disabled
}), [fieldState, disabled]);
const {
getControlProps
} = useFieldControl({
id,
name,
disabled,
value,
defaultValue,
onValueChange
});
const {
renderElement
} = useComponentRenderer({
propGetter: getControlProps,
render: render ?? 'input',
ref: forwardedRef,
className,
state,
extraProps: otherProps,
customStyleHookMapping: STYLE_HOOK_MAPPING
});
return renderElement();
});
process.env.NODE_ENV !== "production" ? FieldControl.propTypes /* remove-proptypes */ = {
// ┌────────────────────────────── Warning ──────────────────────────────┐
// │ These PropTypes are generated from the TypeScript type definitions. │
// │ To update them, edit the TypeScript types and run `pnpm proptypes`. │
// └─────────────────────────────────────────────────────────────────────┘
/**
* @ignore
*/
children: PropTypes.node,
/**
* CSS class applied to the element, or a function that
* returns a class based on the component’s state.
*/
className: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
/**
* @ignore
*/
defaultValue: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.string), PropTypes.number, PropTypes.string]),
/**
* @ignore
*/
disabled: PropTypes.bool,
/**
* @ignore
*/
id: PropTypes.string,
/**
* @ignore
*/
name: PropTypes.string,
/**
* Callback fired when the `value` changes. Use when controlled.
*/
onValueChange: PropTypes.func,
/**
* Allows you to replace the component’s HTML element
* with a different tag, or compose it with another component.
*
* Accepts a `ReactElement` or a function that returns the element to render.
*/
render: PropTypes.oneOfType([PropTypes.element, PropTypes.func]),
/**
* @ignore
*/
value: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.string), PropTypes.number, PropTypes.string])
} : void 0;
export { FieldControl };