mm
This commit is contained in:
@@ -4,7 +4,7 @@ import {
|
||||
import { mergeClass } from "../../styles/utils/merge-class";
|
||||
import { displayRecipe } from "../../styles/recipes/display-recipe";
|
||||
import { dotSizeRecipe } from "../../styles/recipes/dot-size-recipe";
|
||||
import type { CommonProps } from "../../utils/CommonProps";
|
||||
import type { CommonProps } from "../../utils/common-props";
|
||||
import { brandRecipe } from "../../styles/recipes/brand-recipe";
|
||||
|
||||
export type BadgeProps = CommonProps & {
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
import React, { useState, type CSSProperties, type ReactNode } from "react";
|
||||
import { SpinnerSvg } from "../../assets/svg/SpinnerSvg";
|
||||
import React, { type CSSProperties, type ReactNode } from "react";
|
||||
import { SpinnerSvg } from "../../assets/svg/spinner-svg";
|
||||
import { Icon } from "../icon/icon";
|
||||
import { displayRecipe } from "../../styles/recipes/display-recipe";
|
||||
import { itemSizeRecipe } from "../../styles/recipes/item-size-recipe";
|
||||
import { variantRecipe } from "../../styles/recipes/variant-recipe";
|
||||
import { brandRecipe } from "../../styles/recipes/brand-recipe";
|
||||
import type { CommonProps } from "../../utils/CommonProps";
|
||||
import type { CommonProps } from "../../utils/common-props";
|
||||
import { mergeClass } from "../../styles/utils/merge-class";
|
||||
import { containerRecipe } from "../../styles/recipes/container-recipe";
|
||||
|
||||
// 缺个 loading 动画,暂时没想好怎么做,后面发现样本再抄
|
||||
export type ButtonProps = CommonProps & {
|
||||
@@ -61,8 +62,9 @@ export const Button = (props: ButtonProps) => {
|
||||
// ====================================================================================
|
||||
|
||||
const rootClass = mergeClass(
|
||||
containerRecipe({ hasFocusOutline: true }),
|
||||
displayRecipe(),
|
||||
itemSizeRecipe({ size, shape, isIconOnly}),
|
||||
itemSizeRecipe({ size, shape, isIconOnly, isLongPadding: "true" }),
|
||||
variantRecipe({ variant, isDisabled }),
|
||||
brandRecipe({ brand: brandFinal }),
|
||||
className,
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
import type { CommonProps } from "../../utils/common-props";
|
||||
|
||||
export type CheckboxInputGroupProps = CommonProps & {
|
||||
name?: string;
|
||||
selectedList?: string[];
|
||||
};
|
||||
@@ -0,0 +1,109 @@
|
||||
import { useRef, useState, type ChangeEvent, type MouseEvent } from "react";
|
||||
import type { CommonProps } from "../../utils/common-props";
|
||||
import { mergeClass } from "../../styles/utils/merge-class";
|
||||
import { inlineSizeRecipe } from "../../styles/recipes/inline-size-recipe";
|
||||
import { useMergeRefs } from "@floating-ui/react";
|
||||
import { containerRecipe } from "../../styles/recipes/container-recipe";
|
||||
import { downGradeSize } from "../../utils/down-grade-size";
|
||||
import { Icon } from "../icon/icon";
|
||||
import { CheckboxCheckedSvg } from "../../assets/svg/checkbox-checked-svg";
|
||||
import { CheckboxUncheckedSvg } from "../../assets/svg/checkbox-unchecked-svg";
|
||||
import { itemSizeRecipe } from "../../styles/recipes/item-size-recipe";
|
||||
|
||||
export type CheckboxInputProps = CommonProps & {
|
||||
size?: "sm" | "md";
|
||||
name: string;
|
||||
value: string;
|
||||
checked?: boolean;
|
||||
defaultChecked?: boolean;
|
||||
onChange?: (nextChecked: boolean) => void;
|
||||
asBoxOnly?: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
* Use CheckboxInput instead, this just a piece of Component.
|
||||
*
|
||||
* Same name will get like interest=coding&interest=music
|
||||
*/
|
||||
export const CheckboxInput = (props: CheckboxInputProps) => {
|
||||
const {
|
||||
size: propSize = "sm",
|
||||
name,
|
||||
value,
|
||||
checked: controlledChecked,
|
||||
defaultChecked = false,
|
||||
onChange,
|
||||
ref,
|
||||
className = "",
|
||||
children,
|
||||
asBoxOnly = false,
|
||||
} = props;
|
||||
|
||||
const size = asBoxOnly ? downGradeSize(propSize, 1) : propSize;
|
||||
const downSize = asBoxOnly ? downGradeSize(size, 1) : size;
|
||||
|
||||
const [checked, setChecked] = useState<boolean>(
|
||||
controlledChecked ?? defaultChecked,
|
||||
);
|
||||
|
||||
const inputRef = useRef<HTMLInputElement>(null);
|
||||
|
||||
const rootClass = mergeClass(
|
||||
containerRecipe(),
|
||||
itemSizeRecipe({
|
||||
size,
|
||||
isIconOnly: asBoxOnly === true,
|
||||
}),
|
||||
className,
|
||||
);
|
||||
|
||||
const boxClass = mergeClass(
|
||||
inlineSizeRecipe({
|
||||
size: downSize,
|
||||
shape: "rounded",
|
||||
isIconOnly: asBoxOnly === true,
|
||||
}),
|
||||
);
|
||||
|
||||
const inputClass = mergeClass("sr-only");
|
||||
|
||||
// =============================================================
|
||||
|
||||
// 将 input 的 onClick 范围扩大至整个容器元素
|
||||
function clickHandler(e: MouseEvent<HTMLDivElement>) {
|
||||
// 防止 input 和 div 重叠,响应两次 onClick
|
||||
e.stopPropagation();
|
||||
// 记住前面没有 on
|
||||
inputRef?.current?.click();
|
||||
}
|
||||
|
||||
function changeHandler(e: ChangeEvent<HTMLInputElement>) {
|
||||
// 触发重新渲染,受控模式下,即使 set 也改变不了状态
|
||||
setChecked(e.target.checked);
|
||||
// 执行受控回调函数,向上层传递新值,或许上层组件会拒绝变更从 prop 传入的 checked
|
||||
onChange?.(e.target.checked);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className={rootClass} onClick={clickHandler}>
|
||||
<div className={boxClass}>
|
||||
<input
|
||||
name={name}
|
||||
value={value}
|
||||
type="checkbox"
|
||||
ref={useMergeRefs([inputRef, ref])}
|
||||
// 绑定 state 第1/2步,从 state 中获取数据
|
||||
checked={checked}
|
||||
// 绑定 state 第2/2步,本元素被键盘点击后,将新值写回 state
|
||||
onChange={changeHandler}
|
||||
className={inputClass}
|
||||
/>
|
||||
<Icon size={downSize}>
|
||||
{checked ? <CheckboxCheckedSvg /> : <CheckboxUncheckedSvg />}
|
||||
</Icon>
|
||||
{!asBoxOnly && children}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
CheckboxInput.displayName = "CheckboxInput";
|
||||
@@ -1,73 +0,0 @@
|
||||
import { useRef, useState, type ChangeEvent, type MouseEvent } from "react";
|
||||
import { CheckIndicatorSvg } from "../../assets/svg/CheckIndicatorSvg";
|
||||
import type { CommonProps } from "../../utils/CommonProps";
|
||||
import { mergeClass } from "../../styles/utils/merge-class";
|
||||
import { inlineSizeRecipe } from "../../styles/recipes/inline-size-recipe";
|
||||
import { useMergeRefs } from "@floating-ui/react";
|
||||
|
||||
export type CheckboxProps = CommonProps & {
|
||||
size?: "sm" | "md";
|
||||
checked?: boolean;
|
||||
defaultChecked?: boolean;
|
||||
onChange?: (nextChecked: boolean) => void;
|
||||
};
|
||||
|
||||
export const Checkbox = (props: CheckboxProps) => {
|
||||
const {
|
||||
size = "sm",
|
||||
checked: controlledChecked,
|
||||
defaultChecked = false,
|
||||
onChange,
|
||||
ref,
|
||||
className = "",
|
||||
} = props;
|
||||
|
||||
const [checked, setChecked] = useState<boolean>(
|
||||
controlledChecked ?? defaultChecked,
|
||||
);
|
||||
|
||||
const inputRef = useRef<HTMLInputElement>(null);
|
||||
|
||||
const rootClass = mergeClass(
|
||||
inlineSizeRecipe({ size, shape: "rounded", isIconOnly: true }),
|
||||
className,
|
||||
);
|
||||
|
||||
const inputClass = mergeClass("sr-only");
|
||||
|
||||
const indicatorClass = mergeClass(
|
||||
inlineSizeRecipe({ size, shape: "rounded" }),
|
||||
);
|
||||
|
||||
// 将 input 的 onClick 范围扩大至整个容器元素
|
||||
function clickHandler(e: MouseEvent<HTMLDivElement>) {
|
||||
// 防止 input 和 div 重叠,响应两次 onClick
|
||||
e.stopPropagation();
|
||||
// 记住前面没有 on
|
||||
inputRef?.current?.click();
|
||||
}
|
||||
|
||||
// 绑定 state 第2/3步,本元素被键盘点击后,将新值写回 state
|
||||
function changeHandler(e: ChangeEvent<HTMLInputElement>) {
|
||||
// 触发重新渲染,受控模式下,即使 set 也改变不了状态
|
||||
setChecked(e.target.checked);
|
||||
// 执行受控回调函数,向上层传递新值,或许上层组件会拒绝变更从 prop 传入的 checked
|
||||
onChange?.(e.target.checked);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className={rootClass} onClick={clickHandler}>
|
||||
<input
|
||||
className={inputClass}
|
||||
type="checkbox"
|
||||
ref={useMergeRefs([inputRef, ref])}
|
||||
// 绑定 state 第1/3步,从 state 中获取数据
|
||||
checked={checked}
|
||||
// 绑定 state 第2/3步,本元素被键盘点击后,将新值写回 state
|
||||
onChange={changeHandler}
|
||||
/>
|
||||
{checked && <CheckIndicatorSvg className={indicatorClass} />}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
Checkbox.displayName = "Checkbox";
|
||||
@@ -1,6 +1,5 @@
|
||||
|
||||
import { Slot } from "../../utils/slot";
|
||||
import type { CommonProps } from "../../utils/CommonProps";
|
||||
import type { CommonProps } from "../../utils/common-props";
|
||||
import { inlineSizeRecipe } from "../../styles/recipes/inline-size-recipe";
|
||||
import { mergeClass } from "../../styles/utils/merge-class";
|
||||
|
||||
@@ -8,16 +7,28 @@ export type IconProps = CommonProps & {
|
||||
size?: "xs" | "sm" | "md" | "lg" | "xl" | "2xl" | "3xl";
|
||||
shape?: "square" | "circle" | "rounded";
|
||||
} & {
|
||||
onClick?: (e?: React.MouseEvent) => void
|
||||
onClick?: (e?: React.MouseEvent) => void;
|
||||
};
|
||||
|
||||
export const Icon = (props: IconProps) => {
|
||||
const { size = "md", className = "", children, shape = "square", ref, ...rest } = props;
|
||||
const {
|
||||
size = "md",
|
||||
className = "",
|
||||
children,
|
||||
shape = "square",
|
||||
ref,
|
||||
...rest
|
||||
} = props;
|
||||
|
||||
const rootClass = mergeClass(inlineSizeRecipe({ size, shape, isIconOnly: "true" }), className);
|
||||
const rootClass = mergeClass(
|
||||
inlineSizeRecipe({ size, shape, isIconOnly: "true" }),
|
||||
className,
|
||||
);
|
||||
|
||||
return (
|
||||
<Slot {...rest} ref={ref} className={rootClass}>{children}</Slot>
|
||||
<Slot {...rest} ref={ref} className={rootClass}>
|
||||
{children}
|
||||
</Slot>
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
@@ -2,23 +2,28 @@ import { useMenuFloatingContext } from "./use-menu-floating-context";
|
||||
import { useListItem, useMergeRefs } from "@floating-ui/react";
|
||||
import { useMenuContext } from "./use-menu-context";
|
||||
import React, {
|
||||
useEffect,
|
||||
useId,
|
||||
useRef,
|
||||
useState,
|
||||
type CSSProperties,
|
||||
type MouseEvent,
|
||||
} from "react";
|
||||
import { Icon } from "../icon/icon";
|
||||
import { Checkbox } from "../checkbox/checkbox";
|
||||
import { CheckboxInput } from "../checkbox-input/checkbox-input";
|
||||
import { displayRecipe } from "../../styles/recipes/display-recipe";
|
||||
import { mergeClass } from "../../styles/utils/merge-class";
|
||||
import { itemSizeRecipe } from "../../styles/recipes/item-size-recipe";
|
||||
import { variantRecipe } from "../../styles/recipes/variant-recipe";
|
||||
import type { CommonProps } from "../../utils/CommonProps";
|
||||
import type { CommonProps } from "../../utils/common-props";
|
||||
import { containerRecipe } from "../../styles/recipes/container-recipe";
|
||||
import { inlineSizeRecipe } from "../../styles/recipes/inline-size-recipe";
|
||||
|
||||
export type MenuCheckItemProps = CommonProps & {
|
||||
size?: "sm" | "md";
|
||||
iconSvg?: React.ReactNode;
|
||||
hotkey?: string;
|
||||
// =================
|
||||
checked?: boolean;
|
||||
defaultChecked?: boolean;
|
||||
onChange?: (nextChecked: boolean) => void;
|
||||
@@ -26,13 +31,14 @@ export type MenuCheckItemProps = CommonProps & {
|
||||
|
||||
export const MenuCheckItem = (props: MenuCheckItemProps) => {
|
||||
const {
|
||||
size: propSize = "sm",
|
||||
checked: controlledChecked,
|
||||
defaultChecked = false,
|
||||
// ==============================
|
||||
onChange,
|
||||
ref,
|
||||
children,
|
||||
iconSvg = <div></div>,
|
||||
iconSvg,
|
||||
isDisabled = false,
|
||||
hotkey,
|
||||
style,
|
||||
@@ -44,15 +50,36 @@ export const MenuCheckItem = (props: MenuCheckItemProps) => {
|
||||
);
|
||||
const inputRef = useRef<HTMLInputElement>(null);
|
||||
|
||||
// =============== Menu Context ====================================================================
|
||||
// =============== Menu Context Consumer ====================================================================
|
||||
|
||||
const menuContext = useMenuContext();
|
||||
const { size = "sm" } = menuContext!;
|
||||
const size = menuContext?.size ?? propSize;
|
||||
|
||||
// =============== Menu Floating Context ====================================================================
|
||||
const stateRegistry = menuContext?.stateRegistry;
|
||||
const stateKeyRef = useRef<string>(null);
|
||||
|
||||
useEffect(() => {
|
||||
stateKeyRef.current = useId();
|
||||
stateRegistry?.insertKey(stateKeyRef.current);
|
||||
stateRegistry?.updateValue(stateKeyRef.current, {
|
||||
hasIcon: iconSvg != undefined,
|
||||
hasCheckbox: true,
|
||||
hasIndicator: false,
|
||||
});
|
||||
|
||||
return () => {
|
||||
// 清除注册表里的数据,永远不会有无用 key
|
||||
stateRegistry?.deleteEntry(stateKeyRef.current!);
|
||||
};
|
||||
}, []);
|
||||
|
||||
// 后面渲染要用到
|
||||
const stateResult = menuContext?.stateResult;
|
||||
|
||||
// =============== Menu Floating Context Consumer ====================================================================
|
||||
|
||||
const menuFloatingContext = useMenuFloatingContext();
|
||||
const { getItemProps, activeIndex, isOpen } = menuFloatingContext!;
|
||||
const { getItemProps, activeIndex } = menuFloatingContext!;
|
||||
|
||||
const item = useListItem();
|
||||
const isActive = item.index === activeIndex;
|
||||
@@ -71,9 +98,17 @@ export const MenuCheckItem = (props: MenuCheckItemProps) => {
|
||||
...style,
|
||||
};
|
||||
|
||||
const startSectionClass = displayRecipe();
|
||||
const startSectionClass = mergeClass([
|
||||
displayRecipe(),
|
||||
inlineSizeRecipe({ size }),
|
||||
]);
|
||||
|
||||
const endSectionClass = displayRecipe();
|
||||
const endSectionClass = mergeClass([
|
||||
displayRecipe(),
|
||||
inlineSizeRecipe({ size }),
|
||||
]);
|
||||
|
||||
// =============== Event ==========================================
|
||||
|
||||
// 将 Checkbox 的 onClick 范围扩大至整个容器元素
|
||||
function clickHandler(e: MouseEvent<HTMLDivElement>) {
|
||||
@@ -83,7 +118,6 @@ export const MenuCheckItem = (props: MenuCheckItemProps) => {
|
||||
inputRef?.current?.click();
|
||||
}
|
||||
|
||||
// 绑定 state 第2/3步,本元素被键盘点击后,将新值写回 state
|
||||
function changeHandler(nextChecked: boolean) {
|
||||
// 触发重新渲染,受控模式下,即使 set 也改变不了状态
|
||||
setChecked(nextChecked);
|
||||
@@ -105,27 +139,38 @@ export const MenuCheckItem = (props: MenuCheckItemProps) => {
|
||||
onClick: clickHandler,
|
||||
})}
|
||||
>
|
||||
<Checkbox
|
||||
<CheckboxInput
|
||||
name=""
|
||||
value=""
|
||||
ref={useMergeRefs([inputRef, ref])}
|
||||
size={size}
|
||||
defaultChecked={checked}
|
||||
// 绑定 state 第1/3步,从 state 中获取数据
|
||||
// 绑定 state 第1/2步,从 state 中获取数据
|
||||
checked={checked}
|
||||
// 绑定 state 第2/3步,本元素被键盘点击后,将新值写回 state
|
||||
// 绑定 state 第2/2步,本元素被键盘点击后,将新值写回 state
|
||||
onChange={changeHandler}
|
||||
/>
|
||||
|
||||
<div className={startSectionClass}>
|
||||
<Icon size={size}>{iconSvg}</Icon>
|
||||
{stateResult?.hasIcon && iconSvg ? (
|
||||
<Icon size={size}>{iconSvg}</Icon>
|
||||
) : (
|
||||
<Icon size={size}>
|
||||
<div></div>
|
||||
</Icon>
|
||||
)}
|
||||
|
||||
{children}
|
||||
</div>
|
||||
|
||||
<div className={endSectionClass}>
|
||||
{hotkey && <div>{hotkey}</div>}
|
||||
{/* 占位 */}
|
||||
<Icon size={size}>
|
||||
<div></div>
|
||||
</Icon>
|
||||
|
||||
{stateResult?.hasIndicator && (
|
||||
<Icon size={size}>
|
||||
<div></div>
|
||||
</Icon>
|
||||
)}
|
||||
</div>
|
||||
</button>
|
||||
);
|
||||
|
||||
@@ -1,15 +1,16 @@
|
||||
import type { ComponentPropsWithoutRef, CSSProperties } from "react";
|
||||
import type { CSSProperties } from "react";
|
||||
import {
|
||||
FloatingFocusManager,
|
||||
FloatingList,
|
||||
FloatingPortal,
|
||||
useMergeRefs,
|
||||
} from "@floating-ui/react";
|
||||
import { useMenuFloatingContext } from "./use-menu-floating-context";
|
||||
import { useMenuContext } from "./use-menu-context";
|
||||
import { mergeClass } from "../../styles/utils/merge-class";
|
||||
import { containerRecipe } from "../../styles/recipes/container-recipe";
|
||||
import { useThemeContext } from "../theme/use-theme-context";
|
||||
import type { CommonProps } from "../../utils/CommonProps";
|
||||
import type { CommonProps } from "../../utils/common-props";
|
||||
import { brandRecipe } from "../../styles/recipes/brand-recipe";
|
||||
|
||||
export type MenuDropdownProps = CommonProps & {
|
||||
@@ -17,30 +18,41 @@ export type MenuDropdownProps = CommonProps & {
|
||||
};
|
||||
|
||||
export const MenuDropdown = (props: MenuDropdownProps) => {
|
||||
const { modal = true, className = "", children, style, ...rest } = props;
|
||||
const {
|
||||
modal = false,
|
||||
className = "",
|
||||
children,
|
||||
style,
|
||||
ref,
|
||||
...rest
|
||||
} = props;
|
||||
|
||||
// =========== Menu Context Consumer ===================================================================================================
|
||||
|
||||
const { size } = useMenuContext()!;
|
||||
|
||||
// =========== Menu Floating Context Consumer ===================================================================================================
|
||||
|
||||
const {
|
||||
refs,
|
||||
elementsRef,
|
||||
context,
|
||||
floatingStyles,
|
||||
getFloatingProps,
|
||||
isNested,
|
||||
isOpen,
|
||||
} = useMenuFloatingContext()!;
|
||||
|
||||
// ==============================================================================================================
|
||||
// =========== Theme Context Consumer ===================================================================================================
|
||||
|
||||
const { themeBaseClass, themeClass } = useThemeContext()!;
|
||||
|
||||
// ===============================================================================================================
|
||||
// ============== Style =================================================================================================
|
||||
|
||||
const rootClass = mergeClass(
|
||||
containerRecipe({
|
||||
boxShadow: true,
|
||||
rounded: size == "sm" ? 4 : 8,
|
||||
padding: size == "sm" ? 4 : 8,
|
||||
rounded: size == "sm" ? 4 : size == "md" ? 8 : undefined,
|
||||
padding: size == "sm" ? 4 : size == "md" ? 8 : undefined,
|
||||
background: "base",
|
||||
hasFocusOutline: "false",
|
||||
}),
|
||||
|
||||
@@ -1,18 +1,20 @@
|
||||
import { useMenuFloatingContext } from "./use-menu-floating-context";
|
||||
import { useListItem, useMergeRefs } from "@floating-ui/react";
|
||||
import React, { type CSSProperties } from "react";
|
||||
import React, { useEffect, useId, useRef, type CSSProperties } from "react";
|
||||
import { Icon } from "../icon/icon";
|
||||
import { ChevronRightSvg } from "../../assets/svg/ChevronRightSvg";
|
||||
import { ChevronRightSvg } from "../../assets/svg/chevron-right-svg";
|
||||
import { useMenuTriggerContext } from "./use-menu-trigger-context";
|
||||
import { useMenuContext } from "./use-menu-context";
|
||||
import { mergeClass } from "../../styles/utils/merge-class";
|
||||
import { displayRecipe } from "../../styles/recipes/display-recipe";
|
||||
import { itemSizeRecipe } from "../../styles/recipes/item-size-recipe";
|
||||
import { variantRecipe } from "../../styles/recipes/variant-recipe";
|
||||
import type { CommonProps } from "../../utils/CommonProps";
|
||||
import type { CommonProps } from "../../utils/common-props";
|
||||
import { containerRecipe } from "../../styles/recipes/container-recipe";
|
||||
import { inlineSizeRecipe } from "../../styles/recipes/inline-size-recipe";
|
||||
|
||||
export type MenuItemProps = CommonProps & {
|
||||
size?: "sm" | "md";
|
||||
onClick?: (e?: React.MouseEvent) => void;
|
||||
iconSvg?: React.ReactNode;
|
||||
indicatorSvg?: React.ReactNode;
|
||||
@@ -21,9 +23,10 @@ export type MenuItemProps = CommonProps & {
|
||||
|
||||
export const MenuItem = (props: MenuItemProps) => {
|
||||
const {
|
||||
size: propSize = "sm",
|
||||
ref,
|
||||
children,
|
||||
iconSvg = <div></div>,
|
||||
iconSvg,
|
||||
indicatorSvg = <ChevronRightSvg />,
|
||||
onClick,
|
||||
style,
|
||||
@@ -32,11 +35,39 @@ export const MenuItem = (props: MenuItemProps) => {
|
||||
...rest
|
||||
} = props;
|
||||
|
||||
// =============== Menu Context ====================================================================
|
||||
// =============== Menu Trigger Context Consumer ====================================================================
|
||||
|
||||
const { size = "sm" } = useMenuContext()!;
|
||||
const menuTriggerContext = useMenuTriggerContext();
|
||||
const isTrigger = !!menuTriggerContext;
|
||||
|
||||
// =============== Menu Floating Context ====================================================================
|
||||
// =============== Menu Context Consumer ====================================================================
|
||||
|
||||
const menuContext = useMenuContext();
|
||||
const size = menuContext?.size ?? propSize;
|
||||
|
||||
const stateRegistry = menuContext?.stateRegistry;
|
||||
const stateKeyRef = useRef<string>(null);
|
||||
|
||||
// 注册 state
|
||||
useEffect(() => {
|
||||
stateKeyRef.current = useId();
|
||||
stateRegistry?.insertKey(stateKeyRef.current);
|
||||
stateRegistry?.updateValue(stateKeyRef.current, {
|
||||
hasIcon: iconSvg != undefined,
|
||||
hasCheckbox: false,
|
||||
hasIndicator: isTrigger,
|
||||
});
|
||||
|
||||
return () => {
|
||||
// 清除注册表里的数据,永远不会有无用 key 和脏数据
|
||||
stateRegistry?.deleteEntry(stateKeyRef.current!);
|
||||
};
|
||||
}, []);
|
||||
|
||||
// 后面渲染要用到
|
||||
const stateResult = menuContext?.stateResult;
|
||||
|
||||
// =============== Menu Floating Context Consumer ====================================================================
|
||||
|
||||
const menuFloatingContext = useMenuFloatingContext();
|
||||
const { getItemProps, activeIndex, isOpen } = menuFloatingContext!;
|
||||
@@ -44,12 +75,6 @@ export const MenuItem = (props: MenuItemProps) => {
|
||||
const item = useListItem();
|
||||
const isActive = item.index === activeIndex;
|
||||
|
||||
// =============== Menu Trigger Context ====================================================================
|
||||
|
||||
const menuTriggerContext = useMenuTriggerContext();
|
||||
|
||||
const isTrigger = !!menuTriggerContext;
|
||||
|
||||
// ================== Style =================================================================
|
||||
|
||||
const rootClass = mergeClass(
|
||||
@@ -64,9 +89,15 @@ export const MenuItem = (props: MenuItemProps) => {
|
||||
...style,
|
||||
};
|
||||
|
||||
const startSectionClass = displayRecipe();
|
||||
const startSectionClass = mergeClass([
|
||||
displayRecipe(),
|
||||
inlineSizeRecipe({ size }),
|
||||
]);
|
||||
|
||||
const endSectionClass = displayRecipe();
|
||||
const endSectionClass = mergeClass([
|
||||
displayRecipe(),
|
||||
inlineSizeRecipe({ size }),
|
||||
]);
|
||||
|
||||
return (
|
||||
<button
|
||||
@@ -84,20 +115,29 @@ export const MenuItem = (props: MenuItemProps) => {
|
||||
})}
|
||||
>
|
||||
<div className={startSectionClass}>
|
||||
<Icon size={size}>{iconSvg}</Icon>
|
||||
{stateResult?.hasIcon && iconSvg ? (
|
||||
<Icon size={size}>{iconSvg}</Icon>
|
||||
) : (
|
||||
<Icon size={size}>
|
||||
<div></div>
|
||||
</Icon>
|
||||
)}
|
||||
|
||||
{children}
|
||||
</div>
|
||||
|
||||
<div className={endSectionClass}>
|
||||
{hotkey && <div>{hotkey}</div>}
|
||||
|
||||
{isTrigger && isOpen && (
|
||||
<Icon size={size} style={{ rotate: "90deg" }}>
|
||||
{indicatorSvg}
|
||||
</Icon>
|
||||
)}
|
||||
|
||||
{isTrigger && !isOpen && <Icon size={size}>{indicatorSvg}</Icon>}
|
||||
{!isTrigger && (
|
||||
// 占位
|
||||
|
||||
{stateResult?.hasIndicator && (
|
||||
<Icon size={size}>
|
||||
<div></div>
|
||||
</Icon>
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import type { CommonProps } from "../../utils/CommonProps";
|
||||
import { MenuRadioItemGroupContext } from "./use-menu-radio-item-group-context";
|
||||
import type { CommonProps } from "../../utils/common-props";
|
||||
import { MenuRadioItemGroupContext } from "../radiobox-input/use-radiobox-input-group-context";
|
||||
import { useMenuContext } from "./use-menu-context";
|
||||
|
||||
export type MenuRadioItemGroupProps = CommonProps & {
|
||||
size?: "sm" | "md";
|
||||
name: string;
|
||||
defaultSelectName?: string;
|
||||
onChange?: (nextSelect?: string) => void;
|
||||
};
|
||||
@@ -18,7 +19,7 @@ export const MenuRadioItemGroup = (props: MenuRadioItemGroupProps) => {
|
||||
...rest
|
||||
} = props;
|
||||
|
||||
const [selectName, setSelectName] = useState<string>(defaultSelectName);
|
||||
const [selectValue, setSelectValue] = useState<string>(defaultSelectName);
|
||||
|
||||
const { size: menuSize } = useMenuContext()!;
|
||||
|
||||
@@ -26,7 +27,7 @@ export const MenuRadioItemGroup = (props: MenuRadioItemGroupProps) => {
|
||||
|
||||
return (
|
||||
<MenuRadioItemGroupContext
|
||||
value={{ size: currentSize, selectName, setSelectName }}
|
||||
value={{ size: currentSize, selectValue, setSelectValue}}
|
||||
>
|
||||
<div {...rest}>{children}</div>
|
||||
</MenuRadioItemGroupContext>
|
||||
|
||||
@@ -1,20 +1,38 @@
|
||||
import { useRef } from "react";
|
||||
import type { CommonProps } from "../../utils/CommonProps";
|
||||
import { useMenuRadioItemGroupContext } from "./use-menu-radio-item-group-context";
|
||||
import type { CommonProps } from "../../utils/common-props";
|
||||
import { useMenuRadioItemGroupContext } from "../radiobox-input/use-radiobox-input-group-context";
|
||||
|
||||
export type MenuRadioItemProps = CommonProps & {
|
||||
name: string;
|
||||
iconSvg?: React.ReactNode;
|
||||
hotkey?: string;
|
||||
onChange?: (preCheck?: boolean, nextCheck?: boolean) => void
|
||||
size?: "sm" | "md";
|
||||
name?: string;
|
||||
value?: string;
|
||||
iconSvg?: React.ReactNode;
|
||||
hotkey?: string;
|
||||
// ===============
|
||||
onChange?: (nextSelect?: boolean) => void;
|
||||
};
|
||||
|
||||
export const MenuRadioItem = (props: MenuRadioItemProps) => {
|
||||
const { name, children, className, hotkey, onChange, ref, iconSvg, style } = props
|
||||
const {
|
||||
size: propSize = "sm",
|
||||
name,
|
||||
value,
|
||||
iconSvg,
|
||||
hotkey,
|
||||
onChange,
|
||||
ref,
|
||||
children,
|
||||
className,
|
||||
style,
|
||||
} = props;
|
||||
|
||||
const { selectName, setSelectName } = useMenuRadioItemGroupContext()!
|
||||
const isSelect = name === selectName
|
||||
|
||||
return <div></div>
|
||||
}
|
||||
// ========= Radio Group Context Cunsumer =====================================================
|
||||
|
||||
const { selectValue, setSelectValue } = useMenuRadioItemGroupContext()!;
|
||||
const isSelect = value === selectValue;
|
||||
|
||||
const inputRef = useRef<HTMLInputElement>(null);
|
||||
// =========================================================
|
||||
|
||||
return <div></div>;
|
||||
};
|
||||
|
||||
@@ -1,25 +1,24 @@
|
||||
import type { ComponentPropsWithoutRef } from "react";
|
||||
|
||||
import { useMenuFloatingContext } from "./use-menu-floating-context";
|
||||
import { Slot } from "../../utils/slot";
|
||||
import { MenuTriggerContext } from "./use-menu-trigger-context";
|
||||
import type { CommonProps } from "../../utils/common-props";
|
||||
import { containerRecipe } from "../../styles/recipes/container-recipe";
|
||||
|
||||
export type MenuTriggerProps = ComponentPropsWithoutRef<"div">;
|
||||
export type MenuTriggerProps = CommonProps;
|
||||
|
||||
export const MenuTrigger = (props: MenuTriggerProps) => {
|
||||
const { children } = props;
|
||||
|
||||
const menuFloatingContext = useMenuFloatingContext();
|
||||
const { refs, setIsOpen, getReferenceProps } = menuFloatingContext!;
|
||||
const { refs, getReferenceProps } = useMenuFloatingContext()!;
|
||||
|
||||
return (
|
||||
<MenuTriggerContext.Provider value={{ isTrigger: true }}>
|
||||
<Slot className={"reset"} ref={refs.setReference} {...getReferenceProps(
|
||||
{ onClick: () => { setIsOpen(true) } }
|
||||
)}>
|
||||
<Slot
|
||||
ref={refs.setReference}
|
||||
{...getReferenceProps()}
|
||||
>
|
||||
{children}
|
||||
</Slot>
|
||||
</MenuTriggerContext.Provider>
|
||||
)
|
||||
|
||||
);
|
||||
};
|
||||
|
||||
@@ -1,54 +1,107 @@
|
||||
import {
|
||||
FloatingNode,
|
||||
FloatingTree,
|
||||
} from "@floating-ui/react";
|
||||
import type { ComponentPropsWithoutRef } from "react";
|
||||
import { FloatingNode, FloatingTree } from "@floating-ui/react";
|
||||
import { useLayoutEffect, type ComponentPropsWithoutRef } from "react";
|
||||
import { useMenuFloating } from "./use-menu-floating";
|
||||
import { MenuFloatingContext } from "./use-menu-floating-context";
|
||||
import { MenuContext } from "./use-menu-context";
|
||||
import { MenuContext, useMenuContext } from "./use-menu-context";
|
||||
import { mergeClass } from "../../styles/utils/merge-class";
|
||||
import { useStateRegistry } from "../../utils/use-state-registry";
|
||||
|
||||
export type MenuProps = ComponentPropsWithoutRef<"div"> & {
|
||||
size?: "sm" | "md";
|
||||
};
|
||||
|
||||
export const Menu = (props: MenuProps) => {
|
||||
const { size = "sm", className = "", children, ...rest } = props;
|
||||
const { size: propSize = "sm", className = "", children, ...rest } = props;
|
||||
|
||||
// ============= parent Menu Context Cunsumer ==================================
|
||||
|
||||
const parentMenuContext = useMenuContext();
|
||||
const size = parentMenuContext?.size ?? propSize;
|
||||
|
||||
// ============= Menu Floating Provider ==================================
|
||||
|
||||
const menuFloatingContextValue = useMenuFloating();
|
||||
const { isNested, nodeId } = menuFloatingContextValue!
|
||||
const { isNested, nodeId } = menuFloatingContextValue!;
|
||||
|
||||
// ==========================================================================================
|
||||
// ============= Menu Context Provider ==================================
|
||||
|
||||
const rootClass = mergeClass("position--relative",className)
|
||||
const stateRegistry = useStateRegistry<{
|
||||
hasIcon: boolean;
|
||||
hasCheckbox: boolean;
|
||||
hasIndicator: boolean;
|
||||
}>((registry) => {
|
||||
let result: {
|
||||
hasIcon: boolean;
|
||||
hasCheckbox: boolean;
|
||||
hasIndicator: boolean;
|
||||
} = { hasIcon: false, hasCheckbox: false, hasIndicator: false };
|
||||
|
||||
// ==========================================================================================
|
||||
registry.forEach((v) => {
|
||||
if (v?.hasIcon) {
|
||||
result.hasIcon = true;
|
||||
}
|
||||
if (v?.hasCheckbox) {
|
||||
result.hasCheckbox = true;
|
||||
}
|
||||
if (v?.hasIndicator) {
|
||||
result.hasIndicator = true;
|
||||
}
|
||||
});
|
||||
|
||||
if (!isNested) {
|
||||
return (
|
||||
<MenuContext.Provider value={{ size }}>
|
||||
<MenuFloatingContext.Provider value={menuFloatingContextValue}>
|
||||
<FloatingTree>
|
||||
<FloatingNode id={nodeId}>
|
||||
<div {...rest} className={rootClass}>
|
||||
{children}
|
||||
</div>
|
||||
</FloatingNode>
|
||||
</FloatingTree>
|
||||
</MenuFloatingContext.Provider>
|
||||
</MenuContext.Provider>
|
||||
);
|
||||
}
|
||||
return result;
|
||||
});
|
||||
|
||||
return (
|
||||
<MenuContext.Provider value={{ size }}>
|
||||
<MenuFloatingContext.Provider value={menuFloatingContextValue}>
|
||||
// ============== Style ============================================================================
|
||||
|
||||
const rootClass = mergeClass("position--relative", className);
|
||||
|
||||
// ============== Collect Childrens States ============================================================================
|
||||
|
||||
let stateResult:
|
||||
| {
|
||||
hasIcon: boolean;
|
||||
hasCheckbox: boolean;
|
||||
hasIndicator: boolean;
|
||||
}
|
||||
| undefined = undefined;
|
||||
|
||||
useLayoutEffect(() => {
|
||||
if (stateRegistry.checkAllReady()) {
|
||||
stateResult = stateRegistry.getResult();
|
||||
}
|
||||
});
|
||||
|
||||
// ======= Condition Return ======================================================
|
||||
|
||||
let returnStatement;
|
||||
|
||||
// 如果值为 undefined,则 return 空值, 也就是不渲染任何东西,但实际渲染函数还是执行了的
|
||||
if (stateResult) {
|
||||
if (!isNested) {
|
||||
returnStatement = (
|
||||
<FloatingTree>
|
||||
<FloatingNode id={nodeId}>
|
||||
<div {...rest} className={rootClass}>
|
||||
{children}
|
||||
</div>
|
||||
</FloatingNode>
|
||||
</FloatingTree>
|
||||
);
|
||||
} else {
|
||||
returnStatement = (
|
||||
<FloatingNode id={nodeId}>
|
||||
<div {...rest} className={rootClass} >
|
||||
<div {...rest} className={rootClass}>
|
||||
{children}
|
||||
</div>
|
||||
</FloatingNode>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<MenuContext.Provider value={{ size, stateRegistry, stateResult }}>
|
||||
<MenuFloatingContext.Provider value={menuFloatingContextValue}>
|
||||
{returnStatement}
|
||||
</MenuFloatingContext.Provider>
|
||||
</MenuContext.Provider>
|
||||
);
|
||||
|
||||
@@ -1,6 +1,21 @@
|
||||
import { createContext, useContext } from "react";
|
||||
import type { useStateRegistry } from "../../utils/use-state-registry";
|
||||
|
||||
export type MenuContextValue = { size?: "sm" | "md" } | null;
|
||||
export type MenuContextValue = {
|
||||
size?: "sm" | "md";
|
||||
stateRegistry?: ReturnType<
|
||||
typeof useStateRegistry<{
|
||||
hasIcon: boolean;
|
||||
hasCheckbox: boolean;
|
||||
hasIndicator: boolean;
|
||||
}>
|
||||
>;
|
||||
stateResult?: {
|
||||
hasIcon: boolean;
|
||||
hasCheckbox: boolean;
|
||||
hasIndicator: boolean;
|
||||
};
|
||||
} | null;
|
||||
|
||||
export const MenuContext = createContext<MenuContextValue>(null);
|
||||
|
||||
|
||||
@@ -44,20 +44,20 @@ export function useMenuFloating() {
|
||||
});
|
||||
|
||||
const hover = useHover(context, {
|
||||
// 仅子菜单启用 hover
|
||||
enabled: isNested,
|
||||
delay: { open: 75 },
|
||||
// 模拟 click, 鼠标即使移出了安全区,也不关闭 1s=1000ms
|
||||
// 10分钟 = 6,000,000
|
||||
delay: { open: 75, close: 6000000 },
|
||||
// 必须开启,否则鼠标进入子菜单就关闭
|
||||
handleClose: safePolygon({ blockPointerEvents: true }),
|
||||
});
|
||||
|
||||
const click = useClick(context, {
|
||||
event: "mousedown",
|
||||
toggle: !isNested,
|
||||
ignoreMouse: isNested,
|
||||
});
|
||||
const click = useClick(context, {});
|
||||
|
||||
const role = useRole(context, { role: "menu" });
|
||||
|
||||
const dismiss = useDismiss(context, { bubbles: true });
|
||||
const dismiss = useDismiss(context);
|
||||
|
||||
const listNav = useListNavigation(context, {
|
||||
listRef: elementsRef,
|
||||
@@ -86,6 +86,6 @@ export function useMenuFloating() {
|
||||
setSelectedIndex,
|
||||
activeIndex,
|
||||
isOpen,
|
||||
setIsOpen
|
||||
setIsOpen,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
import { createContext, useContext } from "react";
|
||||
|
||||
export type MenuRadioItemGroupContextValue = {
|
||||
size: "sm" | "md"
|
||||
selectName: string;
|
||||
setSelectName: React.Dispatch<React.SetStateAction<string>>;
|
||||
} | null;
|
||||
|
||||
export const MenuRadioItemGroupContext = createContext<MenuRadioItemGroupContextValue>(null)
|
||||
|
||||
export function useMenuRadioItemGroupContext() {
|
||||
return useContext(MenuRadioItemGroupContext)
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import type { CSSProperties } from "react";
|
||||
import type { CommonProps } from "../../utils/CommonProps";
|
||||
import type { CommonProps } from "../../utils/common-props";
|
||||
import { usePopoverContext } from "./use-popover-context";
|
||||
import {
|
||||
FloatingArrow,
|
||||
@@ -23,6 +23,7 @@ export const PopoverDropdown = (props: PopoverDropdownProps) => {
|
||||
context,
|
||||
arrowRef,
|
||||
isMounted,
|
||||
isOpen,
|
||||
transitionStyles,
|
||||
arrowWidth,
|
||||
arrowHeight,
|
||||
@@ -57,14 +58,15 @@ export const PopoverDropdown = (props: PopoverDropdownProps) => {
|
||||
|
||||
return (
|
||||
<>
|
||||
{isMounted && (
|
||||
{isOpen && (
|
||||
<FloatingPortal>
|
||||
<FloatingFocusManager context={context} modal={false}>
|
||||
<div
|
||||
className={rootClass}
|
||||
{...rest}
|
||||
ref={refs.setFloating}
|
||||
className={rootClass}
|
||||
style={rootStyle}
|
||||
{...getFloatingProps({ ...rest })}
|
||||
{...getFloatingProps()}
|
||||
>
|
||||
{children}
|
||||
<FloatingArrow
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
import type { CommonProps } from "../../utils/CommonProps"
|
||||
import type { CommonProps } from "../../utils/common-props";
|
||||
import { Slot } from "../../utils/slot";
|
||||
import { usePopoverContext } from "./use-popover-context";
|
||||
|
||||
export type PopoverTriggerProps = CommonProps
|
||||
export type PopoverTriggerProps = CommonProps;
|
||||
|
||||
export const PopoverTrigger = (props: PopoverTriggerProps) => {
|
||||
const { children, ...rest } = props;
|
||||
const { children, ...rest } = props;
|
||||
|
||||
const { refs, getReferenceProps, setIsOpen, isOpen } = usePopoverContext()!
|
||||
const { refs, getReferenceProps } = usePopoverContext()!;
|
||||
|
||||
return <Slot ref={refs.setReference} {...getReferenceProps(
|
||||
{ ...rest, onClick: () => { setIsOpen(!isOpen) } }
|
||||
)}>
|
||||
{children}
|
||||
return (
|
||||
<Slot ref={refs.setReference} {...getReferenceProps()}>
|
||||
{children}
|
||||
</Slot>
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
import type { CommonProps } from "../../utils/CommonProps"
|
||||
import { PopoverContext } from "./use-popover-context"
|
||||
import { usePopoverFloating } from "./use-popover-floating"
|
||||
import type { CommonProps } from "../../utils/common-props";
|
||||
import { PopoverContext } from "./use-popover-context";
|
||||
import { usePopoverFloating } from "./use-popover-floating";
|
||||
|
||||
export type PopoverProps = CommonProps & {
|
||||
|
||||
}
|
||||
export type PopoverProps = CommonProps & {};
|
||||
|
||||
export const Popover = (props: PopoverProps) => {
|
||||
const { children } = props
|
||||
const popoverFloatingContext = usePopoverFloating()
|
||||
const { children } = props;
|
||||
const popoverFloatingContext = usePopoverFloating();
|
||||
|
||||
return <PopoverContext.Provider value={popoverFloatingContext}>
|
||||
<div>{children}</div>
|
||||
return (
|
||||
<PopoverContext.Provider value={popoverFloatingContext}>
|
||||
<div>{children}</div>
|
||||
</PopoverContext.Provider>
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
@@ -1,83 +1,111 @@
|
||||
import { arrow, autoUpdate, flip, offset, shift, useClick, useDismiss, useFloating, useInteractions, useRole, useTransitionStyles } from "@floating-ui/react";
|
||||
import {
|
||||
arrow,
|
||||
autoUpdate,
|
||||
flip,
|
||||
offset,
|
||||
shift,
|
||||
useClick,
|
||||
useDismiss,
|
||||
useFloating,
|
||||
useInteractions,
|
||||
useRole,
|
||||
useTransitionStyles,
|
||||
} from "@floating-ui/react";
|
||||
import { useMemo, useRef, useState } from "react";
|
||||
|
||||
export function usePopoverFloating() {
|
||||
const [isOpen, setIsOpen] = useState(false);
|
||||
const arrowRef = useRef(null);
|
||||
const [isOpen, setIsOpen] = useState(false);
|
||||
const arrowRef = useRef(null);
|
||||
|
||||
// 拉扯 svg 图像,让三角变得,更尖更长,或者更宽更扁
|
||||
const arrowWidth = 16
|
||||
const arrowHeight = 8
|
||||
// 拉扯 svg 图像,让三角变得,更尖更长,或者更宽更扁
|
||||
const arrowWidth = 16;
|
||||
const arrowHeight = 8;
|
||||
|
||||
const { refs, floatingStyles, context, middlewareData } = useFloating({
|
||||
placement: "top-start",
|
||||
open: isOpen,
|
||||
onOpenChange: setIsOpen,
|
||||
whileElementsMounted: autoUpdate,
|
||||
middleware: [
|
||||
offset(arrowHeight),
|
||||
flip({ padding: 8 }),
|
||||
// 偏移完成后,再留 8px 空白,而不是紧贴着父容器
|
||||
shift({ padding: 8 }),
|
||||
arrow({
|
||||
element: arrowRef,
|
||||
}),
|
||||
],
|
||||
});
|
||||
const { refs, floatingStyles, context, middlewareData } = useFloating({
|
||||
placement: "top-start",
|
||||
open: isOpen,
|
||||
onOpenChange: setIsOpen,
|
||||
whileElementsMounted: autoUpdate,
|
||||
middleware: [
|
||||
offset(arrowHeight),
|
||||
flip({ padding: 8 }),
|
||||
// 偏移完成后,再留 8px 空白,而不是紧贴着父容器
|
||||
shift({ padding: 8 }),
|
||||
arrow({
|
||||
element: arrowRef,
|
||||
}),
|
||||
],
|
||||
});
|
||||
|
||||
// 这两个是未经 useFloating 变换前的坐标
|
||||
// 也就是 arrow 悬浮,左对齐 floating,两者水平中线重合
|
||||
const arrowX = middlewareData.arrow?.x ?? 0;
|
||||
const arrowY = middlewareData.arrow?.y ?? 0;
|
||||
// 这两个是未经 useFloating 变换前的坐标
|
||||
// 也就是 arrow 悬浮,左对齐 floating,两者水平中线重合
|
||||
const arrowX = middlewareData.arrow?.x ?? 0;
|
||||
const arrowY = middlewareData.arrow?.y ?? 0;
|
||||
|
||||
// 变换中心的起始坐标就是 floating 的左上角
|
||||
const transformX = arrowX + arrowWidth / 2;
|
||||
const transformY = arrowY + arrowHeight;
|
||||
// 变换中心的起始坐标就是 floating 的左上角
|
||||
const transformX = arrowX + arrowWidth / 2;
|
||||
const transformY = arrowY + arrowHeight;
|
||||
|
||||
// style 用于内容框,而不是 FloatingArrow
|
||||
const { isMounted, styles: transitionStyles } = useTransitionStyles(context, {
|
||||
duration: 150,
|
||||
initial: {
|
||||
opacity: 0,
|
||||
transform: "scale(0)"
|
||||
},
|
||||
close: {
|
||||
opacity: 0
|
||||
},
|
||||
common: ({ side }) => ({
|
||||
transitionTimingFunction: "ease",
|
||||
transformOrigin: {
|
||||
top: `${transformX}px calc(100% + ${arrowHeight}px)`,
|
||||
bottom: `${transformX}px ${-arrowHeight}px`,
|
||||
left: `calc(100% + ${arrowHeight}px) ${transformY}px`,
|
||||
right: `${-arrowHeight}px ${transformY}px`
|
||||
}[side]
|
||||
})
|
||||
})
|
||||
// style 用于内容框,而不是 FloatingArrow
|
||||
const { isMounted, styles: transitionStyles } = useTransitionStyles(context, {
|
||||
duration: 150,
|
||||
initial: {
|
||||
opacity: 0,
|
||||
transform: "scale(0)",
|
||||
},
|
||||
close: {
|
||||
opacity: 0,
|
||||
},
|
||||
common: ({ side }) => ({
|
||||
transitionTimingFunction: "ease",
|
||||
transformOrigin: {
|
||||
top: `${transformX}px calc(100% + ${arrowHeight}px)`,
|
||||
bottom: `${transformX}px ${-arrowHeight}px`,
|
||||
left: `calc(100% + ${arrowHeight}px) ${transformY}px`,
|
||||
right: `${-arrowHeight}px ${transformY}px`,
|
||||
}[side],
|
||||
}),
|
||||
});
|
||||
|
||||
const click = useClick(context);
|
||||
const dismiss = useDismiss(context);
|
||||
const role = useRole(context);
|
||||
const click = useClick(context);
|
||||
const dismiss = useDismiss(context);
|
||||
const role = useRole(context);
|
||||
|
||||
const { getReferenceProps, getFloatingProps } = useInteractions([
|
||||
click,
|
||||
dismiss,
|
||||
role,
|
||||
]);
|
||||
const { getReferenceProps, getFloatingProps } = useInteractions([
|
||||
click,
|
||||
dismiss,
|
||||
role,
|
||||
]);
|
||||
|
||||
return useMemo(() => ({
|
||||
isOpen,
|
||||
setIsOpen,
|
||||
/* only use at {isMounted && <FloatingPortal/>} */
|
||||
isMounted,
|
||||
arrowRef,
|
||||
arrowHeight,
|
||||
arrowWidth,
|
||||
refs,
|
||||
floatingStyles,
|
||||
transitionStyles,
|
||||
context,
|
||||
getReferenceProps,
|
||||
getFloatingProps
|
||||
}), [isOpen, setIsOpen, isMounted, refs, arrowHeight, arrowWidth, arrowRef, floatingStyles, transitionStyles, context, getReferenceProps, getFloatingProps])
|
||||
}
|
||||
return useMemo(
|
||||
() => ({
|
||||
isOpen,
|
||||
setIsOpen,
|
||||
/* only use at {isMounted && <FloatingPortal/>} */
|
||||
isMounted,
|
||||
arrowRef,
|
||||
arrowHeight,
|
||||
arrowWidth,
|
||||
refs,
|
||||
floatingStyles,
|
||||
transitionStyles,
|
||||
context,
|
||||
getReferenceProps,
|
||||
getFloatingProps,
|
||||
}),
|
||||
[
|
||||
isOpen,
|
||||
setIsOpen,
|
||||
isMounted,
|
||||
refs,
|
||||
arrowHeight,
|
||||
arrowWidth,
|
||||
arrowRef,
|
||||
floatingStyles,
|
||||
transitionStyles,
|
||||
context,
|
||||
getReferenceProps,
|
||||
getFloatingProps,
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import type { CommonProps } from "../../utils/common-props";
|
||||
import { RadioboxInputGroupContext } from "./use-radiobox-input-group-context";
|
||||
|
||||
export type RadioboxInputGroupProps = CommonProps & {
|
||||
size?: "sm" | "md";
|
||||
name: string;
|
||||
defaultSelectedValue?: string;
|
||||
onChange?: (nextSelected?: string) => void;
|
||||
canNoneSelected?: boolean;
|
||||
};
|
||||
|
||||
export const RadioboxInputGroup = (props: RadioboxInputGroupProps) => {
|
||||
const {
|
||||
size = "sm",
|
||||
defaultSelectedValue,
|
||||
name,
|
||||
onChange,
|
||||
canNoneSelected = false,
|
||||
children,
|
||||
} = props;
|
||||
const [selectedValue, setSelectedValue] = useState<string | undefined>(
|
||||
defaultSelectedValue,
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
onChange?.(selectedValue);
|
||||
}, [selectedValue]);
|
||||
|
||||
return (
|
||||
<RadioboxInputGroupContext.Provider
|
||||
value={{ size, selectedValue, setSelectedValue, name, canNoneSelected }}
|
||||
>
|
||||
{children}
|
||||
</RadioboxInputGroupContext.Provider>
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,122 @@
|
||||
import { useRef, useState, type ChangeEvent, type MouseEvent } from "react";
|
||||
import { useMergeRefs } from "@floating-ui/react";
|
||||
import type { CommonProps } from "../../utils/common-props";
|
||||
import { downGradeSize } from "../../utils/down-grade-size";
|
||||
import { mergeClass } from "../../styles/utils/merge-class";
|
||||
import { containerRecipe } from "../../styles/recipes/container-recipe";
|
||||
import { inlineSizeRecipe } from "../../styles/recipes/inline-size-recipe";
|
||||
import { Icon } from "../icon/icon";
|
||||
import { RadioboxCheckedSvg } from "../../assets/svg/radiobox-checked-svg";
|
||||
import { RadioboxUncheckedSvg } from "../../assets/svg/radiobox-unchecked-svg";
|
||||
import { itemSizeRecipe } from "../../styles/recipes/item-size-recipe";
|
||||
import { useRadioboxInputGroupContext } from "./use-radiobox-input-group-context";
|
||||
import { displayRecipe } from "../../styles/recipes/display-recipe";
|
||||
|
||||
export type RadioboxInputProps = CommonProps & {
|
||||
value: string;
|
||||
size?: "sm" | "md";
|
||||
checked?: boolean;
|
||||
defaultChecked?: boolean;
|
||||
onChange?: (nextChecked: boolean) => void;
|
||||
asBoxOnly?: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
* Use RadioboxInput instead, this just a piece of Component.
|
||||
*
|
||||
* a group is define by the same name.
|
||||
*/
|
||||
export const RadioboxInput = (props: RadioboxInputProps) => {
|
||||
const {
|
||||
size: propSize = "sm",
|
||||
value,
|
||||
onChange,
|
||||
ref,
|
||||
className = "",
|
||||
children,
|
||||
asBoxOnly = false,
|
||||
} = props;
|
||||
|
||||
// ======== Group Context =============================================
|
||||
|
||||
const {
|
||||
size: groupSize,
|
||||
name: groupName,
|
||||
selectedValue,
|
||||
setSelectedValue,
|
||||
canNoneSelected,
|
||||
} = useRadioboxInputGroupContext()!;
|
||||
|
||||
const currentSize = groupSize ?? propSize;
|
||||
const size = asBoxOnly ? downGradeSize(currentSize, 1) : currentSize;
|
||||
const downSize = asBoxOnly ? downGradeSize(size, 1) : size;
|
||||
|
||||
const inputRef = useRef<HTMLInputElement>(null);
|
||||
|
||||
const isSelected = selectedValue === value ? true : false;
|
||||
|
||||
// ========= Style =========================================
|
||||
|
||||
const rootClass = mergeClass(
|
||||
containerRecipe(),
|
||||
itemSizeRecipe({ size, isIconOnly: asBoxOnly === true }),
|
||||
className,
|
||||
);
|
||||
|
||||
const boxClass = mergeClass(
|
||||
inlineSizeRecipe({
|
||||
size: downSize,
|
||||
shape: "circle",
|
||||
isIconOnly: asBoxOnly === true,
|
||||
}),
|
||||
);
|
||||
|
||||
// sr-only 1个像素都没有,更不可能被鼠标点击
|
||||
const inputClass = mergeClass(displayRecipe({ screenReaderOnly: "hide" }));
|
||||
|
||||
// ========== Event ===================================================
|
||||
|
||||
// 将 input 的 onClick 范围扩大至整个容器元素
|
||||
function clickHandler(e: MouseEvent<HTMLDivElement>) {
|
||||
e.stopPropagation();
|
||||
// 这里点击 input
|
||||
inputRef.current?.click();
|
||||
}
|
||||
|
||||
function changeHandler(e: ChangeEvent<HTMLInputElement>) {
|
||||
if (isSelected == true && canNoneSelected == true) {
|
||||
setSelectedValue?.(undefined);
|
||||
} else if (isSelected == true && canNoneSelected == false) {
|
||||
// do nothing, bucause repeat selected
|
||||
// isSelected will reset input.checked value
|
||||
} else {
|
||||
setSelectedValue?.(value);
|
||||
onChange?.(e.target.checked);
|
||||
}
|
||||
}
|
||||
|
||||
// =============================================================
|
||||
|
||||
return (
|
||||
<div className={rootClass} onClick={clickHandler}>
|
||||
<div className={boxClass}>
|
||||
<input
|
||||
name={groupName}
|
||||
value={value}
|
||||
type="radio"
|
||||
ref={useMergeRefs([inputRef, ref])}
|
||||
// 绑定 state 第1/2步,从 state 中获取数据
|
||||
checked={isSelected}
|
||||
// 绑定 state 第2/2步,本元素被键盘点击后,将新值写回 state
|
||||
onChange={changeHandler}
|
||||
className={inputClass}
|
||||
/>
|
||||
<Icon size={downSize}>
|
||||
{isSelected ? <RadioboxCheckedSvg /> : <RadioboxUncheckedSvg />}
|
||||
</Icon>
|
||||
{!asBoxOnly && children}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
RadioboxInput.displayName = "RadioboxInput";
|
||||
@@ -0,0 +1,16 @@
|
||||
import { createContext, useContext } from "react";
|
||||
|
||||
export type RadioboxInputGroupContextValue = {
|
||||
size?: "sm" | "md";
|
||||
selectedValue?: string;
|
||||
setSelectedValue?: React.Dispatch<React.SetStateAction<string | undefined>>;
|
||||
name?: string;
|
||||
canNoneSelected?: boolean;
|
||||
} | null;
|
||||
|
||||
export const RadioboxInputGroupContext =
|
||||
createContext<RadioboxInputGroupContextValue>(null);
|
||||
|
||||
export function useRadioboxInputGroupContext() {
|
||||
return useContext(RadioboxInputGroupContext);
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
|
||||
import { Icon } from "../icon/icon";
|
||||
import { CloseSvg } from "../../assets/svg/CloseSvg";
|
||||
import { CloseSvg } from "../../assets/svg/close-svg";
|
||||
import { displayRecipe } from "../../styles/recipes/display-recipe";
|
||||
import { itemSizeRecipe } from "../../styles/recipes/item-size-recipe";
|
||||
import { brandRecipe } from "../../styles/recipes/brand-recipe";
|
||||
import { variantRecipe } from "../../styles/recipes/variant-recipe";
|
||||
import type { CommonProps } from "../../utils/CommonProps";
|
||||
import type { CommonProps } from "../../utils/common-props";
|
||||
import { useState, type ReactNode } from "react";
|
||||
import { mergeClass } from "../../styles/utils/merge-class";
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { useState } from "react";
|
||||
|
||||
import type { CommonProps } from "../../utils/CommonProps";
|
||||
import type { CommonProps } from "../../utils/common-props";
|
||||
import { ThemeContext } from "./use-theme-context";
|
||||
import { mergeClass } from "../../styles/utils/merge-class";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user