UNPKG

@react-md/form

Version:

This package is for creating all the different form input types.

80 lines 4.3 kB
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 __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; import { jsx as _jsx } from "react/jsx-runtime"; import { forwardRef, isValidElement, useCallback, useState } from "react"; import cn from "classnames"; import { Button } from "@react-md/button"; import { useIcon } from "@react-md/icon"; import { bem } from "@react-md/utils"; import { TextField } from "./TextField"; var block = bem("rmd-password"); function isConfigurableIcon(icon) { return !!icon && !isValidElement(icon); } /** * This component is a simple wrapper of the `TextField` that can only be * rendered for password inputs. There is built-in functionality to be able to * temporarily show the password's value by swapping the `type` to `"text"`. */ export var Password = forwardRef(function Password(_a, ref) { var className = _a.className, inputClassName = _a.inputClassName, propVisibilityIcon = _a.visibilityIcon, visibilityStyle = _a.visibilityStyle, visibilityClassName = _a.visibilityClassName, _b = _a.visibilityLabel, visibilityLabel = _b === void 0 ? "Show password" : _b, onVisibilityClick = _a.onVisibilityClick, getVisibilityIcon = _a.getVisibilityIcon, _c = _a.disableVisibility, disableVisibility = _c === void 0 ? false : _c, propRightChildren = _a.rightChildren, _d = _a.isRightAddon, isRightAddon = _d === void 0 ? disableVisibility : _d, props = __rest(_a, ["className", "inputClassName", "visibilityIcon", "visibilityStyle", "visibilityClassName", "visibilityLabel", "onVisibilityClick", "getVisibilityIcon", "disableVisibility", "rightChildren", "isRightAddon"]); var id = props.id; var _e = __read(useState("password"), 2), type = _e[0], setType = _e[1]; var toggle = useCallback(function (event) { if (onVisibilityClick) { onVisibilityClick(event); } setType(function (prevType) { return (prevType === "password" ? "text" : "password"); }); }, [onVisibilityClick]); var visible = type === "text"; var visibilityIcon = useIcon("password", propVisibilityIcon); if (isConfigurableIcon(propVisibilityIcon)) { visibilityIcon = visible ? propVisibilityIcon.visible : propVisibilityIcon.invisible; } var rightChildren = propRightChildren; if (!disableVisibility) { rightChildren = (_jsx(Button, __assign({ id: "".concat(id, "-password-toggle"), "aria-label": visibilityLabel, "aria-pressed": visible, buttonType: "icon", onClick: toggle, style: visibilityStyle, className: cn(block("toggle"), visibilityClassName) }, { children: typeof getVisibilityIcon === "function" ? getVisibilityIcon(type) : visibilityIcon }))); } return (_jsx(TextField, __assign({}, props, { className: cn(block({ offset: !disableVisibility }), className), inputClassName: cn(block("input", { offset: !disableVisibility }), inputClassName), ref: ref, type: type, isRightAddon: isRightAddon, rightChildren: rightChildren }))); }); //# sourceMappingURL=Password.js.map