@navinc/base-react-components
Version:
Nav's Pattern Library
49 lines • 2.54 kB
JavaScript
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;
};
import { jsx as _jsx } from "react/jsx-runtime";
import { useCallback } from 'react';
import styled from 'styled-components';
import { Input } from '../input.js';
import { IconButton } from '../icon-button';
const StyledIconButton = styled(IconButton).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 = useCallback((e) => {
if (e.key === ' ' || e.key === 'Enter') {
onToggle && onToggle();
}
}, [onToggle]);
const onMouse = useCallback((e) => {
if (e.button === 0) {
onToggle && onToggle();
}
}, [onToggle]);
return (_jsx(Input, Object.assign({ "data-testid": `input-with-sensitivity-button: ${props.name}` }, props, { children: _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 }) })));
};
export const InputWithSensitivityButton = styled(_InputWithSensitivityButton).withConfig({ displayName: "brc-sc-InputWithSensitivityButton", componentId: "brc-sc-1k9652a" }) ``;
//# sourceMappingURL=input-with-sensitivity-button.js.map