Skip to content

Commit

Permalink
Merge pull request #20 from allure-framework/toogle-behaviour-fix
Browse files Browse the repository at this point in the history
fix: fix onClick Toggle behaviour
  • Loading branch information
epszaw authored Dec 17, 2024
2 parents d56cb97 + b5616e0 commit c60db38
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 21 deletions.
4 changes: 2 additions & 2 deletions packages/web-awesome/src/components/app/ReportBody/SortBy.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import clsx from "clsx";
import { ComponentChildren } from "preact";
import type { ComponentChildren } from "preact";
import lineChevronDownIcon from "@/assets/svg/line-arrows-chevron-down.svg";
import sortAscIcon from "@/assets/svg/line-arrows-sort-line-asc.svg";
import sortDescIcon from "@/assets/svg/line-arrows-sort-line-desc.svg";
Expand Down Expand Up @@ -92,7 +92,7 @@ export const SortBy = () => {
<Menu.Item
closeMenuOnClick={false}
onClick={onClick}
leadingIcon={direction == "asc" ? sortAscIcon.id : sortDescIcon.id}
leadingIcon={direction === "asc" ? sortAscIcon.id : sortDescIcon.id}
rightSlot={
<BtnWrapper>
<DropdownButton
Expand Down
55 changes: 40 additions & 15 deletions packages/web-awesome/src/components/commons/Menu/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
import { autoUpdate, computePosition, flip, offset, shift } from "@floating-ui/dom";
import { clsx } from "clsx";
import type { ComponentChildren, VNode } from "preact";
import { useEffect, useRef, useState } from "preact/hooks";
import { createContext } from "preact";
import { useContext, useEffect, useRef, useState } from "preact/hooks";
import check from "@/assets/svg/line-general-check.svg";
import { SvgIcon } from "@/components/commons/SvgIcon";
import { Text } from "@/components/commons/Typography";
import * as styles from "./styles.scss";

type MenuContextT = {
setIsOpened: (isOpened: boolean) => void;
};

const MenuContext = createContext<MenuContextT | null>(null);

export const useMenuContext = () => {
const context = useContext(MenuContext);

if (!context) {
throw new Error("useMenuContext must be used within a Menu");
}

return context;
};

export const Menu = (props: {
children: ComponentChildren;
isInitialOpened?: boolean;
Expand Down Expand Up @@ -96,20 +113,26 @@ export const Menu = (props: {
}, [menuRef.current, triggerRef.current]);

return (
<>
{typeof menuTrigger === "function" && (
<MenuTriggerWrapper ref={triggerRef}>
{menuTrigger({
isOpened,
onClick: handleTriggerClick,
setIsOpened,
})}
</MenuTriggerWrapper>
)}
<div ref={menuRef}>
{isOpened && <aside className={clsx(styles.menu, styles[`size-${size}`])}>{children}</aside>}
</div>
</>
<MenuContext.Provider
value={{
setIsOpened,
}}
>
<>
{typeof menuTrigger === "function" && (
<MenuTriggerWrapper ref={triggerRef}>
{menuTrigger({
isOpened,
onClick: handleTriggerClick,
setIsOpened,
})}
</MenuTriggerWrapper>
)}
<div ref={menuRef}>
{isOpened && <aside className={clsx(styles.menu, styles[`size-${size}`])}>{children}</aside>}
</div>
</>
</MenuContext.Provider>
);
};

Expand All @@ -128,13 +151,15 @@ type ItemProps = {
};

Menu.Item = (props: ItemProps) => {
const { setIsOpened } = useMenuContext();
const { children, onClick, leadingIcon, rightSlot, ariaLabel, closeMenuOnClick = true } = props;
const isInteractive = typeof onClick === "function";
const hasLeadingIcon = typeof leadingIcon === "string";

const handleItemClick = (e: MouseEvent) => {
if (isInteractive && closeMenuOnClick) {
e.stopPropagation();
setIsOpened(false);
}

if (isInteractive) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const Toggle = (props: Props) => {
type="checkbox"
checked={value}
aria-label={label}
onChange={handleChange}
onToggle={handleChange}
/>
);
};
8 changes: 5 additions & 3 deletions packages/web-awesome/src/utils/capitalize.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export function capitalize(str: string) {
if (!str) return;
export const capitalize = (str: string) => {
if (!str) {
return;
}
return str.charAt(0).toLocaleUpperCase() + str.slice(1);
}
};

0 comments on commit c60db38

Please sign in to comment.