UNPKG

2.62 kBJavaScriptView Raw
1import _objectWithoutProperties from 'babel-runtime/helpers/objectWithoutProperties';
2import _extends from 'babel-runtime/helpers/extends';
3import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
4import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
5import _inherits from 'babel-runtime/helpers/inherits';
6import classNames from 'classnames';
7import React from 'react';
8import PropTypes from 'prop-types';
9
10import Glyphicon from './Glyphicon';
11import { bsClass, getClassSet, splitBsProps } from './utils/bootstrapUtils';
12
13var defaultProps = {
14 bsRole: 'feedback'
15};
16
17var contextTypes = {
18 $bs_formGroup: PropTypes.object
19};
20
21var FormControlFeedback = function (_React$Component) {
22 _inherits(FormControlFeedback, _React$Component);
23
24 function FormControlFeedback() {
25 _classCallCheck(this, FormControlFeedback);
26
27 return _possibleConstructorReturn(this, _React$Component.apply(this, arguments));
28 }
29
30 FormControlFeedback.prototype.getGlyph = function getGlyph(validationState) {
31 switch (validationState) {
32 case 'success':
33 return 'ok';
34 case 'warning':
35 return 'warning-sign';
36 case 'error':
37 return 'remove';
38 default:
39 return null;
40 }
41 };
42
43 FormControlFeedback.prototype.renderDefaultFeedback = function renderDefaultFeedback(formGroup, className, classes, elementProps) {
44 var glyph = this.getGlyph(formGroup && formGroup.validationState);
45 if (!glyph) {
46 return null;
47 }
48
49 return React.createElement(Glyphicon, _extends({}, elementProps, {
50 glyph: glyph,
51 className: classNames(className, classes)
52 }));
53 };
54
55 FormControlFeedback.prototype.render = function render() {
56 var _props = this.props,
57 className = _props.className,
58 children = _props.children,
59 props = _objectWithoutProperties(_props, ['className', 'children']);
60
61 var _splitBsProps = splitBsProps(props),
62 bsProps = _splitBsProps[0],
63 elementProps = _splitBsProps[1];
64
65 var classes = getClassSet(bsProps);
66
67 if (!children) {
68 return this.renderDefaultFeedback(this.context.$bs_formGroup, className, classes, elementProps);
69 }
70
71 var child = React.Children.only(children);
72 return React.cloneElement(child, _extends({}, elementProps, {
73 className: classNames(child.props.className, className, classes)
74 }));
75 };
76
77 return FormControlFeedback;
78}(React.Component);
79
80FormControlFeedback.defaultProps = defaultProps;
81FormControlFeedback.contextTypes = contextTypes;
82
83export default bsClass('form-control-feedback', FormControlFeedback);
\No newline at end of file