Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

refactor(ui): move interface (#138)

Dushusir672fb50986e9
diff --git a/packages/base-ui/src/Components/Slider/Slider.tsx b/packages/base-ui/src/Components/Slider/Slider.tsx
index 8d91e4b40f..acfaa76c11 100644
--- a/packages/base-ui/src/Components/Slider/Slider.tsx
+++ b/packages/base-ui/src/Components/Slider/Slider.tsx
@@ -122,7 +122,7 @@
// );
// }
// }
-import React, { CSSProperties, ForwardedRef, forwardRef, useEffect, useRef, useState } from 'react';
+import React, { CSSProperties, useEffect, useRef, useState } from 'react';
import { randomId } from '../../Utils';
import styles from './index.module.less';
@@ -149,14 +149,14 @@ export interface BaseSliderRangeProps extends SliderBaseProps {
onClick?: (e: Event, value: number | string) => void;
}
-function SliderFactory(props: BaseSliderRangeProps | BaseSliderSingleProps, ref: ForwardedRef<HTMLDivElement>) {
+export function Slider(props: BaseSliderRangeProps | BaseSliderSingleProps) {
const [valuePrev, setValuePrev] = useState<number>(0);
const [valueNext, setValueNext] = useState<number>(0);
const idPrevRef = useRef<string>(randomId('slider'));
const idNextRef = useRef<string>(randomId('slider'));
const sliderRef = useRef<HTMLDivElement | null>(null);
- const { min = 0, max = 100, step = 1, range = false } = props;
+ const { min = 0, max = 100, step = 1, range = false, value } = props;
useEffect(() => {
// 初始化input值和样式
@@ -200,7 +200,7 @@ function SliderFactory(props: BaseSliderRangeProps | BaseSliderSingleProps, ref:
);
}
}
- }, [min, max, range, valuePrev, valueNext]);
+ }, [value]);
const onInput = (index: number, e: React.FormEvent<HTMLInputElement>) => {
const value = Number((e.target as HTMLInputElement).value);
@@ -237,7 +237,14 @@ function SliderFactory(props: BaseSliderRangeProps | BaseSliderSingleProps, ref:
};
return (
- <div className={styles.slider} role="group" aria-labelledby="multi-lbl" style={{ '--min': min, '--max': max } as CSSProperties} ref={sliderRef}>
+ <div
+ className={styles.slider}
+ role="group"
+ aria-labelledby="multi-lbl"
+ style={{ '--min': min, '--max': max } as CSSProperties}
+ ref={sliderRef}
+ onClick={(e) => onClick(0, e)}
+ >
<label className="sr-only" htmlFor={idPrevRef.current}></label>
<input id={idPrevRef.current} type="range" min={min} value={valuePrev} max={max} step={step} onInput={(e) => onInput(0, e)} />
{range && (
@@ -249,6 +256,3 @@ function SliderFactory(props: BaseSliderRangeProps | BaseSliderSingleProps, ref:
</div>
);
}
-
-// Use React.forwardRef to wrap the component
-export const Slider = forwardRef(SliderFactory);
diff --git a/packages/base-ui/src/Interfaces/CountBar.ts b/packages/base-ui/src/Interfaces/CountBar.ts
deleted file mode 100644
index 202ce2c2dc..0000000000
--- a/packages/base-ui/src/Interfaces/CountBar.ts
+++ /dev/null
@@ -1,6 +0,0 @@
-import { BaseComponentProps } from '../BaseComponent';
-
-// TODO remove to component file
-export interface BaseCountBarProps extends BaseComponentProps {
- changeRatio: (ratio: string) => void;
-}
diff --git a/packages/base-ui/src/Interfaces/CustomLabel.ts b/packages/base-ui/src/Interfaces/CustomLabel.ts
deleted file mode 100644
index b3cc3c6b85..0000000000
--- a/packages/base-ui/src/Interfaces/CustomLabel.ts
+++ /dev/null
@@ -1,16 +0,0 @@
-import { BaseSelectChildrenProps } from '../Components/Select/Select';
-
-export interface ICustomLabelProps {
- prefix?: string[] | string;
- suffix?: string[] | string;
- options?: BaseSelectChildrenProps[];
- label?: string;
- children?: ICustomLabelProps[];
- onKeyUp?: (e: Event) => void;
-}
-
-// TODO move to right menu
-export interface ICustomLabelType {
- name: string;
- props?: ICustomLabelProps;
-}
diff --git a/packages/base-ui/src/Interfaces/RightMenu.ts b/packages/base-ui/src/Interfaces/RightMenu.ts
deleted file mode 100644
index 3f9514fe4b..0000000000
--- a/packages/base-ui/src/Interfaces/RightMenu.ts
+++ /dev/null
@@ -1,15 +0,0 @@
-import { BaseComponentProps } from '../BaseComponent';
-
-export interface List {
- content: React.ReactNode;
- onClick?: () => void;
- vertical?: boolean;
- type?: string;
- children?: List[];
-}
-
-// TODO remove to component file
-export interface BaseRightMenuProps extends BaseComponentProps {
- onClick?: () => void;
- style?: {};
-}
diff --git a/packages/base-ui/src/Interfaces/SheetBar.ts b/packages/base-ui/src/Interfaces/SheetBar.ts
deleted file mode 100644
index c1f702e09f..0000000000
--- a/packages/base-ui/src/Interfaces/SheetBar.ts
+++ /dev/null
@@ -1,20 +0,0 @@
-import { BooleanNumber } from '@univerjs/core';
-
-import { BaseComponentProps } from '../BaseComponent';
-import { BaseSelectProps } from '../Components';
-
-// TODO move to component file
-export interface BaseSheetBarProps extends BaseComponentProps, Omit<BaseSelectProps, 'children'> {
- children?: any[];
- index?: string;
- color?: string;
- sheetId?: string;
- style?: React.CSSProperties;
- hidden?: BooleanNumber;
- addSheet?: () => void;
- onMouseDown?: () => void;
- selectSheet?: (slideItemIndex: number) => void;
- changeSheetName?: (sheetId: string, name: string) => void;
- dragEnd?: (elements: HTMLElement[]) => void;
- selected?: boolean;
-}
diff --git a/packages/base-ui/src/Interfaces/Ul.ts b/packages/base-ui/src/Interfaces/Ul.ts
deleted file mode 100644
index 9f708b7dfa..0000000000
--- a/packages/base-ui/src/Interfaces/Ul.ts
+++ /dev/null
@@ -1,35 +0,0 @@
-import { BooleanNumber } from '@univerjs/core';
-
-import { BaseComponent, BaseComponentProps, JSXComponent } from '../BaseComponent';
-
-// TODO move to component file
-export interface BaseUlProps extends BaseComponentProps {
- label?: string | JSX.Element | string[];
- /**
- * 是否显示 选中图标
- */
- selected?: boolean;
- /**
- * 是否显示 隐藏图标
- */
- hidden?: BooleanNumber;
- icon?: JSX.Element | string | null | undefined;
- border?: boolean;
- children?: BaseUlProps[];
- onClick?: (...arg: any[]) => void;
- onKeyUp?: (...any: any[]) => void;
- onMouseDown?: (...any: any[]) => void;
- style?: React.CSSProperties;
- showSelect?: (e: MouseEvent) => void;
- getParent?: any;
- show?: boolean;
- className?: string;
- selectType?: string;
- name?: string;
- ref?: any;
- locale?: Array<string | object> | string;
- /**
- * 是否隐藏当前item
- */
- hideLi?: boolean;
-}
\ No newline at end of file
diff --git a/packages/base-ui/src/Interfaces/index.ts b/packages/base-ui/src/Interfaces/index.ts
deleted file mode 100644
index c9436615de..0000000000
--- a/packages/base-ui/src/Interfaces/index.ts
+++ /dev/null
@@ -1,5 +0,0 @@
-export * from './CountBar';
-export * from './CustomLabel';
-export * from './RightMenu';
-export * from './SheetBar';
-export * from './Ul';
diff --git a/packages/base-ui/src/index.ts b/packages/base-ui/src/index.ts
index a9d0f7af41..c84182da8c 100644
--- a/packages/base-ui/src/index.ts
+++ b/packages/base-ui/src/index.ts
@@ -5,7 +5,6 @@ export * from './Components';
export { SharedController } from './controllers/shared-shortcut.controller';
export * from './Enum';
export * from './Helpers';
-export * from './Interfaces';
export {
ICustomComponentOption,
ICustomComponentProps,
diff --git a/packages/ui-plugin-sheets/src/Controller/RightMenuUIController.ts b/packages/ui-plugin-sheets/src/Controller/RightMenuUIController.ts
index 973792bf44..d41a9b26ca 100644
--- a/packages/ui-plugin-sheets/src/Controller/RightMenuUIController.ts
+++ b/packages/ui-plugin-sheets/src/Controller/RightMenuUIController.ts
@@ -1,6 +1,6 @@
import { IMouseEvent, IPointerEvent } from '@univerjs/base-render';
import { CanvasView } from '@univerjs/base-sheets';
-import { BaseMenuItem, ComponentManager, ICustomLabelType, IMenuItemFactory, IMenuService, MenuPosition } from '@univerjs/base-ui';
+import { BaseMenuItem, BaseSelectChildrenProps, ComponentManager, IMenuItemFactory, IMenuService, MenuPosition } from '@univerjs/base-ui';
import { Disposable } from '@univerjs/core';
import { Inject, Injector } from '@wendellhu/redi';
@@ -19,7 +19,22 @@ import {
SetRowHeightMenuItemFactory,
} from './menu';
-export interface RightMenuProps extends BaseMenuItem {
+export interface ICustomLabelProps {
+ prefix?: string[] | string;
+ suffix?: string[] | string;
+ options?: BaseSelectChildrenProps[];
+ label?: string;
+ children?: ICustomLabelProps[];
+ onKeyUp?: (e: Event) => void;
+}
+
+// TODO move to right menu
+export interface ICustomLabelType {
+ name: string;
+ props?: ICustomLabelProps;
+}
+
+export interface RightMenuProps extends Omit<BaseMenuItem, 'label' | 'children'> {
label?: string | ICustomLabelType | JSX.Element;
children?: RightMenuProps[];
suffix?: string;
diff --git a/packages/ui-plugin-sheets/src/Controller/SheetBarUIController.ts b/packages/ui-plugin-sheets/src/Controller/SheetBarUIController.ts
index 6e625c36eb..22fd20e909 100644
--- a/packages/ui-plugin-sheets/src/Controller/SheetBarUIController.ts
+++ b/packages/ui-plugin-sheets/src/Controller/SheetBarUIController.ts
@@ -11,15 +11,14 @@ import {
SetWorksheetOrderMutation,
SetWorksheetShowCommand,
} from '@univerjs/base-sheets';
-import { BaseMenuItem, BaseUlProps, ColorPicker, ComponentManager, ICustomComponent, IMenuItemFactory, IMenuService } from '@univerjs/base-ui';
-import { Disposable, ICommandService, ICurrentUniverService, IKeyValue, IWorksheetConfig, Nullable, ObserverManager, UIObserver } from '@univerjs/core';
+import { BaseComponentProps, ColorPicker, ComponentManager, IMenuItemFactory, IMenuService } from '@univerjs/base-ui';
+import { BooleanNumber, Disposable, ICommandService, ICurrentUniverService, IKeyValue, IWorksheetConfig, Nullable, ObserverManager, UIObserver } from '@univerjs/core';
import { Inject, Injector, SkipSelf } from '@wendellhu/redi';
import { SHEET_UI_PLUGIN_NAME } from '../Basics/Const';
import { RenameSheetCommand } from '../commands/rename.command';
import { ShowMenuListCommand } from '../commands/unhide.command';
import { SheetBar } from '../View/SheetBar';
-
import { ISheetBarMenuItem } from '../View/SheetBar/SheetBarMenu';
import {
ChangeColorSheetMenuItemFactory,
@@ -30,11 +29,36 @@ import {
UnHideSheetMenuItemFactory,
} from './menu';
-interface SheetUl extends BaseMenuItem {
- label?: ICustomComponent | string;
- children?: SheetUl[];
+export interface BaseUlProps extends BaseComponentProps {
+ label?: string | JSX.Element | string[];
+ /**
+ * 是否显示 选中图标
+ */
+ selected?: boolean;
+ /**
+ * 是否显示 隐藏图标
+ */
+ hidden?: BooleanNumber;
+ icon?: JSX.Element | string | null | undefined;
+ border?: boolean;
+ children?: BaseUlProps[];
+ onClick?: (...arg: any[]) => void;
+ onKeyUp?: (...any: any[]) => void;
+ onMouseDown?: (...any: any[]) => void;
+ style?: React.CSSProperties;
+ showSelect?: (e: MouseEvent) => void;
+ getParent?: any;
+ show?: boolean;
+ className?: string;
+ selectType?: string;
+ name?: string;
+ ref?: any;
+ locale?: Array<string | object> | string;
+ /**
+ * 是否隐藏当前item
+ */
+ hideLi?: boolean;
}
-
export interface SheetUlProps extends BaseUlProps {
index: string;
color?: Nullable<string>;
@@ -181,7 +205,7 @@ export class SheetBarUIController extends Disposable {
hideSheet() {}
unHideSheet() {
- this._sheetBar.ref.current.showSelect();
+ (this._sheetBar?.ref?.current as IKeyValue).showSelect();
}
moveSheet(direct: string) {}
@@ -190,7 +214,7 @@ export class SheetBarUIController extends Disposable {
this._commandService.executeCommand(SetWorksheetNameCommand.id, { name, worksheetId });
};
- contextMenu(e: MouseEvent) {
+ contextMenu(e: React.MouseEvent<HTMLDivElement, MouseEvent>) {
this._sheetBar.contextMenu(e);
}
@@ -220,19 +244,19 @@ export class SheetBarUIController extends Disposable {
}
setMenuListSelect(worksheetId: string) {
- this._sheetBar.ref.current.selectItem(worksheetId);
+ (this._sheetBar?.ref?.current as IKeyValue).selectItem(worksheetId);
}
setMenuListHide(worksheetId: string) {
- this._sheetBar.ref.current.hideItem(worksheetId);
+ (this._sheetBar?.ref?.current as IKeyValue).hideItem(worksheetId);
}
setMenuListDelete(worksheetId: string) {
- this._sheetBar.ref.current.deleteItem(worksheetId);
+ (this._sheetBar?.ref?.current as IKeyValue).deleteItem(worksheetId);
}
setMenuListLabel(worksheetId: string, label: string) {
- this._sheetBar.ref.current.setItemLabel(worksheetId, label);
+ (this._sheetBar?.ref?.current as IKeyValue).setItemLabel(worksheetId, label);
}
setMenuListInsert(index: number, sheet: IWorksheetConfig, workbookId: string) {
@@ -256,11 +280,11 @@ export class SheetBarUIController extends Disposable {
}
},
};
- this._sheetBar.ref.current.insertItem(index, item);
+ (this._sheetBar?.ref?.current as IKeyValue).insertItem(index, item);
}
setMenuListOrder() {
- this._sheetBar.ref.current.setItemOrder(this._menuList);
+ (this._sheetBar?.ref?.current as IKeyValue).setItemOrder(this._menuList);
}
protected _refreshSheetBarUI(): void {
@@ -279,7 +303,7 @@ export class SheetBarUIController extends Disposable {
});
}
},
- contextMenu: (e: MouseEvent) => {
+ contextMenu: (e: React.MouseEvent<HTMLDivElement, MouseEvent>) => {
const target = e.currentTarget as HTMLDivElement;
this._dataId = target.dataset.id as string;
//this._barControl.contextMenu(e);
diff --git a/packages/ui-plugin-sheets/src/View/CountBar/CountBar.tsx b/packages/ui-plugin-sheets/src/View/CountBar/CountBar.tsx
index e3bc4df19a..25f1d78569 100644
--- a/packages/ui-plugin-sheets/src/View/CountBar/CountBar.tsx
+++ b/packages/ui-plugin-sheets/src/View/CountBar/CountBar.tsx
@@ -1,10 +1,12 @@
-import { AppContext, BaseCountBarProps, Button, CountBarComponent, Icon, JSXComponent, Slider } from '@univerjs/base-ui';
-import { ObserverManager, PLUGIN_NAMES } from '@univerjs/core';
-import { Injector } from '@wendellhu/redi';
+import { AppContext, BaseComponentProps, Button, Icon, Slider } from '@univerjs/base-ui';
import { Component, createRef } from 'react';
import styles from './index.module.less';
+export interface BaseCountBarProps extends BaseComponentProps {
+ changeRatio: (ratio: string) => void;
+}
+
interface CountState {
zoom: number;
content: string;
@@ -49,7 +51,6 @@ export class CountBar extends Component<CountBarProps, CountState> {
this.setValue({
zoom,
});
- this.ref.current.changeInputValue(0, zoom);
}
}
@@ -59,7 +60,6 @@ export class CountBar extends Component<CountBarProps, CountState> {
this.props.onChange(target.value);
}
this.setValue({ zoom: target.value });
- this.ref.current.changeInputValue(0, target.value);
};
handleClick = (e: Event, value: number | string) => {
@@ -84,29 +84,22 @@ export class CountBar extends Component<CountBarProps, CountState> {
if (this.props.onChange) {
this.props.onChange(String(value));
}
- this.ref.current.changeInputValue(0, value);
};
- override componentDidMount() {
- (this.context.injector as Injector).get(ObserverManager).getObserver<CountBar>('onCountBarDidMountObservable', PLUGIN_NAMES.SPREADSHEET)?.notifyObservers(this);
- }
-
override render() {
const { zoom, content } = this.state;
// const PageIcon = this.getComponentRender().renderFunction('PageIcon');
// const LayoutIcon = this.getComponentRender().renderFunction('LayoutIcon');
return (
<div className={styles.countBar}>
- <Button type="text" className={styles.countZoom}>
- {zoom}
- </Button>
- <Button type="text" onClick={this.addZoom}>
+ <Button className={styles.countZoom}>{zoom}</Button>
+ <Button onClick={this.addZoom}>
<Icon.Math.AddIcon />
</Button>
<div className={styles.countSlider}>
- <Slider ref={this.ref} onChange={this.onChange} value={zoom} min={this.min} max={this.max} onClick={this.handleClick} />
+ <Slider onChange={this.onChange} value={zoom} min={this.min} max={this.max} onClick={this.handleClick} />
</div>
- <Button type="text" onClick={this.reduceZoom}>
+ <Button onClick={this.reduceZoom}>
<Icon.Math.ReduceIcon />
</Button>
{/* <Button type="text">
@@ -115,7 +108,7 @@ export class CountBar extends Component<CountBarProps, CountState> {
{/* <Button type="text">
<LayoutIcon />
</Button> */}
- <Button type="text">
+ <Button>
<Icon.Sheet.RegularIcon />
</Button>
<span className={styles.countStatistic}>{content}</span>
@@ -123,9 +116,3 @@ export class CountBar extends Component<CountBarProps, CountState> {
);
}
}
-
-export class UniverCountBar implements CountBarComponent {
- render(): JSXComponent<BaseCountBarProps> {
- return CountBar;
- }
-}
diff --git a/packages/ui-plugin-sheets/src/View/RightMenu/RightMenu.tsx b/packages/ui-plugin-sheets/src/View/RightMenu/RightMenu.tsx
index ee6b4b2648..3e3241fe09 100644
--- a/packages/ui-plugin-sheets/src/View/RightMenu/RightMenu.tsx
+++ b/packages/ui-plugin-sheets/src/View/RightMenu/RightMenu.tsx
@@ -1,10 +1,15 @@
import { IMouseEvent } from '@univerjs/base-render';
-import { BaseRightMenuProps, IDisplayMenuItem, IMenuItem, Menu, MenuPosition } from '@univerjs/base-ui';
+import { BaseComponentProps, IDisplayMenuItem, IMenuItem, Menu, MenuPosition } from '@univerjs/base-ui';
import { Component, createRef } from 'react';
import { RightMenuProps } from '../../Controller';
import Style from './index.module.less';
+export interface BaseRightMenuProps extends BaseComponentProps {
+ onClick?: () => void;
+ style?: {};
+}
+
interface IState {
visible: boolean;
srcElement: any;
diff --git a/packages/ui-plugin-sheets/src/View/SheetBar/SheetBar.tsx b/packages/ui-plugin-sheets/src/View/SheetBar/SheetBar.tsx
index 9ba3b8711a..1a79fec31f 100644
--- a/packages/ui-plugin-sheets/src/View/SheetBar/SheetBar.tsx
+++ b/packages/ui-plugin-sheets/src/View/SheetBar/SheetBar.tsx
@@ -1,12 +1,27 @@
import { SetWorksheetActivateCommand } from '@univerjs/base-sheets';
-import { AppContext, BaseMenuItem, BaseSheetBarProps, Button, CustomLabel, Icon, IDisplayMenuItem, IMenuItem, Menu, MenuPosition } from '@univerjs/base-ui';
-import { ICommandService, IKeyValue } from '@univerjs/core';
+import { AppContext, BaseComponentProps, BaseMenuItem, BaseSelectProps, Button, CustomLabel, Icon, IDisplayMenuItem, IMenuItem, Menu, MenuPosition } from '@univerjs/base-ui';
+import { BooleanNumber, ICommandService, IKeyValue } from '@univerjs/core';
import { Component, createRef } from 'react';
import styles from './index.module.less';
import { ISheetBarMenuItem, SheetBarMenu } from './SheetBarMenu';
import { SlideTabBar } from './SlideTabBar/SlideTabBar';
+export interface BaseSheetBarProps extends BaseComponentProps, Omit<BaseSelectProps, 'children'> {
+ children?: any[];
+ index?: string;
+ color?: string;
+ sheetId?: string;
+ style?: React.CSSProperties;
+ hidden?: BooleanNumber;
+ addSheet?: () => void;
+ onMouseDown?: () => void;
+ selectSheet?: (slideItemIndex: number) => void;
+ changeSheetName?: (sheetId: string, name: string) => void;
+ dragEnd?: (elements: HTMLElement[]) => void;
+ selected?: boolean;
+}
+
type SheetState = {
sheetList: BaseSheetBarProps[];
menuList: BaseSheetBarProps[];
@@ -252,10 +267,10 @@ export class SheetBar extends Component<BaseSheetBarProps, SheetState> {
<div className={styles.sheetBar} ref={this.slideTabRoot}>
{/* user options button */}
<div className={styles.sheetBarOptions}>
- <Button className={styles.sheetBarOptionsButton} onClick={addSheet} type="text">
+ <Button className={styles.sheetBarOptionsButton} onClick={addSheet}>
<Icon.Math.AddIcon style={{ fontSize: '20px' }} />
</Button>
- <Button className={styles.sheetBarOptionsButton} onClick={(e: MouseEvent) => this.ref.current?.showMenu(true)} type="text">
+ <Button className={styles.sheetBarOptionsButton} onClick={(e: MouseEvent) => this.ref.current?.showMenu(true)}>
<Icon.MenuIcon style={{ fontSize: '20px' }} />
<SheetBarMenu menu={menuList as ISheetBarMenuItem[]} ref={this.ref}></SheetBarMenu>
</Button>
@@ -300,10 +315,10 @@ export class SheetBar extends Component<BaseSheetBarProps, SheetState> {
{/* prev next scroll button */}
<div className={`${styles.sheetBarOptions} ${styles.sheetBarScrollButton}`}>
- <Button type="text" className={styles.sheetBarOptionsButton} onClick={this.scrollLeft}>
+ <Button className={styles.sheetBarOptionsButton} onClick={this.scrollLeft}>
<Icon.NextIcon rotate={90} style={{ padding: '5px' }} />
</Button>
- <Button type="text" className={styles.sheetBarOptionsButton} onClick={this.scrollRight}>
+ <Button className={styles.sheetBarOptionsButton} onClick={this.scrollRight}>
<Icon.NextIcon rotate={-90} style={{ padding: '5px' }} />
</Button>
</div>