Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TS migration] Migrate 'BaseMiniContextMenuItem.js' component to TypeScript #31053

Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import PropTypes from 'prop-types';
import React from 'react';
import React, {ForwardedRef } from 'react';
import {View} from 'react-native';
BartoszGrajdek marked this conversation as resolved.
Show resolved Hide resolved
import _ from 'underscore';
import DomUtils from '@libs/DomUtils';
import getButtonState from '@libs/getButtonState';
import ReportActionComposeFocusManager from '@libs/ReportActionComposeFocusManager';
Expand All @@ -11,53 +9,41 @@ import variables from '@styles/variables';
import PressableWithoutFeedback from './Pressable/PressableWithoutFeedback';
import Tooltip from './Tooltip/PopoverAnchorTooltip';

const propTypes = {
type BaseMiniContextMenuItemProps = {
/**
* Text to display when hovering the menu item
*/
tooltipText: PropTypes.string.isRequired,
tooltipText: string;

/**
* Callback to fire on press
*/
onPress: PropTypes.func.isRequired,
onPress: () => void;

/**
* The children to display within the menu item
*/
children: PropTypes.oneOfType([PropTypes.func, PropTypes.node]).isRequired,
children: React.ReactNode | ((state: unknown) => React.ReactNode);

BartoszGrajdek marked this conversation as resolved.
Show resolved Hide resolved
/**
* Whether the button should be in the active state
*/
isDelayButtonStateComplete: PropTypes.bool,

/**
* A ref to forward to the Pressable
*/
innerRef: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
};

const defaultProps = {
isDelayButtonStateComplete: true,
innerRef: () => {},
isDelayButtonStateComplete: boolean,
};

/**
* Component that renders a mini context menu item with a
* pressable. Also renders a tooltip when hovering the item.
* @param {Object} props
* @returns {JSX.Element}
*/
function BaseMiniContextMenuItem(props) {
function BaseMiniContextMenuItem({tooltipText, onPress, children, isDelayButtonStateComplete = true}: BaseMiniContextMenuItemProps, ref: ForwardedRef<View>) {
return (
<Tooltip text={props.tooltipText}>
<Tooltip text={tooltipText} shouldRender>
<PressableWithoutFeedback
ref={props.innerRef}
onPress={props.onPress}
ref={ref}
onPress={onPress}
onMouseDown={(e) => {
if (!ReportActionComposeFocusManager.isFocused() && !ReportActionComposeFocusManager.isEditFocused()) {
BartoszGrajdek marked this conversation as resolved.
Show resolved Hide resolved
DomUtils.getActiveElement().blur();
DomUtils?.getActiveElement()?.blur();
return;
}

BartoszGrajdek marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -69,35 +55,23 @@ function BaseMiniContextMenuItem(props) {
// Prevent text input blur on left click
e.preventDefault();
}}
accessibilityLabel={props.tooltipText}
accessibilityLabel={tooltipText}
style={({hovered, pressed}) => [
styles.reportActionContextMenuMiniButton,
StyleUtils.getButtonBackgroundColorStyle(getButtonState(hovered, pressed, props.isDelayButtonStateComplete)),
props.isDelayButtonStateComplete && styles.cursorDefault,
StyleUtils.getButtonBackgroundColorStyle(getButtonState(hovered, pressed, isDelayButtonStateComplete)),
isDelayButtonStateComplete && styles.cursorDefault,
]}
>
{(pressableState) => (
<View style={[StyleUtils.getWidthAndHeightStyle(variables.iconSizeNormal), styles.alignItemsCenter, styles.justifyContentCenter]}>
{_.isFunction(props.children) ? props.children(pressableState) : props.children}
{typeof children === 'function' ? children(pressableState) : children}
</View>
)}
</PressableWithoutFeedback>
</Tooltip>
);
}

BaseMiniContextMenuItem.propTypes = propTypes;
BaseMiniContextMenuItem.defaultProps = defaultProps;
BaseMiniContextMenuItem.displayName = 'BaseMiniContextMenuItem';

const BaseMiniContextMenuItemWithRef = React.forwardRef((props, ref) => (
<BaseMiniContextMenuItem
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
innerRef={ref}
/>
));

BaseMiniContextMenuItemWithRef.displayName = 'BaseMiniContextMenuItemWithRef';

export default BaseMiniContextMenuItemWithRef;
export default React.forwardRef(BaseMiniContextMenuItem);
2 changes: 1 addition & 1 deletion src/libs/DomUtils/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import GetActiveElement from './types';

const getActiveElement: GetActiveElement = () => document.activeElement;
const getActiveElement: GetActiveElement = () => (document.activeElement as HTMLElement);
BartoszGrajdek marked this conversation as resolved.
Show resolved Hide resolved

export default {
getActiveElement,
Expand Down
2 changes: 1 addition & 1 deletion src/libs/DomUtils/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
type GetActiveElement = () => Element | null;
type GetActiveElement = () => HTMLElement | null;

export default GetActiveElement;
Loading