UNPKG

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