UNPKG

@navinc/base-react-components

Version:
55 lines 3.02 kB
"use strict"; 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 }); const jsx_runtime_1 = require("react/jsx-runtime"); const react_1 = require("react"); const styled_components_1 = __importDefault(require("styled-components")); const input_js_1 = __importDefault(require("../input.js")); const icon_button_1 = __importDefault(require("../icon-button")); const StyledIconButton = (0, styled_components_1.default)(icon_button_1.default).withConfig({ displayName: "brc-sc-StyledIconButton", componentId: "brc-sc-5hm24j" }) ` position: absolute; right: 16px; top: 50%; transform: translateY(-50%); width: 24px; `; const InputWithSensitivityButton = (_a) => { var { isVisible, onToggle } = _a, props = __rest(_a, ["isVisible", "onToggle"]); const onKeyboard = (0, react_1.useCallback)((e) => { if (e.key === ' ' || e.key === 'Enter') { onToggle && onToggle(); } }, [onToggle]); const onMouse = (0, react_1.useCallback)((e) => { if (e.button === 0) { onToggle && onToggle(); } }, [onToggle]); return ((0, jsx_runtime_1.jsx)(input_js_1.default, Object.assign({ "data-testid": `input-with-sensitivity-button: ${props.name}` }, props, { children: (0, jsx_runtime_1.jsx)(StyledIconButton, { name: isVisible ? 'feedback/invisible' : 'feedback/visible', /* Using onToggle instead of onClick. When using on click, the following occurs: * 1. on mouse down, the onBlur of the input fires and calls field.onBlur - this triggers a state update * 2. the component rerenders - this means this Icon component rerenders. The element is swapped out with * a new identical element * 3. on mouse up, the browser does not send the onClick event because the element is different. On click * requires a mouse-down followed by a mouse-up on the same element * * Using React.memo(VisibilityIconComponent) also works */ onMouseDown: onMouse, onKeyPress: onKeyboard }) }))); }; const StyledInputWithSensitivityButton = (0, styled_components_1.default)(InputWithSensitivityButton).withConfig({ displayName: "brc-sc-StyledInputWithSensitivityButton", componentId: "brc-sc-1et3kje" }) ``; exports.default = StyledInputWithSensitivityButton; //# sourceMappingURL=input-with-sensitivity-button.js.map