@navinc/base-react-components
Version:
Nav's Pattern Library
67 lines • 3.42 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, useTheme } from 'styled-components';
import { Input } from '../input.js';
import { IconButton } from '../icon-button.js';
const StyledIconButton = styled(IconButton).withConfig({ displayName: "brc-sc-StyledIconButton", componentId: "brc-sc-1cgz8ot" }) `
position: absolute;
right: 16px;
top: 50%;
transform: translateY(-50%);
width: 24px;
`;
const UnstyledInputWithSensitivityButton = (_a) => {
var { isVisible, onToggle } = _a, props = __rest(_a, ["isVisible", "onToggle"]);
const theme = useTheme();
const onKeyboard = useCallback((e) => {
if (e.key === ' ' || e.key === 'Enter') {
if (onToggle) {
onToggle();
}
}
}, [onToggle]);
const onMouse = useCallback((e) => {
if (e.button === 0) {
if (onToggle) {
onToggle();
}
}
}, [onToggle]);
return (_jsx(Input, Object.assign({ "data-testid": `input-with-sensitivity-button: ${props.name}` }, props, { children: _jsx(StyledIconButton, { type: "button", 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, color: theme.primary }) })));
};
/** This is a controlled `<Input>` component with styling to accommodate the "visible" and "invisible" icon button that toggles the visibility of the input value.
*
* After changing the `name` prop using the controls, please press the storybook "remount" button (the refresh icon, 2 arrow chasing each other),
*
* ## Additional props not listed
*
* - **isVisible** (boolean): visibility state of the input value
* - **value** (string): input value
* - **onChange** (func): handler for input typing
* - **onToggle** (func): handler for visibility icon click and keyboard (see code comments)
*
* @deprecated This component is deprecated and will be removed in a future release. Avoid using it in new code.
*/
export const InputWithSensitivityButton = styled(UnstyledInputWithSensitivityButton).withConfig({ displayName: "brc-sc-InputWithSensitivityButton", componentId: "brc-sc-1ti540v" }) ``;
//# sourceMappingURL=input-with-sensitivity-button.js.map