@razorpay/blade
Version:
The Design System that powers Razorpay
271 lines (267 loc) • 12.3 kB
JavaScript
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
import React__default, { useState, useEffect } from 'react';
import '../BaseInput/index.js';
import { getKeyboardAndAutocompleteProps } from '../BaseInput/utils.js';
import isEmpty from '../../../utils/lodashButBetter/isEmpty.js';
import '../../Icons/index.js';
import '../../Button/IconButton/index.js';
import '../../../utils/metaAttribute/index.js';
import '../../Box/BaseBox/index.js';
import '../../Spinner/index.js';
import '../../../utils/assignWithoutSideEffects/index.js';
import '../../../utils/index.js';
import { useMergeRefs } from '../../../utils/useMergeRefs.js';
import { dropdownComponentIds } from '../../Dropdown/dropdownComponentIds.js';
import { useDropdown } from '../../Dropdown/useDropdown.js';
import '../../Dropdown/index.js';
import '../../Divider/index.js';
import '../../../utils/isValidAllowedChildren/index.js';
import { TopNavOverlayThemeOverride } from '../../TopNav/TopNavOverlayThemeOverride.js';
import { useModalContext } from '../../Modal/ModalContext.js';
import { jsx, jsxs } from 'react/jsx-runtime';
import { getPlatformType } from '../../../utils/getPlatformType/getPlatformType.js';
import { getComponentId } from '../../../utils/isValidAllowedChildren/isValidAllowedChildren.js';
import { InputDropdownButton } from '../../Dropdown/InputDropdownButton.web.js';
import { DropdownOverlay } from '../../Dropdown/DropdownOverlay.web.js';
import { IconButton } from '../../Button/IconButton/IconButton.js';
import CloseIcon from '../../Icons/CloseIcon/CloseIcon.js';
import { Spinner } from '../../Spinner/Spinner/Spinner.js';
import { BaseBox } from '../../Box/BaseBox/BaseBox.web.js';
import { Divider } from '../../Divider/Divider.js';
import { BaseInput } from '../BaseInput/BaseInput.js';
import { MetaConstants } from '../../../utils/metaAttribute/metaConstants.js';
import SearchIcon from '../../Icons/SearchIcon/SearchIcon.js';
import { assignWithoutSideEffects } from '../../../utils/assignWithoutSideEffects/assignWithoutSideEffects.js';
var _excluded = ["label", "accessibilityLabel", "labelPosition", "placeholder", "defaultValue", "name", "value", "onChange", "onClick", "onFocus", "onBlur", "onSubmit", "isDisabled", "labelSuffix", "labelTrailing", "helpText", "onClearButtonClick", "isLoading", "autoCapitalize", "autoFocus", "testID", "size", "showSearchIcon", "trailing"];
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; }
// need to do this to tell TS to infer type as SearchInput of React Native and make it believe that `ref.current.clear()` exists
// eslint-disable-next-line @typescript-eslint/no-explicit-any
var isReactNative = function isReactNative(_textInputRef) {
return getPlatformType() === 'react-native';
};
var _SearchInput = function _SearchInput(_ref, ref) {
var label = _ref.label,
accessibilityLabel = _ref.accessibilityLabel,
_ref$labelPosition = _ref.labelPosition,
labelPosition = _ref$labelPosition === void 0 ? 'top' : _ref$labelPosition,
placeholder = _ref.placeholder,
defaultValue = _ref.defaultValue,
name = _ref.name,
value = _ref.value,
_onChange = _ref.onChange,
_onClick = _ref.onClick,
_onFocus = _ref.onFocus,
_onBlur = _ref.onBlur,
onSubmit = _ref.onSubmit,
isDisabled = _ref.isDisabled,
labelSuffix = _ref.labelSuffix,
labelTrailing = _ref.labelTrailing,
helpText = _ref.helpText,
onClearButtonClick = _ref.onClearButtonClick,
isLoading = _ref.isLoading,
autoCapitalize = _ref.autoCapitalize,
autoFocus = _ref.autoFocus,
testID = _ref.testID,
_ref$size = _ref.size,
size = _ref$size === void 0 ? 'medium' : _ref$size,
_ref$showSearchIcon = _ref.showSearchIcon,
showSearchIcon = _ref$showSearchIcon === void 0 ? true : _ref$showSearchIcon,
trailing = _ref.trailing,
rest = _objectWithoutProperties(_ref, _excluded);
var textInputRef = React__default.useRef(null);
var mergedRef = useMergeRefs(ref, textInputRef);
var _useState = useState(false),
_useState2 = _slicedToArray(_useState, 2),
shouldShowClearButton = _useState2[0],
setShouldShowClearButton = _useState2[1];
var _useState3 = useState(false),
_useState4 = _slicedToArray(_useState3, 2),
isTrailingDropDownOpen = _useState4[0],
setIsTrailingDropDownOpen = _useState4[1];
var _useDropdown = useDropdown(),
triggererWrapperRef = _useDropdown.triggererWrapperRef,
onTriggerKeydown = _useDropdown.onTriggerKeydown,
onTriggerClick = _useDropdown.onTriggerClick,
dropdownTriggerer = _useDropdown.dropdownTriggerer,
closeParentDropDown = _useDropdown.close,
isParentDropDownOpen = _useDropdown.isOpen;
var isInsideDropdown = dropdownTriggerer === 'SearchInput';
var _useState5 = useState(false),
_useState6 = _slicedToArray(_useState5, 2),
isSearchFocused = _useState6[0],
setIsSearchFocused = _useState6[1];
var modalContext = useModalContext();
React__default.useEffect(function () {
setShouldShowClearButton(Boolean(defaultValue !== null && defaultValue !== void 0 ? defaultValue : value));
}, [defaultValue, value]);
useEffect(function () {
if (isParentDropDownOpen && isTrailingDropDownOpen) {
setIsTrailingDropDownOpen(false);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [closeParentDropDown, isParentDropDownOpen]);
useEffect(function () {
if (isTrailingDropDownOpen && isParentDropDownOpen) {
closeParentDropDown();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [setIsTrailingDropDownOpen, isTrailingDropDownOpen]);
var trailingDropdown = trailing && getComponentId(trailing) === 'Dropdown' ? trailing : null;
var renderTrailingDropDown = function renderTrailingDropDown() {
if (!trailingDropdown) {
return null;
}
return /*#__PURE__*/React__default.cloneElement(trailingDropdown, {
selectionType: 'single',
isOpen: isTrailingDropDownOpen,
height: '100%',
onOpenChange: function onOpenChange(isOpen) {
setIsTrailingDropDownOpen(isOpen);
},
children: React__default.Children.map(trailingDropdown.props.children, function (child) {
if (child.type === InputDropdownButton) {
return /*#__PURE__*/React__default.cloneElement(child, {
_isInsideSearchInput: true,
size: size
});
}
if (child.type === DropdownOverlay) {
return /*#__PURE__*/React__default.cloneElement(child, {
referenceRef: triggererWrapperRef,
_isNestedDropdown: true,
defaultPlacement: 'bottom-end'
});
}
return child;
})
});
};
var renderClearButton = function renderClearButton() {
return /*#__PURE__*/jsx(IconButton, {
size: "medium",
icon: CloseIcon,
onClick: function onClick() {
var _textInputRef$current;
if (isEmpty(value) && textInputRef.current) {
// when the input field is uncontrolled take the ref and clear the input and then call the onClearButtonClick function
if (isReactNative(textInputRef.current)) {
textInputRef.current.clear();
textInputRef.current.focus();
} else if (textInputRef.current instanceof HTMLInputElement) {
textInputRef.current.value = '';
textInputRef.current.focus();
}
}
// if the input field is controlled just call the click handler and the value change shall be left upto the consumer
onClearButtonClick === null || onClearButtonClick === void 0 || onClearButtonClick();
textInputRef === null || textInputRef === void 0 || (_textInputRef$current = textInputRef.current) === null || _textInputRef$current === void 0 || _textInputRef$current.focus();
setShouldShowClearButton(false);
},
isDisabled: isDisabled,
accessibilityLabel: "Clear Input Content"
});
};
var renderInteractionElement = function renderInteractionElement() {
if (isLoading) {
return /*#__PURE__*/jsx(Spinner, {
accessibilityLabel: "Loading Content",
color: "primary"
});
}
if (shouldShowClearButton && trailingDropdown) {
return /*#__PURE__*/jsxs(BaseBox, {
display: "flex",
gap: "spacing.3",
children: [renderClearButton(), " ", /*#__PURE__*/jsx(Divider, {
orientation: "vertical"
})]
});
}
if (shouldShowClearButton) {
return renderClearButton();
}
return null;
};
var searchContent = /*#__PURE__*/jsx(BaseBox, {
position: "relative",
children: /*#__PURE__*/jsx(BaseInput, _objectSpread(_objectSpread({
id: "searchinput",
componentName: MetaConstants.SearchInput,
ref: mergedRef,
isDropdownTrigger: true,
setInputWrapperRef: isInsideDropdown || isTrailingDropDownOpen ? function (wrapperNode) {
triggererWrapperRef.current = wrapperNode;
} : undefined,
label: label,
accessibilityLabel: accessibilityLabel,
hideLabelText: !Boolean(label),
labelPosition: labelPosition,
labelSuffix: labelSuffix,
labelTrailing: labelTrailing,
placeholder: placeholder,
defaultValue: defaultValue,
value: value,
name: name,
onKeyDown: isInsideDropdown ? onTriggerKeydown : undefined,
onChange: function onChange(_ref2) {
var name = _ref2.name,
value = _ref2.value;
if (value !== null && value !== void 0 && value.length) {
// show the clear button when the user starts typing in
setShouldShowClearButton(true);
}
if (shouldShowClearButton && !(value !== null && value !== void 0 && value.length)) {
// hide the clear button when the input field is empty
setShouldShowClearButton(false);
}
_onChange === null || _onChange === void 0 || _onChange({
name: name,
value: value
});
},
onClick: function onClick(e) {
if (isDisabled) return;
if (isInsideDropdown) {
onTriggerClick();
}
_onClick === null || _onClick === void 0 || _onClick(e);
},
onFocus: function onFocus(e) {
setIsSearchFocused(true);
_onFocus === null || _onFocus === void 0 || _onFocus(e);
},
onBlur: function onBlur(e) {
setIsSearchFocused(false);
_onBlur === null || _onBlur === void 0 || _onBlur(e);
},
onSubmit: onSubmit,
isDisabled: isDisabled,
leadingIcon: showSearchIcon ? SearchIcon : undefined,
trailingInteractionElement: renderInteractionElement(),
trailingDropDown: renderTrailingDropDown(),
helpText: helpText
// eslint-disable-next-line jsx-a11y/no-autofocus
,
autoFocus: autoFocus,
testID: testID
}, getKeyboardAndAutocompleteProps({
type: 'search',
autoCapitalize: autoCapitalize
})), {}, {
size: size
}, rest))
});
return /*#__PURE__*/jsx(TopNavOverlayThemeOverride, {
shouldOverrideTheme: modalContext.isInsideModal ? true : isSearchFocused,
children: searchContent
});
};
var SearchInput = /*#__PURE__*/assignWithoutSideEffects(/*#__PURE__*/React__default.forwardRef(_SearchInput), {
displayName: 'SearchInput',
componentId: dropdownComponentIds.triggers.SearchInput
});
export { SearchInput };
//# sourceMappingURL=SearchInput.js.map