PromucFlow_constructor/app/client/src/widgets/SelectWidget/component/index.tsx
Ivan Akulov 424d2f6965
chore: upgrade to prettier v2 + enforce import types (#21013)Co-authored-by: Satish Gandham <hello@satishgandham.com> Co-authored-by: Satish Gandham <satish.iitg@gmail.com>
## Description

This PR upgrades Prettier to v2 + enforces TypeScript’s [`import
type`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html#type-only-imports-and-export)
syntax where applicable. It’s submitted as a separate PR so we can merge
it easily.

As a part of this PR, we reformat the codebase heavily:
- add `import type` everywhere where it’s required, and
- re-format the code to account for Prettier 2’s breaking changes:
https://prettier.io/blog/2020/03/21/2.0.0.html#breaking-changes

This PR is submitted against `release` to make sure all new code by team
members will adhere to new formatting standards, and we’ll have fewer
conflicts when merging `bundle-optimizations` into `release`. (I’ll
merge `release` back into `bundle-optimizations` once this PR is
merged.)

### Why is this needed?

This PR is needed because, for the Lodash optimization from
7cbb12af88,
we need to use `import type`. Otherwise, `babel-plugin-lodash` complains
that `LoDashStatic` is not a lodash function.

However, just using `import type` in the current codebase will give you
this:

<img width="962" alt="Screenshot 2023-03-08 at 17 45 59"
src="https://user-images.githubusercontent.com/2953267/223775744-407afa0c-e8b9-44a1-90f9-b879348da57f.png">

That’s because Prettier 1 can’t parse `import type` at all. To parse it,
we need to upgrade to Prettier 2.

### Why enforce `import type`?

Apart from just enabling `import type` support, this PR enforces
specifying `import type` everywhere it’s needed. (Developers will get
immediate TypeScript and ESLint errors when they forget to do so.)

I’m doing this because I believe `import type` improves DX and makes
refactorings easier.

Let’s say you had a few imports like below. Can you tell which of these
imports will increase the bundle size? (Tip: it’s not all of them!)

```ts
// app/client/src/workers/Linting/utils.ts
import { Position } from "codemirror";
import { LintError as JSHintError, LintOptions } from "jshint";
import { get, isEmpty, isNumber, keys, last, set } from "lodash";
```

It’s pretty hard, right?

What about now?

```ts
// app/client/src/workers/Linting/utils.ts
import type { Position } from "codemirror";
import type { LintError as JSHintError, LintOptions } from "jshint";
import { get, isEmpty, isNumber, keys, last, set } from "lodash";
```

Now, it’s clear that only `lodash` will be bundled.

This helps developers to see which imports are problematic, but it
_also_ helps with refactorings. Now, if you want to see where
`codemirror` is bundled, you can just grep for `import \{.*\} from
"codemirror"` – and you won’t get any type-only imports.

This also helps (some) bundlers. Upon transpiling, TypeScript erases
type-only imports completely. In some environment (not ours), this makes
the bundle smaller, as the bundler doesn’t need to bundle type-only
imports anymore.

## Type of change

- Chore (housekeeping or task changes that don't impact user perception)


## How Has This Been Tested?

This was tested to not break the build.

### Test Plan
> Add Testsmith test cases links that relate to this PR

### Issues raised during DP testing
> Link issues raised during DP testing for better visiblity and tracking
(copy link from comments dropped on this PR)


## Checklist:
### Dev activity
- [x] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] PR is being merged under a feature flag


### QA activity:
- [ ] Test plan has been approved by relevant developers
- [ ] Test plan has been peer reviewed by QA
- [ ] Cypress test cases have been added and approved by either SDET or
manual QA
- [ ] Organized project review call with relevant stakeholders after
Round 1/2 of QA
- [ ] Added Test Plan Approved label after reveiwing all Cypress test

---------

Co-authored-by: Satish Gandham <hello@satishgandham.com>
Co-authored-by: Satish Gandham <satish.iitg@gmail.com>
2023-03-16 17:11:47 +05:30

471 lines
15 KiB
TypeScript

import React from "react";
import type { ComponentProps } from "widgets/BaseComponent";
import type { Alignment } from "@blueprintjs/core";
import { Classes } from "@blueprintjs/core";
import type { DropdownOption } from "../constants";
import type {
IItemListRendererProps,
IItemRendererProps,
} from "@blueprintjs/select";
import { debounce, findIndex, isEmpty, isNil, isNumber } from "lodash";
import equal from "fast-deep-equal/es6";
import "../../../../node_modules/@blueprintjs/select/lib/css/blueprint-select.css";
import { FixedSizeList } from "react-window";
import type { TextSize } from "constants/WidgetConstants";
import {
StyledControlGroup,
StyledSingleDropDown,
DropdownStyles,
DropdownContainer,
MenuItem,
} from "./index.styled";
import { WidgetContainerDiff } from "widgets/WidgetUtils";
import type { LabelPosition } from "components/constants";
import SelectButton from "./SelectButton";
import { labelMargin } from "../../WidgetUtils";
import LabelWithTooltip from "widgets/components/LabelWithTooltip";
const DEBOUNCE_TIMEOUT = 800;
const ITEM_SIZE = 40;
const MAX_RENDER_MENU_ITEMS_HEIGHT = 300;
interface SelectComponentState {
activeItemIndex: number | undefined;
isOpen?: boolean;
}
class SelectComponent extends React.Component<
SelectComponentProps,
SelectComponentState
> {
listRef: any = React.createRef();
labelRef = React.createRef<HTMLDivElement>();
spanRef = React.createRef<HTMLSpanElement>();
state = {
// used to show focused item for keyboard up down key interection
activeItemIndex: -1,
isOpen: false,
};
componentDidMount = () => {
const newState: SelectComponentState = {
activeItemIndex: this.props.selectedIndex,
};
if (this.props.isOpen) {
newState.isOpen = this.props.isOpen;
}
// set default selectedIndex as focused index
this.setState(newState);
};
componentDidUpdate = (prevProps: SelectComponentProps) => {
if (
prevProps.selectedIndex !== this.props.selectedIndex &&
this.state.activeItemIndex !== this.props.selectedIndex
) {
// update focus index if selectedIndex changed by property pane
this.setState({ activeItemIndex: this.props.selectedIndex });
}
};
togglePopoverVisibility = () => {
if (this.state.isOpen) {
this.handleOnDropdownClose();
} else {
this.handleOnDropdownOpen();
}
this.setState({ isOpen: !this.state.isOpen });
};
handleActiveItemChange = (activeItem: DropdownOption | null) => {
// Update state.activeItemIndex if activeItem is different from the current value
if (
activeItem?.value !==
this.props?.options[this.state.activeItemIndex]?.value
) {
// find new index from options
const activeItemIndex = findIndex(this.props.options, [
"label",
activeItem?.label,
]);
this.setState({ activeItemIndex });
}
};
itemListPredicate(query: string, items: DropdownOption[]) {
if (!query) return items;
const filter = items.filter(
(item) =>
item.label?.toString().toLowerCase().includes(query.toLowerCase()) ||
String(item.value).toLowerCase().includes(query.toLowerCase()),
);
return filter;
}
onItemSelect = (item: DropdownOption): void => {
this.props.onOptionSelected(item);
// If Popover is open, then toggle visibility.
// Required when item selection is made via keyboard input.
if (this.state.isOpen) this.togglePopoverVisibility();
};
isOptionSelected = (currentOption: DropdownOption) => {
// if currentOption is null, then return false
if (isNil(currentOption)) return false;
if (this.props.value) return currentOption.value === this.props.value;
const optionIndex = findIndex(this.props.options, (option) => {
return option.value === currentOption.value;
});
return optionIndex === this.props.selectedIndex;
};
onQueryChange = debounce((filterValue: string) => {
if (equal(filterValue, this.props.filterText)) return;
this.props.onFilterChange(filterValue);
this.listRef?.current?.scrollTo(0);
}, DEBOUNCE_TIMEOUT);
renderSingleSelectItem = (
option: DropdownOption,
itemProps: IItemRendererProps,
) => {
if (!this.state.isOpen) return null;
if (!itemProps.modifiers.matchesPredicate) {
return null;
}
const isSelected: boolean = this.isOptionSelected(option);
// For tabbable menuItems
const isFocused = itemProps.modifiers.active;
const focusClassName = `${isFocused && "has-focus"}`;
const selectedClassName = `${isSelected && "menu-item-active"}`;
return (
<MenuItem
accentColor={this.props.accentColor}
key={option.value}
onClick={itemProps.handleClick}
>
<a
className={`menu-item-link ${selectedClassName} ${focusClassName}`}
tabIndex={0}
>
<div className="menu-item-text">{option.label}</div>
</a>
</MenuItem>
);
};
handleCancelClick = (event: React.MouseEvent<Element, MouseEvent>) => {
event.stopPropagation();
this.onItemSelect({});
};
handleOnDropdownOpen = () => {
if (!this.state.isOpen && this.props.onDropdownOpen) {
this.props.onDropdownOpen();
}
};
handleOnDropdownClose = () => {
if (this.state.isOpen && this.props.onDropdownClose) {
this.props.onDropdownClose();
}
};
handleCloseList = () => {
if (this.state.isOpen) {
this.togglePopoverVisibility();
if (!this.props.selectedIndex) return;
return this.handleActiveItemChange(
this.props.options[this.props.selectedIndex],
);
} else {
this.handleOnDropdownClose();
/**
* Clear the search input on closing the widget
* and when serverSideFiltering is off
*/
if (this.props.resetFilterTextOnClose && this.props.filterText?.length) {
this.onQueryChange("");
}
if (this.props.onClose) {
this.props.onClose();
}
}
};
noResultsUI = (
<MenuItem accentColor={this.props.accentColor}>
<a className="menu-item-link">
<div className="menu-item-text">No Results Found</div>
</a>
</MenuItem>
);
itemListRenderer = (
props: IItemListRendererProps<any>,
): JSX.Element | null => {
if (!this.state.isOpen) return null;
let activeItemIndex = this.props.selectedIndex || null;
if (props.activeItem && activeItemIndex === null) {
activeItemIndex = props.filteredItems?.findIndex(
(item) => item.value === props.activeItem?.value,
);
}
if (!props.filteredItems || !props.filteredItems.length)
return this.noResultsUI;
return this.renderList(
props.filteredItems,
activeItemIndex,
props.renderItem,
);
};
menuListStyle = { height: "auto", maxHeight: MAX_RENDER_MENU_ITEMS_HEIGHT };
renderList = (
items: DropdownOption[],
activeItemIndex: number | null,
renderItem: (item: any, index: number) => JSX.Element | null,
): JSX.Element | null => {
// Don't scroll if the list is filtered.
const optionsCount = this.props.options.length;
const scrollOffset: number =
!this.props.filterText &&
isNumber(activeItemIndex) &&
optionsCount * ITEM_SIZE > MAX_RENDER_MENU_ITEMS_HEIGHT
? activeItemIndex * ITEM_SIZE
: 0;
const RowRenderer = (itemProps: any) => (
<div key={itemProps.index} style={itemProps.style}>
{renderItem(items[itemProps.index], itemProps.index)}
</div>
);
return (
<FixedSizeList
className="menu-virtual-list"
height={MAX_RENDER_MENU_ITEMS_HEIGHT}
initialScrollOffset={scrollOffset}
itemCount={items.length}
itemSize={ITEM_SIZE}
ref={this.listRef}
style={this.menuListStyle}
width={"100%"}
>
{RowRenderer}
</FixedSizeList>
);
};
getDropdownWidth = () => {
const parentWidth = this.props.width - WidgetContainerDiff;
if (this.props.compactMode && this.labelRef.current) {
const labelWidth = this.labelRef.current.getBoundingClientRect().width;
const widthDiff = parentWidth - labelWidth - labelMargin;
return widthDiff > this.props.dropDownWidth
? widthDiff
: this.props.dropDownWidth;
}
return parentWidth > this.props.dropDownWidth
? parentWidth
: this.props.dropDownWidth;
};
render() {
const {
accentColor,
borderRadius,
boxShadow,
compactMode,
disabled,
isDynamicHeightEnabled,
isLoading,
labelAlignment,
labelPosition,
labelStyle,
labelText,
labelTextColor,
labelTextSize,
labelTooltip,
labelWidth,
widgetId,
} = this.props;
// active focused item
const activeItem = () => {
if (
this.state.activeItemIndex === -1 ||
isNil(this.state.activeItemIndex)
)
return undefined;
if (!isEmpty(this.props.options))
return this.props.options[this.state.activeItemIndex];
};
// get selected option label from selectedIndex
const selectedOption =
!isEmpty(this.props.options) &&
this.props.selectedIndex !== undefined &&
this.props.selectedIndex > -1
? this.props.options[this.props.selectedIndex].label
: this.props.label;
// for display selected option, there is no separate option to show placeholder
const value =
!isNil(selectedOption) && selectedOption !== ""
? selectedOption
: this.props.placeholder || "-- Select --";
// Check if text overflows
const tooltipText: string =
this.spanRef.current?.parentElement &&
(this.spanRef.current.parentElement.offsetHeight <
this.spanRef.current.parentElement.scrollHeight ||
this.spanRef.current.parentElement.offsetWidth <
this.spanRef.current.parentElement.scrollWidth)
? value.toString()
: "";
return (
<DropdownContainer
className={this.props.className}
compactMode={compactMode}
data-testid="select-container"
labelPosition={labelPosition}
>
<DropdownStyles
accentColor={accentColor}
borderRadius={borderRadius}
dropDownWidth={this.getDropdownWidth()}
id={widgetId}
/>
{labelText && (
<LabelWithTooltip
alignment={labelAlignment}
className={`select-label`}
color={labelTextColor}
compact={compactMode}
cyHelpTextClassName="select-tooltip"
disabled={disabled}
fontSize={labelTextSize}
fontStyle={labelStyle}
helpText={labelTooltip}
isDynamicHeightEnabled={isDynamicHeightEnabled}
loading={isLoading}
position={labelPosition}
ref={this.labelRef}
text={labelText}
width={labelWidth}
/>
)}
<StyledControlGroup
$compactMode={compactMode}
$isDisabled={disabled}
$labelPosition={labelPosition}
fill
>
<StyledSingleDropDown
accentColor={accentColor}
activeItem={activeItem()}
borderRadius={borderRadius}
boxShadow={boxShadow}
className={isLoading ? Classes.SKELETON : ""}
disabled={disabled}
filterable={this.props.isFilterable}
hasError={this.props.hasError}
isValid={this.props.isValid}
itemListPredicate={
!this.props.serverSideFiltering
? this.itemListPredicate
: undefined
}
itemListRenderer={this.itemListRenderer}
itemRenderer={this.renderSingleSelectItem}
items={this.props.options}
noResults={this.noResultsUI}
onActiveItemChange={this.handleActiveItemChange}
onItemSelect={this.onItemSelect}
onQueryChange={this.onQueryChange}
popoverProps={{
portalContainer:
document.getElementById("art-board") || undefined,
boundary: "window",
isOpen: this.state.isOpen,
minimal: true,
usePortal: true,
onClose: this.handleCloseList,
// onActiveItemChange is called twice abd puts the focus on the first item https://github.com/palantir/blueprint/issues/4192
onOpening: () => {
if (!this.props.selectedIndex) {
return this.handleActiveItemChange(null);
}
return this.handleActiveItemChange(
this.props.options[this.props.selectedIndex],
);
},
modifiers: {
preventOverflow: {
enabled: false,
},
},
popoverClassName: `select-popover-wrapper select-popover-width-${this.props.widgetId}`,
}}
query={this.props.filterText}
resetOnClose={this.props.resetFilterTextOnClose}
scrollToActiveItem
value={this.props.value as string}
>
<SelectButton
disabled={disabled}
displayText={value.toString()}
handleCancelClick={this.handleCancelClick}
hideCancelIcon={this.props.hideCancelIcon}
spanRef={this.spanRef}
togglePopoverVisibility={this.togglePopoverVisibility}
tooltipText={tooltipText}
value={this.props.value?.toString()}
/>
</StyledSingleDropDown>
</StyledControlGroup>
</DropdownContainer>
);
}
}
export interface SelectComponentProps extends ComponentProps {
className?: string;
disabled?: boolean;
onOptionSelected: (optionSelected: DropdownOption) => void;
placeholder?: string;
labelAlignment?: Alignment;
labelPosition?: LabelPosition;
labelText: string;
labelTextColor?: string;
labelTextSize?: TextSize;
labelStyle?: string;
labelWidth?: number;
labelTooltip?: string;
compactMode: boolean;
selectedIndex?: number;
options: DropdownOption[];
isDynamicHeightEnabled?: boolean;
isLoading: boolean;
isFilterable: boolean;
isValid: boolean;
width: number;
dropDownWidth: number;
height: number;
serverSideFiltering: boolean;
hasError?: boolean;
onFilterChange: (text: string) => void;
onDropdownOpen?: () => void;
onDropdownClose?: () => void;
value?: string | number;
label?: string | number;
filterText?: string;
borderRadius: string;
boxShadow?: string;
accentColor?: string;
isOpen?: boolean;
onClose?: () => void;
hideCancelIcon?: boolean;
resetFilterTextOnClose?: boolean;
}
export default React.memo(SelectComponent);