@react-md/form
Version:
This package is for creating all the different form input types.
82 lines • 5.01 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileInput = void 0;
var jsx_runtime_1 = require("react/jsx-runtime");
var react_1 = require("react");
var classnames_1 = __importDefault(require("classnames"));
var button_1 = require("@react-md/button");
var icon_1 = require("@react-md/icon");
var states_1 = require("@react-md/states");
var typography_1 = require("@react-md/typography");
var utils_1 = require("@react-md/utils");
var block = (0, utils_1.bem)("rmd-file-input");
/**
* This component is a wrapper for the `<input type="file" />` that can be themed
* like a button.
*/
exports.FileInput = (0, react_1.forwardRef)(function FileInput(_a, ref) {
var style = _a.style, propClassName = _a.className, propIcon = _a.icon, _b = _a.iconAfter, iconAfter = _b === void 0 ? false : _b, propChildren = _a.children, _c = _a.theme, theme = _c === void 0 ? "primary" : _c, _d = _a.themeType, themeType = _d === void 0 ? "contained" : _d, _e = _a.buttonType, buttonType = _e === void 0 ? propChildren ? "text" : "icon" : _e, _f = _a.multiple, multiple = _f === void 0 ? false : _f, propDisableIconSpacing = _a.disableIconSpacing, _g = _a.disableRepeatableFiles, disableRepeatableFiles = _g === void 0 ? false : _g, onKeyDown = _a.onKeyDown, onKeyUp = _a.onKeyUp, onMouseDown = _a.onMouseDown, onMouseUp = _a.onMouseUp, onMouseLeave = _a.onMouseLeave, onClick = _a.onClick, onTouchStart = _a.onTouchStart, onTouchMove = _a.onTouchMove, onTouchEnd = _a.onTouchEnd, onChange = _a.onChange, props = __rest(_a, ["style", "className", "icon", "iconAfter", "children", "theme", "themeType", "buttonType", "multiple", "disableIconSpacing", "disableRepeatableFiles", "onKeyDown", "onKeyUp", "onMouseDown", "onMouseUp", "onMouseLeave", "onClick", "onTouchStart", "onTouchMove", "onTouchEnd", "onChange"]);
var id = props.id, disabled = props.disabled;
var icon = (0, icon_1.useIcon)("upload", propIcon);
var disableIconSpacing = propDisableIconSpacing !== null && propDisableIconSpacing !== void 0 ? propDisableIconSpacing : typeof propChildren === "undefined";
var children = propChildren;
if (typeof propChildren === "undefined") {
children = (0, jsx_runtime_1.jsx)(typography_1.SrOnly, { children: "Upload" });
}
var _h = (0, states_1.useInteractionStates)({
handlers: {
onKeyDown: onKeyDown,
onKeyUp: onKeyUp,
onMouseDown: onMouseDown,
onMouseUp: onMouseUp,
onMouseLeave: onMouseLeave,
onClick: onClick,
onTouchStart: onTouchStart,
onTouchMove: onTouchMove,
onTouchEnd: onTouchEnd,
},
className: (0, button_1.buttonThemeClassNames)({
theme: theme,
themeType: themeType,
buttonType: buttonType,
disabled: disabled,
className: propClassName,
}),
// pressing enter or space would trigger two click events otherwise.
disableEnterClick: true,
}), ripples = _h.ripples, className = _h.className, handlers = _h.handlers;
var content = icon;
if (disableIconSpacing || (children && !icon)) {
content = ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [!iconAfter && icon, children, iconAfter && icon] }));
}
else if (children) {
content = ((0, jsx_runtime_1.jsx)(icon_1.TextIconSpacing, __assign({ icon: icon, iconAfter: iconAfter }, { children: children })));
}
return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("input", __assign({}, props, handlers, { ref: ref, onChange: onChange, value: disableRepeatableFiles ? undefined : "", type: "file", className: block(), multiple: multiple })), (0, jsx_runtime_1.jsxs)("label", __assign({ htmlFor: id, style: style, className: (0, classnames_1.default)("rmd-file-input-label", className) }, { children: [content, ripples] }))] }));
});
//# sourceMappingURL=FileInput.js.map