UNPKG

3.47 kBJavaScriptView Raw
1import _extends from "@babel/runtime/helpers/esm/extends";
2import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
3var _excluded = ["id", "bsPrefix", "bsCustomPrefix", "disabled", "isValid", "isInvalid", "feedbackTooltip", "feedback", "className", "style", "label", "children", "custom", "lang", "data-browse", "as", "inputAs"];
4import classNames from 'classnames';
5import React, { useContext, useMemo } from 'react';
6import all from 'prop-types-extra/lib/all';
7import Feedback from './Feedback';
8import FormFileInput from './FormFileInput';
9import FormFileLabel from './FormFileLabel';
10import FormContext from './FormContext';
11import { useBootstrapPrefix } from './ThemeProvider';
12var FormFile = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
13 var id = _ref.id,
14 bsPrefix = _ref.bsPrefix,
15 bsCustomPrefix = _ref.bsCustomPrefix,
16 _ref$disabled = _ref.disabled,
17 disabled = _ref$disabled === void 0 ? false : _ref$disabled,
18 _ref$isValid = _ref.isValid,
19 isValid = _ref$isValid === void 0 ? false : _ref$isValid,
20 _ref$isInvalid = _ref.isInvalid,
21 isInvalid = _ref$isInvalid === void 0 ? false : _ref$isInvalid,
22 _ref$feedbackTooltip = _ref.feedbackTooltip,
23 feedbackTooltip = _ref$feedbackTooltip === void 0 ? false : _ref$feedbackTooltip,
24 feedback = _ref.feedback,
25 className = _ref.className,
26 style = _ref.style,
27 label = _ref.label,
28 children = _ref.children,
29 custom = _ref.custom,
30 lang = _ref.lang,
31 dataBrowse = _ref['data-browse'],
32 _ref$as = _ref.as,
33 Component = _ref$as === void 0 ? 'div' : _ref$as,
34 _ref$inputAs = _ref.inputAs,
35 inputAs = _ref$inputAs === void 0 ? 'input' : _ref$inputAs,
36 props = _objectWithoutPropertiesLoose(_ref, _excluded);
37
38 var _ref2 = custom ? [bsCustomPrefix, 'custom'] : [bsPrefix, 'form-file'],
39 prefix = _ref2[0],
40 defaultPrefix = _ref2[1];
41
42 bsPrefix = useBootstrapPrefix(prefix, defaultPrefix);
43 var type = 'file';
44
45 var _useContext = useContext(FormContext),
46 controlId = _useContext.controlId;
47
48 var innerFormContext = useMemo(function () {
49 return {
50 controlId: id || controlId,
51 custom: custom
52 };
53 }, [controlId, custom, id]);
54 var hasLabel = label != null && label !== false && !children;
55 var input = /*#__PURE__*/React.createElement(FormFileInput, _extends({}, props, {
56 ref: ref,
57 isValid: isValid,
58 isInvalid: isInvalid,
59 disabled: disabled,
60 as: inputAs,
61 lang: lang
62 }));
63 return /*#__PURE__*/React.createElement(FormContext.Provider, {
64 value: innerFormContext
65 }, /*#__PURE__*/React.createElement(Component, {
66 style: style,
67 className: classNames(className, bsPrefix, custom && "custom-" + type)
68 }, children || /*#__PURE__*/React.createElement(React.Fragment, null, custom ? /*#__PURE__*/React.createElement(React.Fragment, null, input, hasLabel && /*#__PURE__*/React.createElement(FormFileLabel, {
69 "data-browse": dataBrowse
70 }, label)) : /*#__PURE__*/React.createElement(React.Fragment, null, hasLabel && /*#__PURE__*/React.createElement(FormFileLabel, null, label), input), (isValid || isInvalid) && /*#__PURE__*/React.createElement(Feedback, {
71 type: isValid ? 'valid' : 'invalid',
72 tooltip: feedbackTooltip
73 }, feedback))));
74});
75FormFile.displayName = 'FormFile';
76FormFile.Input = FormFileInput;
77FormFile.Label = FormFileLabel;
78export default FormFile;
\No newline at end of file