UNPKG

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