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

MenuItem in any level #2749

Closed
wants to merge 7 commits into from
Closed

MenuItem in any level #2749

wants to merge 7 commits into from

Conversation

khlutkova
Copy link
Contributor

@khlutkova khlutkova commented Jan 28, 2022

@JackUait
Copy link
Contributor

@zhzz А почему это изменение для next'а, а не текущего мастера, если оно не потенциально ломающее?

@khlutkova khlutkova reopened this Feb 17, 2022
@khlutkova khlutkova requested a review from zhzz February 21, 2022 07:56
@khlutkova khlutkova marked this pull request as ready for review February 25, 2022 09:11
packages/react-ui/components/Autocomplete/Autocomplete.tsx Outdated Show resolved Hide resolved
packages/react-ui/components/MenuItem/MenuItem.tsx Outdated Show resolved Hide resolved
packages/react-ui/components/MenuItem/MenuItem.tsx Outdated Show resolved Hide resolved
packages/react-ui/components/MenuItem/MenuItem.tsx Outdated Show resolved Hide resolved
Comment on lines 273 to 332
private highlight = (key: string | undefined) => {
this.setState({ highlightedKey: key });
getRootNode(this)?.focus();
};

private unhighlight = () => {
this.setState({ highlightedIndex: -1 });
};

private move(step: number) {
this.setState((state, props) => {
const children = childrenToArray(props.children);
if (!children.some(isActiveElement)) {
return null;
}
let index = state.highlightedIndex;
do {
index += step;
if (!props.cyclicSelection && (index < 0 || index > children.length)) {
return null;
}
const menuItems = this.menuItems;
if (!menuItems.length) {
return;
}

if (index < 0) {
index = children.length - 1;
} else if (index > children.length) {
index = 0;
}
let highlightedIndex = -1;
if (this.state.highlightedKey) {
highlightedIndex = this.menuItems.findIndex((item) => item.key === this.state.highlightedKey);
}

const child = children[index];
if (isActiveElement(child)) {
return { highlightedIndex: index };
}
} while (index !== state.highlightedIndex);
return null;
}, this.scrollToSelected);
const newHighlightedIndex = highlightedIndex + step;
if (newHighlightedIndex < 0) {
this.setState({ highlightedKey: menuItems[menuItems.length - 1].key });
this.scrollToBottom();
return;
}

if (newHighlightedIndex >= menuItems.length) {
this.setState({ highlightedKey: menuItems[0].key });
this.scrollToTop();
return;
}

this.setState({ highlightedKey: menuItems[newHighlightedIndex].key });

switch (newHighlightedIndex) {
case 0:
this.scrollToTop();
break;
case menuItems.length - 1:
this.scrollToBottom();
break;
default:
this.scrollToSelected();
}
}

private scrollToSelected = () => {
if (this.scrollContainer && this.highlighted) {
this.scrollContainer.scrollTo(getRootNode(this.highlighted));
}
};

private scrollToTop = () => {
if (this.scrollContainer) {
this.scrollContainer.scrollToTop();
}
};

private scrollToBottom = () => {
if (this.scrollContainer) {
this.scrollContainer.scrollToBottom();
}
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Эта логика идентична логике из Menu. Нужно переиспользовать её. Это потенциальное место для большого количества багов.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Про идентичность согласна.
Но на будущее есть задача про унификацию меню-подобных компонентов, в рамках которой и хотела избавиться от копипасты.
Подумаю, что и как можно сделать на текущем этапе.

packages/react-ui/internal/Menu/Menu.tsx Outdated Show resolved Hide resolved
packages/react-ui/internal/InternalMenu/InternalMenu.tsx Outdated Show resolved Hide resolved
packages/react-ui/internal/Menu/Menu.tsx Show resolved Hide resolved
@JackUait
Copy link
Contributor

JackUait commented Mar 2, 2022

Нужно удалить лишние комментарии.

@JackUait
Copy link
Contributor

JackUait commented Mar 2, 2022

Везде где используются значения из MenuItemType их нужно переиспользовать.

@khlutkova khlutkova changed the base branch from next to master March 14, 2022 11:59
@khlutkova khlutkova requested a review from JackUait March 23, 2022 10:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants