UNPKG

1.94 kBJavaScriptView Raw
1import _extends from "@babel/runtime/helpers/esm/extends";
2import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
3import classNames from 'classnames';
4import React, { useContext } from 'react';
5import warning from 'warning';
6import Col from './Col';
7import FormContext from './FormContext';
8import { useBootstrapPrefix } from './ThemeProvider';
9var defaultProps = {
10 column: false,
11 srOnly: false
12};
13var FormLabel = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
14 var _ref$as = _ref.as,
15 Component = _ref$as === void 0 ? 'label' : _ref$as,
16 bsPrefix = _ref.bsPrefix,
17 column = _ref.column,
18 srOnly = _ref.srOnly,
19 className = _ref.className,
20 htmlFor = _ref.htmlFor,
21 props = _objectWithoutPropertiesLoose(_ref, ["as", "bsPrefix", "column", "srOnly", "className", "htmlFor"]);
22
23 var _useContext = useContext(FormContext),
24 controlId = _useContext.controlId;
25
26 bsPrefix = useBootstrapPrefix(bsPrefix, 'form-label');
27 var columnClass = 'col-form-label';
28 if (typeof column === 'string') columnClass = columnClass + " " + columnClass + "-" + column;
29 var classes = classNames(className, bsPrefix, srOnly && 'sr-only', column && columnClass);
30 process.env.NODE_ENV !== "production" ? warning(controlId == null || !htmlFor, '`controlId` is ignored on `<FormLabel>` when `htmlFor` is specified.') : void 0;
31 htmlFor = htmlFor || controlId;
32 if (column) return /*#__PURE__*/React.createElement(Col, _extends({
33 as: "label",
34 className: classes,
35 htmlFor: htmlFor
36 }, props));
37 return (
38 /*#__PURE__*/
39 // eslint-disable-next-line jsx-a11y/label-has-for, jsx-a11y/label-has-associated-control
40 React.createElement(Component, _extends({
41 ref: ref,
42 className: classes,
43 htmlFor: htmlFor
44 }, props))
45 );
46});
47FormLabel.displayName = 'FormLabel';
48FormLabel.defaultProps = defaultProps;
49export default FormLabel;
\No newline at end of file