refactor select button

This commit is contained in:
Preet 2022-03-17 10:57:39 +05:30
parent 1e5d7153ea
commit bceae2fb4e
3 changed files with 63 additions and 60 deletions

View File

@ -0,0 +1,57 @@
import React, { memo } from "react";
import Icon, { IconSize } from "components/ads/Icon";
import { Button } from "@blueprintjs/core";
import { Colors } from "constants/Colors";
import { isNil } from "lodash";
import { isString } from "../../../utils/helpers";
import { StyledDiv } from "./index.styled";
export const isEmptyOrNill = (value: any) => {
return isNil(value) || (isString(value) && value === "");
};
interface SelectButtonProps {
disabled?: boolean;
displayText?: string;
handleCancelClick?: (event: React.MouseEvent<Element, MouseEvent>) => void;
togglePopoverVisibility: () => void;
value?: string;
}
function SelectButton(props: SelectButtonProps) {
const {
disabled,
displayText,
handleCancelClick,
togglePopoverVisibility,
value,
} = props;
return (
<Button
disabled={disabled}
onClick={togglePopoverVisibility}
rightIcon={
<StyledDiv>
{!isEmptyOrNill(value) ? (
<Icon
className="dropdown-icon cancel-icon"
fillColor={disabled ? Colors.GREY_7 : Colors.GREY_10}
name="cross"
onClick={handleCancelClick}
size={IconSize.XXS}
/>
) : null}
<Icon
className="dropdown-icon"
fillColor={disabled ? Colors.GREY_7 : Colors.GREY_10}
name="dropdown"
/>
</StyledDiv>
}
text={displayText}
/>
);
}
export default memo(SelectButton);

View File

@ -12,7 +12,7 @@ import {
BlueprintCSSTransform, BlueprintCSSTransform,
createGlobalStyle, createGlobalStyle,
} from "constants/DefaultTheme"; } from "constants/DefaultTheme";
import { isEmptyOrNill } from "."; import { isEmptyOrNill } from "./SelectButton";
export const TextLabelWrapper = styled.div<{ export const TextLabelWrapper = styled.div<{
compactMode: boolean; compactMode: boolean;

View File

@ -1,16 +1,14 @@
import React, { useMemo } from "react"; import React from "react";
import _ from "lodash";
import { ComponentProps } from "widgets/BaseComponent"; import { ComponentProps } from "widgets/BaseComponent";
import { Button, Classes } from "@blueprintjs/core"; import { Classes } from "@blueprintjs/core";
import { DropdownOption } from "../constants"; import { DropdownOption } from "../constants";
import { import {
IItemListRendererProps, IItemListRendererProps,
IItemRendererProps, IItemRendererProps,
} from "@blueprintjs/select"; } from "@blueprintjs/select";
import { debounce, findIndex, isEmpty, isNil } from "lodash"; import { debounce, findIndex, isEmpty, isNil, isNumber } from "lodash";
import "../../../../node_modules/@blueprintjs/select/lib/css/blueprint-select.css"; import "../../../../node_modules/@blueprintjs/select/lib/css/blueprint-select.css";
import { FixedSizeList } from "react-window"; import { FixedSizeList } from "react-window";
import { Colors } from "constants/Colors";
import { TextSize } from "constants/WidgetConstants"; import { TextSize } from "constants/WidgetConstants";
import { import {
StyledLabel, StyledLabel,
@ -20,12 +18,10 @@ import {
DropdownStyles, DropdownStyles,
DropdownContainer, DropdownContainer,
MenuItem, MenuItem,
StyledDiv,
} from "./index.styled"; } from "./index.styled";
import Fuse from "fuse.js"; import Fuse from "fuse.js";
import { WidgetContainerDiff } from "widgets/WidgetUtils"; import { WidgetContainerDiff } from "widgets/WidgetUtils";
import Icon, { IconSize } from "components/ads/Icon"; import SelectButton from "./SelectButton";
import { isString } from "../../../utils/helpers";
const FUSE_OPTIONS = { const FUSE_OPTIONS = {
shouldSort: true, shouldSort: true,
@ -36,10 +32,6 @@ const FUSE_OPTIONS = {
keys: ["label", "value"], keys: ["label", "value"],
}; };
export const isEmptyOrNill = (value: any) => {
return isNil(value) || (isString(value) && value === "");
};
const DEBOUNCE_TIMEOUT = 800; const DEBOUNCE_TIMEOUT = 800;
const ITEM_SIZE = 40; const ITEM_SIZE = 40;
@ -49,52 +41,6 @@ interface SelectComponentState {
isOpen?: boolean; isOpen?: boolean;
} }
interface SelectButtonProps {
disabled?: boolean;
displayText?: string;
handleCancelClick?: (event: React.MouseEvent<Element, MouseEvent>) => void;
togglePopoverVisibility: () => void;
value?: string;
}
function SelectButton(props: SelectButtonProps) {
const {
disabled,
displayText,
handleCancelClick,
togglePopoverVisibility,
value,
} = props;
return useMemo(
() => (
<Button
disabled={disabled}
onClick={togglePopoverVisibility}
rightIcon={
<StyledDiv>
{!isEmptyOrNill(value) ? (
<Icon
className="dropdown-icon cancel-icon"
fillColor={disabled ? Colors.GREY_7 : Colors.GREY_10}
name="cross"
onClick={handleCancelClick}
size={IconSize.XXS}
/>
) : null}
<Icon
className="dropdown-icon"
fillColor={disabled ? Colors.GREY_7 : Colors.GREY_10}
name="dropdown"
/>
</StyledDiv>
}
text={displayText}
/>
),
[disabled, displayText, handleCancelClick, value],
);
}
class SelectComponent extends React.Component< class SelectComponent extends React.Component<
SelectComponentProps, SelectComponentProps,
SelectComponentState SelectComponentState
@ -246,7 +192,7 @@ class SelectComponent extends React.Component<
<FixedSizeList <FixedSizeList
height={300} height={300}
initialScrollOffset={ initialScrollOffset={
_.isNumber(activeItemIndex) ? activeItemIndex * ITEM_SIZE : 0 isNumber(activeItemIndex) ? activeItemIndex * ITEM_SIZE : 0
} }
itemCount={items.length} itemCount={items.length}
itemSize={ITEM_SIZE} itemSize={ITEM_SIZE}