@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
99 lines (98 loc) • 4.68 kB
JavaScript
"use client";
import _extends from "@babel/runtime/helpers/esm/extends";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
const _excluded = ["id", "className", "innerRef", "value", "label", "disabled", "size"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import "core-js/modules/web.dom-collections.iterator.js";
import React, { useContext, useRef, useState, useCallback } from 'react';
import classnames from 'classnames';
import SharedContext from '../../../../shared/Context';
import StringField from '../String';
import { SubmitButton } from '../../../../components/Input';
import IconView from '../../../../icons/view';
import IconViewOff from '../../../../icons/hide';
import IconViewMedium from '../../../../icons/view_medium';
import IconViewOffMedium from '../../../../icons/hide_medium';
import { convertSnakeCaseProps } from '../../../../shared/helpers/withSnakeCaseProps';
import useTranslation from '../../hooks/useTranslation';
function Password(_ref) {
var _innerRef$current;
let {
id,
className,
innerRef,
value,
label,
disabled,
size
} = _ref,
externalProps = _objectWithoutProperties(_ref, _excluded);
const props = convertSnakeCaseProps(Object.freeze(externalProps));
const [hidden, setHidden] = useState(true);
const sharedContext = useContext(SharedContext);
const translations = useTranslation().Password;
const ref = useRef((_innerRef$current = innerRef === null || innerRef === void 0 ? void 0 : innerRef.current) !== null && _innerRef$current !== void 0 ? _innerRef$current : null);
const toggleVisibility = useCallback(event => {
const {
onShowPassword,
onHidePassword
} = convertSnakeCaseProps(props);
const value = ref.current.value;
setHidden(hidden => {
hidden ? onShowPassword === null || onShowPassword === void 0 ? void 0 : onShowPassword(_objectSpread(_objectSpread({}, event), {}, {
value
})) : onHidePassword === null || onHidePassword === void 0 ? void 0 : onHidePassword(_objectSpread(_objectSpread({}, event), {}, {
value
}));
return !hidden;
});
if (ref.current) {
ref.current.focus();
}
}, [props]);
const getAriaLabel = useCallback(() => {
const ariaLabels = {
showPassword: translations.ariaLabelShow,
hidePassword: translations.ariaLabelHide
};
if (externalProps.show_password) {
ariaLabels['showPassword'] = externalProps.show_password;
}
if (externalProps.hide_password) {
ariaLabels['hidePassword'] = externalProps.hide_password;
}
return ariaLabels;
}, [externalProps.show_password, externalProps.hide_password, translations]);
const ariaLabels = getAriaLabel();
const ToggleVisibilityButton = useCallback(() => {
return React.createElement(SubmitButton, {
id: id + '-submit-button',
type: "button",
variant: "secondary",
"aria-controls": id,
"aria-label": hidden ? ariaLabels.showPassword : ariaLabels.hidePassword,
icon: size === 'large' ? hidden ? IconViewMedium : IconViewOffMedium : hidden ? IconView : IconViewOff,
disabled: disabled,
skeleton: sharedContext.skeleton,
onClick: toggleVisibility
});
}, [id, hidden, sharedContext.skeleton, disabled, size, toggleVisibility, ariaLabels]);
return React.createElement(StringField, _extends({
id: id,
className: classnames('dnb-forms-field-password', className),
label: label !== null && label !== void 0 ? label : translations.label,
type: hidden ? 'password' : 'text',
value: value,
innerRef: ref,
"aria-describedby": id + '-submit-button',
submitElement: React.createElement(ToggleVisibilityButton, null),
disabled: disabled,
size: size,
autoComplete: "current-password"
}, props));
}
export default Password;
Password._supportsSpacingProps = true;
//# sourceMappingURL=Password.js.map