PromucFlow_constructor/app/client/src/components/editorComponents/DropdownComponent.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

252 lines
7.5 KiB
TypeScript

import type { ReactNode } from "react";
import React, { Component } from "react";
import styled from "styled-components";
import type { IMenuProps } from "@blueprintjs/core";
import { MenuItem, Menu, ControlGroup, InputGroup } from "@blueprintjs/core";
import { BaseButton } from "components/designSystems/appsmith/BaseButton";
import type {
ItemRenderer,
ItemListRenderer,
IItemListRendererProps,
} from "@blueprintjs/select";
import { Select } from "@blueprintjs/select";
import type { DropdownOption } from "components/constants";
import { ButtonVariantTypes } from "components/constants";
import type { WrappedFieldInputProps } from "redux-form";
interface ButtonWrapperProps {
height?: string;
width?: string;
}
interface MenuProps {
width?: string;
}
type MenuComponentProps = IMenuProps & MenuProps;
const Dropdown = Select.ofType<DropdownOption>();
const StyledButtonWrapper = styled.div<ButtonWrapperProps>`
width: ${(props) => props.width || "100%"};
height: ${(props) => props.height || "100%"};
button.bp3-button {
border: 1px solid ${(props) => props.theme.colors.border} !important;
background: #fff !important;
& > span {
color: ${(props) => props.theme.colors.dropdown.header.text} !important;
}
font-weight: ${(props) => props.theme.fontWeights[1]};
}
`;
const StyledMenu = styled(Menu)<MenuComponentProps>`
min-width: ${(props) => props.width || "100%"};
border-radius: 0;
`;
const StyledMenuItem = styled(MenuItem)`
border-radius: 0;
color: ${(props) => props.theme.colors.dropdown.header.text};
&&&:hover {
color: ${(props) => props.theme.colors.dropdown.menu.hoverText};
background: ${(props) => props.theme.colors.dropdown.menu.hover};
}
&&&.bp3-active {
color: ${(props) => props.theme.colors.dropdown.selected.text};
background: ${(props) => props.theme.colors.dropdown.selected.bg};
}
`;
// function checks if dropdown is connected to a redux form (of interface 'FormDropdownComponentProps')
const isFormDropdown = (
props: DropdownComponentProps | FormDropdownComponentProps,
): props is FormDropdownComponentProps => {
return "input" in props && props.input !== undefined;
};
class DropdownComponent extends Component<
DropdownComponentProps | FormDropdownComponentProps
> {
private newItemTextInput: HTMLInputElement | null = null;
private setNewItemTextInput = (element: HTMLInputElement | null) => {
this.newItemTextInput = element;
};
public state = {
isEditing: false,
};
showTextBox = (): void => {
this.setState({
isEditing: true,
});
};
handleAddItem = (): void => {
this.props.addItem &&
this.newItemTextInput &&
this.props.addItem.addItemHandler(this.newItemTextInput.value);
this.setState({
isEditing: false,
});
};
renderItemList: ItemListRenderer<DropdownOption> = (
props: IItemListRendererProps<DropdownOption>,
) => {
const { items, renderItem } = props;
const { addItem, width } = this.props;
const renderItems = items.map(renderItem).filter(Boolean);
const displayMode = (
<BaseButton
buttonStyle="PRIMARY"
icon-right="plus"
onClick={this.showTextBox}
text={addItem?.displayText}
/>
);
const editMode = (
<ControlGroup fill>
<InputGroup inputRef={this.setNewItemTextInput} />
<BaseButton onClick={this.handleAddItem} text={addItem?.displayText} />
</ControlGroup>
);
return (
<StyledMenu ulRef={props.itemsParentRef} width={width}>
{renderItems}
{addItem && (!this.state.isEditing ? displayMode : editMode)}
</StyledMenu>
);
};
searchItem = (query: string, option: DropdownOption): boolean => {
return (
option.label.toLowerCase().indexOf(query.toLowerCase()) > -1 ||
option.value.toLowerCase().indexOf(query.toLowerCase()) > -1 ||
(!!option.label &&
option.label.toLowerCase().indexOf(query.toLowerCase()) > -1)
);
};
// function is called after user selects an option
onItemSelect = (item: DropdownOption): void => {
if (isFormDropdown(this.props)) {
this.props.input.onChange(item.value);
} else {
this.props.selectHandler(item.value);
}
};
renderItem: ItemRenderer<DropdownOption> = (
option: DropdownOption,
{ handleClick, modifiers },
) => {
if (!modifiers.matchesPredicate) {
return null;
}
return (
<StyledMenuItem
active={modifiers.active}
key={option.value}
label={this.props.hasLabel ? option.label : ""}
onClick={handleClick}
shouldDismissPopover={false}
text={option.label}
/>
);
};
// helper function that returns a dropdown option given its value
// returns undefined if option isn't found
getDropdownOption = (
value: string | undefined,
): DropdownOption | undefined => {
return this.props.options.find((option) => option.value === value);
};
// this function returns the selected item's label
// returns the "placeholder" in the event that no option is selected.
getSelectedDisplayText = () => {
const value = isFormDropdown(this.props)
? this.props.input.value
: this.props.selected?.value;
const item = this.getDropdownOption(value);
return item ? item.label : this.props.placeholder;
};
// this function returns the active option
// returns undefined if no option is selected
getActiveOption = (): DropdownOption | undefined => {
if (isFormDropdown(this.props)) {
return this.getDropdownOption(this.props.input.value);
} else {
return this.props.selected;
}
};
render() {
const { autocomplete, height, options, width } = this.props;
return (
<Dropdown
activeItem={this.getActiveOption()}
filterable={!!autocomplete}
itemListRenderer={this.renderItemList}
itemPredicate={this.searchItem}
itemRenderer={this.renderItem}
items={options}
itemsEqual="value"
noResults={<MenuItem disabled text="No results." />}
onItemSelect={this.onItemSelect}
popoverProps={{ minimal: true }}
// Destructure the "input" prop if dropdown is form-connected
{...(isFormDropdown(this.props) ? this.props.input : {})}
>
{this.props.toggle || (
<StyledButtonWrapper height={height} width={width}>
<BaseButton
buttonStyle="PRIMARY"
buttonVariant={ButtonVariantTypes.SECONDARY}
rightIcon="chevron-down"
text={this.getSelectedDisplayText()}
/>
</StyledButtonWrapper>
)}
</Dropdown>
);
}
}
// Dropdown can either be connected to a redux-form
// or be a stand-alone component
// Props common to both classes of dropdowns
export interface BaseDropdownComponentProps {
addItem?: {
displayText: string;
addItemHandler: (name: string) => void;
};
autocomplete?: boolean;
checked?: boolean;
hasLabel?: boolean;
height?: string;
multi?: boolean;
multiselectDisplayType?: "TAGS" | "CHECKBOXES";
options: DropdownOption[];
placeholder: string;
toggle?: ReactNode;
width?: string;
}
// stand-alone dropdown interface
export interface DropdownComponentProps extends BaseDropdownComponentProps {
selectHandler: (selectedValue: string) => void;
selected: DropdownOption | undefined;
}
// Form-connected dropdown interface
export interface FormDropdownComponentProps extends BaseDropdownComponentProps {
input: WrappedFieldInputProps;
}
export default DropdownComponent;