2021-07-13 08:05:09 +00:00
|
|
|
import * as React from "react";
|
|
|
|
|
import styled, { createGlobalStyle } from "styled-components";
|
2022-03-04 06:45:50 +00:00
|
|
|
import { Alignment, Button, Classes, MenuItem } from "@blueprintjs/core";
|
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
https://github.com/appsmithorg/appsmith/commit/7cbb12af886621256224be0c93e6a465dd710ad3,
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 11:41:47 +00:00
|
|
|
import type { IconName } from "@blueprintjs/icons";
|
|
|
|
|
import { IconNames } from "@blueprintjs/icons";
|
|
|
|
|
import type { ItemListRenderer, ItemRenderer } from "@blueprintjs/select";
|
|
|
|
|
import { Select } from "@blueprintjs/select";
|
|
|
|
|
import type { GridListProps, VirtuosoGridHandle } from "react-virtuoso";
|
|
|
|
|
import { VirtuosoGrid } from "react-virtuoso";
|
2021-07-13 08:05:09 +00:00
|
|
|
|
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
https://github.com/appsmithorg/appsmith/commit/7cbb12af886621256224be0c93e6a465dd710ad3,
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 11:41:47 +00:00
|
|
|
import type { ControlProps } from "./BaseControl";
|
|
|
|
|
import BaseControl from "./BaseControl";
|
2021-12-07 09:45:18 +00:00
|
|
|
import { replayHighlightClass } from "globalStyles/portals";
|
2022-03-04 06:45:50 +00:00
|
|
|
import _ from "lodash";
|
2022-05-17 06:55:25 +00:00
|
|
|
import { generateReactKey } from "utils/generators";
|
2022-07-14 05:00:30 +00:00
|
|
|
import { emitInteractionAnalyticsEvent } from "utils/AppsmithUtils";
|
2023-05-19 18:37:06 +00:00
|
|
|
import { Tooltip } from "design-system";
|
2021-07-13 08:05:09 +00:00
|
|
|
|
|
|
|
|
const IconSelectContainerStyles = createGlobalStyle<{
|
|
|
|
|
targetWidth: number | undefined;
|
2022-05-17 06:55:25 +00:00
|
|
|
id: string;
|
2021-07-13 08:05:09 +00:00
|
|
|
}>`
|
2022-05-17 06:55:25 +00:00
|
|
|
${({ id, targetWidth }) => `
|
|
|
|
|
.icon-select-popover-${id} {
|
|
|
|
|
width: ${targetWidth}px;
|
|
|
|
|
background: white;
|
|
|
|
|
|
|
|
|
|
.bp3-input-group {
|
|
|
|
|
margin: 5px !important;
|
|
|
|
|
}
|
2021-07-13 08:05:09 +00:00
|
|
|
}
|
2023-05-19 18:37:06 +00:00
|
|
|
.bp3-button-text {
|
|
|
|
|
color: var(--ads-v2-color-fg) !important;
|
|
|
|
|
}
|
|
|
|
|
.bp3-icon {
|
|
|
|
|
color: var(--ads-v2-color-fg) !important;
|
|
|
|
|
}
|
2022-05-17 06:55:25 +00:00
|
|
|
`}
|
2021-07-13 08:05:09 +00:00
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const StyledButton = styled(Button)`
|
|
|
|
|
box-shadow: none !important;
|
2023-05-19 18:37:06 +00:00
|
|
|
border: 1px solid var(--ads-v2-color-border);
|
|
|
|
|
border-radius: var(--ads-v2-border-radius);
|
2021-10-04 15:34:37 +00:00
|
|
|
height: 36px;
|
2021-07-13 08:05:09 +00:00
|
|
|
background-color: #ffffff !important;
|
|
|
|
|
> span.bp3-icon-caret-down {
|
|
|
|
|
color: rgb(169, 167, 167);
|
|
|
|
|
}
|
2021-12-24 07:25:25 +00:00
|
|
|
|
2023-05-19 18:37:06 +00:00
|
|
|
&:hover {
|
|
|
|
|
border: 1px solid var(--ads-v2-color-border-emphasis);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-24 07:25:25 +00:00
|
|
|
&:focus {
|
2023-05-19 18:37:06 +00:00
|
|
|
outline: var(--ads-v2-border-width-outline) solid
|
|
|
|
|
var(--ads-v2-color-outline);
|
|
|
|
|
border: 1px solid var(--ads-v2-color-border-emphasis);
|
2021-12-24 07:25:25 +00:00
|
|
|
}
|
2021-07-13 08:05:09 +00:00
|
|
|
`;
|
|
|
|
|
|
2022-03-04 06:45:50 +00:00
|
|
|
const StyledMenu = styled.ul<GridListProps>`
|
2021-07-13 08:05:09 +00:00
|
|
|
display: grid;
|
|
|
|
|
grid-template-columns: repeat(4, 1fr);
|
|
|
|
|
grid-auto-rows: minmax(50px, auto);
|
|
|
|
|
gap: 8px;
|
|
|
|
|
max-height: 170px !important;
|
|
|
|
|
padding-left: 5px !important;
|
|
|
|
|
padding-right: 5px !important;
|
2022-03-04 06:45:50 +00:00
|
|
|
& li {
|
|
|
|
|
list-style: none;
|
|
|
|
|
}
|
2021-07-13 08:05:09 +00:00
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const StyledMenuItem = styled(MenuItem)`
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
align-items: center;
|
|
|
|
|
padding: 13px 5px;
|
2023-05-19 18:37:06 +00:00
|
|
|
|
2021-07-13 08:05:09 +00:00
|
|
|
&:active,
|
|
|
|
|
&.bp3-active {
|
2023-05-19 18:37:06 +00:00
|
|
|
background-color: var(--ads-v2-color-bg-muted) !important;
|
|
|
|
|
border-radius: var(--ads-v2-border-radius) !important;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
&:hover {
|
|
|
|
|
background-color: var(--ads-v2-color-bg-subtle) !important;
|
|
|
|
|
border-radius: var(--ads-v2-border-radius) !important;
|
2021-07-13 08:05:09 +00:00
|
|
|
}
|
2023-05-19 18:37:06 +00:00
|
|
|
|
2021-07-13 08:05:09 +00:00
|
|
|
> span.bp3-icon {
|
|
|
|
|
margin-right: 0;
|
2023-05-19 18:37:06 +00:00
|
|
|
color: var(--ads-v2-color-fg) !important;
|
2021-07-13 08:05:09 +00:00
|
|
|
}
|
2023-05-19 18:37:06 +00:00
|
|
|
|
2021-07-13 08:05:09 +00:00
|
|
|
> div {
|
|
|
|
|
width: 100%;
|
|
|
|
|
text-align: center;
|
2023-05-19 18:37:06 +00:00
|
|
|
color: var(--ads-v2-color-fg) !important;
|
2021-07-13 08:05:09 +00:00
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
export interface IconSelectControlProps extends ControlProps {
|
|
|
|
|
propertyValue?: IconName;
|
2021-11-16 10:42:20 +00:00
|
|
|
defaultIconName?: IconName;
|
2023-09-18 13:07:03 +00:00
|
|
|
hideNoneIcon?: boolean;
|
2021-07-13 08:05:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface IconSelectControlState {
|
2022-03-04 06:45:50 +00:00
|
|
|
activeIcon: IconType;
|
|
|
|
|
isOpen: boolean;
|
2021-07-13 08:05:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const NONE = "(none)";
|
|
|
|
|
type IconType = IconName | typeof NONE;
|
|
|
|
|
const ICON_NAMES = Object.keys(IconNames).map<IconType>(
|
|
|
|
|
(name: string) => IconNames[name as keyof typeof IconNames],
|
|
|
|
|
);
|
2022-06-03 05:07:02 +00:00
|
|
|
const icons = new Set(ICON_NAMES);
|
2021-07-13 08:05:09 +00:00
|
|
|
|
|
|
|
|
const TypedSelect = Select.ofType<IconType>();
|
|
|
|
|
|
|
|
|
|
class IconSelectControl extends BaseControl<
|
|
|
|
|
IconSelectControlProps,
|
|
|
|
|
IconSelectControlState
|
|
|
|
|
> {
|
|
|
|
|
private iconSelectTargetRef: React.RefObject<HTMLButtonElement>;
|
2022-03-04 06:45:50 +00:00
|
|
|
private virtuosoRef: React.RefObject<VirtuosoGridHandle>;
|
|
|
|
|
private initialItemIndex: number;
|
|
|
|
|
private filteredItems: Array<IconType>;
|
|
|
|
|
private searchInput: React.RefObject<HTMLInputElement>;
|
2022-05-17 06:55:25 +00:00
|
|
|
id: string = generateReactKey();
|
2021-07-13 08:05:09 +00:00
|
|
|
|
|
|
|
|
constructor(props: IconSelectControlProps) {
|
|
|
|
|
super(props);
|
|
|
|
|
this.iconSelectTargetRef = React.createRef();
|
2022-03-04 06:45:50 +00:00
|
|
|
this.virtuosoRef = React.createRef();
|
|
|
|
|
this.searchInput = React.createRef();
|
|
|
|
|
this.initialItemIndex = 0;
|
|
|
|
|
this.filteredItems = [];
|
2023-09-18 13:07:03 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Multiple instances of the IconSelectControl class may be created,
|
|
|
|
|
* and each instance modifies the ICON_NAMES array and the icons set.
|
|
|
|
|
* Without the below logic, the NONE icon may be added or removed
|
|
|
|
|
* multiple times, leading to unexpected behaviour.
|
|
|
|
|
*/
|
|
|
|
|
const noneIconExists = icons.has(NONE);
|
|
|
|
|
|
|
|
|
|
if (!props.hideNoneIcon && !noneIconExists) {
|
|
|
|
|
ICON_NAMES.unshift(NONE);
|
|
|
|
|
icons.add(NONE);
|
|
|
|
|
} else if (props.hideNoneIcon && noneIconExists) {
|
|
|
|
|
ICON_NAMES.shift();
|
|
|
|
|
icons.delete(NONE);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-04 06:45:50 +00:00
|
|
|
this.state = {
|
|
|
|
|
activeIcon: props.propertyValue ?? NONE,
|
|
|
|
|
isOpen: false,
|
|
|
|
|
};
|
2021-07-13 08:05:09 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-04 06:45:50 +00:00
|
|
|
// debouncedSetState is used to fix the following bug:
|
|
|
|
|
// https://github.com/appsmithorg/appsmith/pull/10460#issuecomment-1022895174
|
|
|
|
|
private debouncedSetState = _.debounce(
|
|
|
|
|
(obj: any, callback?: () => void) => {
|
|
|
|
|
this.setState((prevState: IconSelectControlState) => {
|
|
|
|
|
return {
|
|
|
|
|
...prevState,
|
|
|
|
|
...obj,
|
|
|
|
|
};
|
|
|
|
|
}, callback);
|
|
|
|
|
},
|
|
|
|
|
300,
|
|
|
|
|
{
|
|
|
|
|
leading: true,
|
|
|
|
|
trailing: false,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2021-07-13 08:05:09 +00:00
|
|
|
componentDidMount() {
|
2022-03-04 06:45:50 +00:00
|
|
|
// keydown event is attached to body so that it will not interfere with the keydown handler in GlobalHotKeys
|
|
|
|
|
document.body.addEventListener("keydown", this.handleKeydown);
|
2021-07-13 08:05:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2022-03-04 06:45:50 +00:00
|
|
|
document.body.removeEventListener("keydown", this.handleKeydown);
|
2021-07-13 08:05:09 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-04 06:45:50 +00:00
|
|
|
private handleQueryChange = _.debounce(() => {
|
|
|
|
|
if (this.filteredItems.length === 2)
|
|
|
|
|
this.setState({ activeIcon: this.filteredItems[1] });
|
|
|
|
|
}, 50);
|
|
|
|
|
|
2021-07-13 08:05:09 +00:00
|
|
|
public render() {
|
2021-11-16 10:42:20 +00:00
|
|
|
const { defaultIconName, propertyValue: iconName } = this.props;
|
2022-03-16 14:26:40 +00:00
|
|
|
const { activeIcon } = this.state;
|
|
|
|
|
const containerWidth =
|
|
|
|
|
this.iconSelectTargetRef.current?.getBoundingClientRect?.()?.width || 0;
|
|
|
|
|
|
2021-07-13 08:05:09 +00:00
|
|
|
return (
|
|
|
|
|
<>
|
2022-05-17 06:55:25 +00:00
|
|
|
<IconSelectContainerStyles id={this.id} targetWidth={containerWidth} />
|
2021-07-13 08:05:09 +00:00
|
|
|
<TypedSelect
|
2022-03-04 06:45:50 +00:00
|
|
|
activeItem={activeIcon || defaultIconName || NONE}
|
2021-07-13 08:05:09 +00:00
|
|
|
className="icon-select-container"
|
2022-03-04 06:45:50 +00:00
|
|
|
inputProps={{
|
|
|
|
|
inputRef: this.searchInput,
|
|
|
|
|
}}
|
2021-07-13 08:05:09 +00:00
|
|
|
itemListRenderer={this.renderMenu}
|
|
|
|
|
itemPredicate={this.filterIconName}
|
|
|
|
|
itemRenderer={this.renderIconItem}
|
|
|
|
|
items={ICON_NAMES}
|
2022-07-14 05:00:30 +00:00
|
|
|
onItemSelect={this.handleItemSelect}
|
2022-03-04 06:45:50 +00:00
|
|
|
onQueryChange={this.handleQueryChange}
|
|
|
|
|
popoverProps={{
|
|
|
|
|
enforceFocus: false,
|
|
|
|
|
minimal: true,
|
|
|
|
|
isOpen: this.state.isOpen,
|
2023-05-19 18:37:06 +00:00
|
|
|
popoverClassName: `icon-select-popover icon-select-popover-${this.id}`,
|
2022-03-04 06:45:50 +00:00
|
|
|
onInteraction: (state) => {
|
|
|
|
|
if (this.state.isOpen !== state)
|
|
|
|
|
this.debouncedSetState({ isOpen: state });
|
|
|
|
|
},
|
|
|
|
|
}}
|
2021-07-13 08:05:09 +00:00
|
|
|
>
|
|
|
|
|
<StyledButton
|
|
|
|
|
alignText={Alignment.LEFT}
|
2021-12-07 09:45:18 +00:00
|
|
|
className={
|
|
|
|
|
Classes.TEXT_OVERFLOW_ELLIPSIS + " " + replayHighlightClass
|
|
|
|
|
}
|
2021-07-13 08:05:09 +00:00
|
|
|
elementRef={this.iconSelectTargetRef}
|
|
|
|
|
fill
|
2021-11-16 10:42:20 +00:00
|
|
|
icon={iconName || defaultIconName}
|
2022-03-04 06:45:50 +00:00
|
|
|
onClick={this.handleButtonClick}
|
2021-07-13 08:05:09 +00:00
|
|
|
rightIcon="caret-down"
|
2022-05-25 10:05:53 +00:00
|
|
|
tabIndex={0}
|
2021-11-16 10:42:20 +00:00
|
|
|
text={iconName || defaultIconName || NONE}
|
2021-07-13 08:05:09 +00:00
|
|
|
/>
|
|
|
|
|
</TypedSelect>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-04 06:45:50 +00:00
|
|
|
private setActiveIcon(iconIndex: number) {
|
|
|
|
|
this.setState(
|
|
|
|
|
{
|
|
|
|
|
activeIcon: this.filteredItems[iconIndex],
|
|
|
|
|
},
|
|
|
|
|
() => {
|
|
|
|
|
if (this.virtuosoRef.current) {
|
|
|
|
|
this.virtuosoRef.current.scrollToIndex(iconIndex);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private handleKeydown = (e: KeyboardEvent) => {
|
|
|
|
|
if (this.state.isOpen) {
|
|
|
|
|
switch (e.key) {
|
|
|
|
|
case "Tab":
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
this.setState({
|
|
|
|
|
isOpen: false,
|
|
|
|
|
activeIcon: this.props.propertyValue ?? NONE,
|
|
|
|
|
});
|
|
|
|
|
break;
|
|
|
|
|
case "ArrowDown":
|
|
|
|
|
case "Down": {
|
2022-07-14 05:00:30 +00:00
|
|
|
emitInteractionAnalyticsEvent(this.iconSelectTargetRef.current, {
|
|
|
|
|
key: e.key,
|
|
|
|
|
});
|
2022-03-04 06:45:50 +00:00
|
|
|
if (document.activeElement === this.searchInput.current) {
|
|
|
|
|
(document.activeElement as HTMLElement).blur();
|
|
|
|
|
if (this.initialItemIndex < 0) this.initialItemIndex = -4;
|
|
|
|
|
else break;
|
|
|
|
|
}
|
|
|
|
|
const nextIndex = this.initialItemIndex + 4;
|
|
|
|
|
if (nextIndex < this.filteredItems.length)
|
|
|
|
|
this.setActiveIcon(nextIndex);
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case "ArrowUp":
|
|
|
|
|
case "Up": {
|
|
|
|
|
if (document.activeElement === this.searchInput.current) {
|
|
|
|
|
break;
|
|
|
|
|
} else if (
|
|
|
|
|
(e.shiftKey ||
|
|
|
|
|
(this.initialItemIndex >= 0 && this.initialItemIndex < 4)) &&
|
|
|
|
|
this.searchInput.current
|
|
|
|
|
) {
|
2022-07-14 05:00:30 +00:00
|
|
|
emitInteractionAnalyticsEvent(this.iconSelectTargetRef.current, {
|
|
|
|
|
key: e.key,
|
|
|
|
|
});
|
2022-03-04 06:45:50 +00:00
|
|
|
this.searchInput.current.focus();
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-07-14 05:00:30 +00:00
|
|
|
emitInteractionAnalyticsEvent(this.iconSelectTargetRef.current, {
|
|
|
|
|
key: e.key,
|
|
|
|
|
});
|
2022-03-04 06:45:50 +00:00
|
|
|
const nextIndex = this.initialItemIndex - 4;
|
|
|
|
|
if (nextIndex >= 0) this.setActiveIcon(nextIndex);
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case "ArrowRight":
|
|
|
|
|
case "Right": {
|
|
|
|
|
if (document.activeElement === this.searchInput.current) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-07-14 05:00:30 +00:00
|
|
|
emitInteractionAnalyticsEvent(this.iconSelectTargetRef.current, {
|
|
|
|
|
key: e.key,
|
|
|
|
|
});
|
2022-03-04 06:45:50 +00:00
|
|
|
const nextIndex = this.initialItemIndex + 1;
|
|
|
|
|
if (nextIndex < this.filteredItems.length)
|
|
|
|
|
this.setActiveIcon(nextIndex);
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case "ArrowLeft":
|
|
|
|
|
case "Left": {
|
|
|
|
|
if (document.activeElement === this.searchInput.current) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-07-14 05:00:30 +00:00
|
|
|
emitInteractionAnalyticsEvent(this.iconSelectTargetRef.current, {
|
|
|
|
|
key: e.key,
|
|
|
|
|
});
|
2022-03-04 06:45:50 +00:00
|
|
|
const nextIndex = this.initialItemIndex - 1;
|
|
|
|
|
if (nextIndex >= 0) this.setActiveIcon(nextIndex);
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case " ":
|
|
|
|
|
case "Enter": {
|
|
|
|
|
if (
|
|
|
|
|
this.searchInput.current === document.activeElement &&
|
|
|
|
|
this.filteredItems.length !== 2
|
|
|
|
|
)
|
|
|
|
|
break;
|
2022-07-14 05:00:30 +00:00
|
|
|
emitInteractionAnalyticsEvent(this.iconSelectTargetRef.current, {
|
|
|
|
|
key: e.key,
|
|
|
|
|
});
|
|
|
|
|
this.handleIconChange(
|
|
|
|
|
this.filteredItems[this.initialItemIndex],
|
|
|
|
|
true,
|
|
|
|
|
);
|
2022-03-04 06:45:50 +00:00
|
|
|
this.debouncedSetState({ isOpen: false });
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case "Escape": {
|
2022-07-14 05:00:30 +00:00
|
|
|
emitInteractionAnalyticsEvent(this.iconSelectTargetRef.current, {
|
|
|
|
|
key: e.key,
|
|
|
|
|
});
|
2022-03-04 06:45:50 +00:00
|
|
|
this.setState({
|
|
|
|
|
isOpen: false,
|
|
|
|
|
activeIcon: this.props.propertyValue ?? NONE,
|
|
|
|
|
});
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-14 05:00:30 +00:00
|
|
|
} else if (this.iconSelectTargetRef.current === document.activeElement) {
|
|
|
|
|
switch (e.key) {
|
|
|
|
|
case "ArrowUp":
|
|
|
|
|
case "Up":
|
|
|
|
|
case "ArrowDown":
|
|
|
|
|
case "Down":
|
|
|
|
|
this.debouncedSetState({ isOpen: true }, this.handleButtonClick);
|
|
|
|
|
break;
|
|
|
|
|
case "Tab":
|
|
|
|
|
emitInteractionAnalyticsEvent(this.iconSelectTargetRef.current, {
|
|
|
|
|
key: `${e.shiftKey ? "Shift+" : ""}${e.key}`,
|
|
|
|
|
});
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-03-04 06:45:50 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private handleButtonClick = () => {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
if (this.virtuosoRef.current) {
|
|
|
|
|
this.virtuosoRef.current.scrollToIndex(this.initialItemIndex);
|
|
|
|
|
}
|
|
|
|
|
}, 0);
|
|
|
|
|
};
|
|
|
|
|
|
2021-07-13 08:05:09 +00:00
|
|
|
private renderMenu: ItemListRenderer<IconType> = ({
|
2022-03-04 06:45:50 +00:00
|
|
|
activeItem,
|
|
|
|
|
filteredItems,
|
2021-07-13 08:05:09 +00:00
|
|
|
renderItem,
|
|
|
|
|
}) => {
|
2022-03-04 06:45:50 +00:00
|
|
|
this.filteredItems = filteredItems;
|
|
|
|
|
this.initialItemIndex = filteredItems.findIndex((x) => x === activeItem);
|
2021-07-13 08:05:09 +00:00
|
|
|
|
2022-03-04 06:45:50 +00:00
|
|
|
return (
|
|
|
|
|
<VirtuosoGrid
|
|
|
|
|
components={{
|
|
|
|
|
List: StyledMenu,
|
|
|
|
|
}}
|
|
|
|
|
computeItemKey={(index) => filteredItems[index]}
|
|
|
|
|
initialItemCount={16}
|
|
|
|
|
itemContent={(index) => renderItem(filteredItems[index], index)}
|
|
|
|
|
ref={this.virtuosoRef}
|
|
|
|
|
style={{ height: "165px" }}
|
|
|
|
|
tabIndex={-1}
|
|
|
|
|
totalCount={filteredItems.length}
|
|
|
|
|
/>
|
|
|
|
|
);
|
2021-07-13 08:05:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private renderIconItem: ItemRenderer<IconName | typeof NONE> = (
|
|
|
|
|
icon,
|
|
|
|
|
{ handleClick, modifiers },
|
|
|
|
|
) => {
|
|
|
|
|
if (!modifiers.matchesPredicate) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return (
|
2023-05-19 18:37:06 +00:00
|
|
|
<Tooltip content={icon} mouseEnterDelay={0}>
|
2021-07-13 08:05:09 +00:00
|
|
|
<StyledMenuItem
|
|
|
|
|
active={modifiers.active}
|
|
|
|
|
icon={icon === NONE ? undefined : icon}
|
|
|
|
|
key={icon}
|
|
|
|
|
onClick={handleClick}
|
|
|
|
|
text={icon === NONE ? NONE : undefined}
|
2022-03-04 06:45:50 +00:00
|
|
|
textClassName={icon === NONE ? "bp3-icon-(none)" : ""}
|
2021-07-13 08:05:09 +00:00
|
|
|
/>
|
2023-05-19 18:37:06 +00:00
|
|
|
</Tooltip>
|
2021-07-13 08:05:09 +00:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private filterIconName = (
|
|
|
|
|
query: string,
|
|
|
|
|
iconName: IconName | typeof NONE,
|
|
|
|
|
) => {
|
|
|
|
|
if (iconName === NONE || query === "") {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return iconName.toLowerCase().indexOf(query.toLowerCase()) >= 0;
|
|
|
|
|
};
|
|
|
|
|
|
2022-07-14 05:00:30 +00:00
|
|
|
private handleIconChange = (icon: IconType, isUpdatedViaKeyboard = false) => {
|
2022-03-04 06:45:50 +00:00
|
|
|
this.setState({ activeIcon: icon });
|
2021-07-13 08:05:09 +00:00
|
|
|
this.updateProperty(
|
|
|
|
|
this.props.propertyName,
|
|
|
|
|
icon === NONE ? undefined : icon,
|
2022-07-14 05:00:30 +00:00
|
|
|
isUpdatedViaKeyboard,
|
2021-07-13 08:05:09 +00:00
|
|
|
);
|
2022-03-04 06:45:50 +00:00
|
|
|
};
|
2021-07-13 08:05:09 +00:00
|
|
|
|
2022-07-14 05:00:30 +00:00
|
|
|
private handleItemSelect = (icon: IconType) => {
|
|
|
|
|
this.handleIconChange(icon, false);
|
|
|
|
|
};
|
|
|
|
|
|
2021-07-13 08:05:09 +00:00
|
|
|
static getControlType() {
|
|
|
|
|
return "ICON_SELECT";
|
|
|
|
|
}
|
2022-06-03 05:07:02 +00:00
|
|
|
|
|
|
|
|
static canDisplayValueInUI(
|
|
|
|
|
config: IconSelectControlProps,
|
|
|
|
|
value: any,
|
|
|
|
|
): boolean {
|
|
|
|
|
if (icons.has(value)) return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-07-13 08:05:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default IconSelectControl;
|