@financial-times/o3-form 0.7.1 → 0.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/cjs/PasswordInput.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var _jsxruntime = require('react/jsx-runtime');
|
|
2
2
|
var _outils = require('@financial-times/o-utils');
|
|
3
3
|
var _FormField = require('./fieldComponents/FormField');
|
|
4
|
-
var
|
|
4
|
+
var _PasswordInputToggle = require('./PasswordInputToggle');
|
|
5
5
|
var _react = require('react');
|
|
6
6
|
const uniqueId = _outils.uidBuilder.call(void 0, "o3-form-password-input");
|
|
7
7
|
const PasswordInput = ({
|
|
@@ -17,9 +17,7 @@ const PasswordInput = ({
|
|
|
17
17
|
const id = inputId || uniqueId("_");
|
|
18
18
|
let inputRef = _react.useRef.call(void 0, null);
|
|
19
19
|
let buttonRef = _react.useRef.call(void 0, null);
|
|
20
|
-
let toggleRef = _react.useRef.call(void 0,
|
|
21
|
-
null
|
|
22
|
-
);
|
|
20
|
+
let toggleRef = _react.useRef.call(void 0, null);
|
|
23
21
|
const inputClasses = [
|
|
24
22
|
"o3-form",
|
|
25
23
|
"o3-form-text-input",
|
|
@@ -32,7 +30,7 @@ const PasswordInput = ({
|
|
|
32
30
|
const inputEl = inputRef.current;
|
|
33
31
|
const btnEl = buttonRef.current;
|
|
34
32
|
if (!inputEl || !btnEl) return;
|
|
35
|
-
toggleRef.current = new (0,
|
|
33
|
+
toggleRef.current = new (0, _PasswordInputToggle.PasswordInputToggle)(inputEl, {
|
|
36
34
|
toggleButtonId: btnEl.id
|
|
37
35
|
});
|
|
38
36
|
return () => {
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {Object} PasswordInputOptions
|
|
3
|
+
* @property {string} [toggleButtonId]
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @export
|
|
7
|
+
*/
|
|
8
|
+
declare class PasswordInputToggle {
|
|
9
|
+
_toggleButtonId: string;
|
|
10
|
+
_input: HTMLInputElement;
|
|
11
|
+
_toggleState: boolean;
|
|
12
|
+
_toggleButton: HTMLElement | null;
|
|
13
|
+
/**
|
|
14
|
+
* Class constructor.
|
|
15
|
+
*
|
|
16
|
+
* @param {HTMLElement} [element] - An input element in the DOM
|
|
17
|
+
* @param {PasswordInputToggleOptions} [options] - An options object for configuring the password input
|
|
18
|
+
*/
|
|
19
|
+
constructor(element: HTMLInputElement, options?: {
|
|
20
|
+
toggleButtonId: string;
|
|
21
|
+
});
|
|
22
|
+
_setAttributes(): void;
|
|
23
|
+
/** @returns {void} */
|
|
24
|
+
toggle(): void;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export { PasswordInputToggle };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});class PasswordInputToggle {
|
|
2
|
+
/**
|
|
3
|
+
* Class constructor.
|
|
4
|
+
*
|
|
5
|
+
* @param {HTMLElement} [element] - An input element in the DOM
|
|
6
|
+
* @param {PasswordInputToggleOptions} [options] - An options object for configuring the password input
|
|
7
|
+
*/
|
|
8
|
+
constructor(element, options = { toggleButtonId: "o3-form-password-input-toggle" }) {
|
|
9
|
+
this._toggleButtonId = options.toggleButtonId;
|
|
10
|
+
this._input = element;
|
|
11
|
+
this._toggleState = false;
|
|
12
|
+
this._toggleButton = document.getElementById(options.toggleButtonId);
|
|
13
|
+
this._setAttributes();
|
|
14
|
+
}
|
|
15
|
+
_setAttributes() {
|
|
16
|
+
const buttonLabel = this._toggleState ? "Hide password" : "Show password";
|
|
17
|
+
this._input.type = this._toggleState ? "text" : "password";
|
|
18
|
+
if (this._toggleButton) {
|
|
19
|
+
this._toggleButton.setAttribute(
|
|
20
|
+
"aria-pressed",
|
|
21
|
+
this._toggleState.toString()
|
|
22
|
+
);
|
|
23
|
+
this._toggleButton.setAttribute("aria-label", buttonLabel);
|
|
24
|
+
this._toggleButton.setAttribute("title", buttonLabel);
|
|
25
|
+
this._toggleButton.classList.remove(
|
|
26
|
+
this._toggleState ? "o3-password-input__show-password-toggle--show" : "o3-password-input__show-password-toggle--hide"
|
|
27
|
+
);
|
|
28
|
+
this._toggleButton.classList.add(
|
|
29
|
+
this._toggleState ? "o3-password-input__show-password-toggle--hide" : "o3-password-input__show-password-toggle--show"
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/** @returns {void} */
|
|
34
|
+
toggle() {
|
|
35
|
+
if (!this._toggleButton) {
|
|
36
|
+
throw new Error(
|
|
37
|
+
`Toggle button for password field not found using id ${this._toggleButtonId}`
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
this._toggleState = !this._toggleState;
|
|
41
|
+
this._setAttributes();
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
exports.PasswordInputToggle = PasswordInputToggle;
|
package/esm/PasswordInput.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
2
2
|
import { uidBuilder } from "@financial-times/o-utils";
|
|
3
3
|
import { LabeledFormField } from "./fieldComponents/FormField";
|
|
4
|
-
import {
|
|
4
|
+
import { PasswordInputToggle as PasswordInputToggleController } from "./PasswordInputToggle";
|
|
5
5
|
import { useEffect, useRef } from "react";
|
|
6
6
|
const uniqueId = uidBuilder("o3-form-password-input");
|
|
7
7
|
const PasswordInput = ({
|
|
@@ -17,9 +17,7 @@ const PasswordInput = ({
|
|
|
17
17
|
const id = inputId || uniqueId("_");
|
|
18
18
|
let inputRef = useRef(null);
|
|
19
19
|
let buttonRef = useRef(null);
|
|
20
|
-
let toggleRef = useRef(
|
|
21
|
-
null
|
|
22
|
-
);
|
|
20
|
+
let toggleRef = useRef(null);
|
|
23
21
|
const inputClasses = [
|
|
24
22
|
"o3-form",
|
|
25
23
|
"o3-form-text-input",
|
|
@@ -32,7 +30,7 @@ const PasswordInput = ({
|
|
|
32
30
|
const inputEl = inputRef.current;
|
|
33
31
|
const btnEl = buttonRef.current;
|
|
34
32
|
if (!inputEl || !btnEl) return;
|
|
35
|
-
toggleRef.current = new
|
|
33
|
+
toggleRef.current = new PasswordInputToggleController(inputEl, {
|
|
36
34
|
toggleButtonId: btnEl.id
|
|
37
35
|
});
|
|
38
36
|
return () => {
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {Object} PasswordInputOptions
|
|
3
|
+
* @property {string} [toggleButtonId]
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @export
|
|
7
|
+
*/
|
|
8
|
+
declare class PasswordInputToggle {
|
|
9
|
+
_toggleButtonId: string;
|
|
10
|
+
_input: HTMLInputElement;
|
|
11
|
+
_toggleState: boolean;
|
|
12
|
+
_toggleButton: HTMLElement | null;
|
|
13
|
+
/**
|
|
14
|
+
* Class constructor.
|
|
15
|
+
*
|
|
16
|
+
* @param {HTMLElement} [element] - An input element in the DOM
|
|
17
|
+
* @param {PasswordInputToggleOptions} [options] - An options object for configuring the password input
|
|
18
|
+
*/
|
|
19
|
+
constructor(element: HTMLInputElement, options?: {
|
|
20
|
+
toggleButtonId: string;
|
|
21
|
+
});
|
|
22
|
+
_setAttributes(): void;
|
|
23
|
+
/** @returns {void} */
|
|
24
|
+
toggle(): void;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export { PasswordInputToggle };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
class PasswordInputToggle {
|
|
2
|
+
/**
|
|
3
|
+
* Class constructor.
|
|
4
|
+
*
|
|
5
|
+
* @param {HTMLElement} [element] - An input element in the DOM
|
|
6
|
+
* @param {PasswordInputToggleOptions} [options] - An options object for configuring the password input
|
|
7
|
+
*/
|
|
8
|
+
constructor(element, options = { toggleButtonId: "o3-form-password-input-toggle" }) {
|
|
9
|
+
this._toggleButtonId = options.toggleButtonId;
|
|
10
|
+
this._input = element;
|
|
11
|
+
this._toggleState = false;
|
|
12
|
+
this._toggleButton = document.getElementById(options.toggleButtonId);
|
|
13
|
+
this._setAttributes();
|
|
14
|
+
}
|
|
15
|
+
_setAttributes() {
|
|
16
|
+
const buttonLabel = this._toggleState ? "Hide password" : "Show password";
|
|
17
|
+
this._input.type = this._toggleState ? "text" : "password";
|
|
18
|
+
if (this._toggleButton) {
|
|
19
|
+
this._toggleButton.setAttribute(
|
|
20
|
+
"aria-pressed",
|
|
21
|
+
this._toggleState.toString()
|
|
22
|
+
);
|
|
23
|
+
this._toggleButton.setAttribute("aria-label", buttonLabel);
|
|
24
|
+
this._toggleButton.setAttribute("title", buttonLabel);
|
|
25
|
+
this._toggleButton.classList.remove(
|
|
26
|
+
this._toggleState ? "o3-password-input__show-password-toggle--show" : "o3-password-input__show-password-toggle--hide"
|
|
27
|
+
);
|
|
28
|
+
this._toggleButton.classList.add(
|
|
29
|
+
this._toggleState ? "o3-password-input__show-password-toggle--hide" : "o3-password-input__show-password-toggle--show"
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/** @returns {void} */
|
|
34
|
+
toggle() {
|
|
35
|
+
if (!this._toggleButton) {
|
|
36
|
+
throw new Error(
|
|
37
|
+
`Toggle button for password field not found using id ${this._toggleButtonId}`
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
this._toggleState = !this._toggleState;
|
|
41
|
+
this._setAttributes();
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
export {
|
|
45
|
+
PasswordInputToggle
|
|
46
|
+
};
|