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(design): improve type definitions and refactor components for better clarity (#5131)

白熱c599b08496dc
diff --git a/packages/design/src/components/dropdown-menu/DropdownMenu.tsx b/packages/design/src/components/dropdown-menu/DropdownMenu.tsx
index fc543d760c..48495f19f6 100644
--- a/packages/design/src/components/dropdown-menu/DropdownMenu.tsx
+++ b/packages/design/src/components/dropdown-menu/DropdownMenu.tsx
@@ -26,23 +26,35 @@ import {
DropdownMenuTrigger,
} from './DropdownMenuPrimitive';
-type DropdownMenu = {
+interface IDropdownMenuNormalItem {
type: 'item';
className?: string;
children: ReactNode;
disabled?: boolean;
onSelect?: (item: DropdownMenu) => void;
-} | {
- type?: 'separator';
+}
+
+interface IDropdownMenuSeparatorItem {
+ type: 'separator';
className?: string;
-} | {
- type?: 'radio';
+}
+
+interface IDropdownMenuOption {
+ label?: ReactNode;
+ value?: string;
+ disabled?: boolean;
+}
+
+interface IDropdownMenuRadioItem {
+ type: 'radio';
className?: string;
value: string;
hideIndicator?: boolean;
- options: { value: string; label: ReactNode; disabled?: boolean }[];
+ options: (IDropdownMenuOption | IDropdownMenuSeparatorItem)[];
onSelect?: (item: string) => void;
-};
+}
+
+type DropdownMenu = IDropdownMenuNormalItem | IDropdownMenuSeparatorItem | IDropdownMenuRadioItem;
export interface IDropdownMenuProps extends ComponentProps<typeof DropdownMenuContent> {
children: ReactNode;
@@ -78,7 +90,6 @@ export function DropdownMenu(props: IDropdownMenuProps) {
}
function renderMenuItem(item: DropdownMenu, index: number) {
- // const { type, children, icon, checked, hidden, onSelect } = item;
const { className, type } = item;
if (type === 'separator') {
@@ -91,16 +102,28 @@ export function DropdownMenu(props: IDropdownMenuProps) {
value={item.value}
onValueChange={item.onSelect}
>
- {item.options.map((option) => (
- <DropdownMenuRadioItem
- key={option.value}
- value={option.value}
- disabled={option.disabled}
- hideIndicator={item.hideIndicator}
- >
- {option.label}
- </DropdownMenuRadioItem>
- ))}
+ {item.options.map((option, index) => {
+ if ('type' in option) {
+ if (option.type === 'separator') {
+ return <DropdownMenuSeparator key={index} className={className} />;
+ }
+ } else {
+ if (option.value === undefined) {
+ throw new Error('[DropdownMenu]: `value` is required');
+ }
+ return (
+ <DropdownMenuRadioItem
+ key={option.value}
+ value={option.value}
+ disabled={option.disabled}
+ hideIndicator={item.hideIndicator}
+ >
+ {option.label}
+ </DropdownMenuRadioItem>
+ );
+ }
+ return null;
+ })}
</DropdownMenuRadioGroup>
);
} else if (type === 'item') {
diff --git a/packages/design/src/components/select/Select.tsx b/packages/design/src/components/select/Select.tsx
index 54d0c993da..07cd37a3f0 100644
--- a/packages/design/src/components/select/Select.tsx
+++ b/packages/design/src/components/select/Select.tsx
@@ -24,9 +24,14 @@ import { DropdownMenu } from '../dropdown-menu/DropdownMenu';
interface IOption {
label?: string | ReactNode;
value?: string;
+ disabled?: boolean;
options?: IOption[];
}
+interface IOptionSeparator {
+ type: 'separator';
+}
+
export interface ISelectProps {
className?: string;
@@ -83,15 +88,34 @@ export function Select(props: ISelectProps) {
}
const items: IDropdownMenuProps['items'] = useMemo(() => {
+ const selectOptions: (IOption | IOptionSeparator)[] = [];
+
+ for (const option of options) {
+ if (option.options) {
+ option.options.forEach((opt) => {
+ selectOptions.push({
+ label: opt.label,
+ value: opt.value!,
+ disabled: opt.disabled,
+ });
+ });
+ selectOptions.push({
+ type: 'separator',
+ });
+ } else {
+ selectOptions.push({
+ label: option.label,
+ value: option.value!,
+ disabled: option.disabled,
+ });
+ }
+ }
+
return [{
type: 'radio',
value,
hideIndicator: true,
- options: options.map((option) => ({
- label: option.label,
- value: option.value!,
- disabled: false,
- })),
+ options: selectOptions,
onSelect: (item) => {
onChange(item);
},
@@ -99,8 +123,25 @@ export function Select(props: ISelectProps) {
}, [options]);
const displayValue = useMemo(() => {
- const selectedOption = options.find((option) => option.value === value);
- return selectedOption ? selectedOption.label : '';
+ let label = null;
+
+ for (const option of options) {
+ if (option.options) {
+ for (const opt of option.options) {
+ if (opt.value === value) {
+ label = opt.label;
+ break;
+ }
+ }
+ } else {
+ if (option.value === value) {
+ label = option.label;
+ break;
+ }
+ }
+ }
+
+ return label || value;
}, [options, value]);
return (
diff --git a/packages/sheets-filter-ui/src/views/components/SheetsFilterPanel.tsx b/packages/sheets-filter-ui/src/views/components/SheetsFilterPanel.tsx
index 3558be2a68..096ad38580 100644
--- a/packages/sheets-filter-ui/src/views/components/SheetsFilterPanel.tsx
+++ b/packages/sheets-filter-ui/src/views/components/SheetsFilterPanel.tsx
@@ -70,7 +70,7 @@ export function FilterPanel() {
return (
<div
- data-u-comp-sheets-filter-panel
+ data-u-comp="sheets-filter-panel"
className={`
univer-box-border univer-flex univer-h-[432px] univer-w-[312px] univer-flex-col univer-rounded-lg
univer-bg-white univer-p-4 univer-shadow-lg
@@ -82,7 +82,7 @@ export function FilterPanel() {
sharedProps={{ range, colIndex, onClose: onCancel }}
/>
<div
- data-u-comp-sheets-filter-panel-header
+ data-u-comp="sheets-filter-panel"
className="univer-mb-1 univer-flex-shrink-0 univer-flex-grow-0"
>
<Segmented
@@ -94,7 +94,7 @@ export function FilterPanel() {
{filterByModel
? (
<div
- data-u-comp-sheets-filter-panel-content
+ data-u-comp="sheets-filter-panel-content"
className="univer-flex-shrink univer-flex-grow univer-pt-2"
>
{filterBy === FilterBy.VALUES
@@ -106,7 +106,7 @@ export function FilterPanel() {
<div style={{ flex: 1 }} />
)}
<div
- data-u-comp-sheets-filter-panel-footer
+ data-u-comp="sheets-filter-panel-footer"
className={`
univer-mt-4 univer-inline-flex univer-flex-shrink-0 univer-flex-grow-0 univer-flex-nowrap
univer-justify-between univer-overflow-hidden
diff --git a/packages/sheets-ui/src/views/dropdown/list-dropdown/index.tsx b/packages/sheets-ui/src/views/dropdown/list-dropdown/index.tsx
index 005425007d..4de15ce56e 100644
--- a/packages/sheets-ui/src/views/dropdown/list-dropdown/index.tsx
+++ b/packages/sheets-ui/src/views/dropdown/list-dropdown/index.tsx
@@ -73,51 +73,49 @@ const SelectList = (props: ISelectListProps) => {
<div className={styles.dvListDropdownTitle}>
{title}
</div>
- <div className="univer-max-h-52">
- <div
- key={filter}
- className={clsx('univer-px-2', scrollbarClassName)}
- >
- {filteredOptions.map((item, i) => {
- const selected = value.indexOf(item.value) > -1;
- const handleClick = () => {
- let set: Set<string>;
- if (selected) {
- set = new Set(value.filter((sub) => sub !== item.value));
- } else {
- set = new Set(multiple ? [...value, item.value] : [item.value]);
+ <div
+ key={filter}
+ className={clsx('univer-max-h-52 univer-overflow-y-auto univer-px-2', scrollbarClassName)}
+ >
+ {filteredOptions.map((item, i) => {
+ const selected = value.indexOf(item.value) > -1;
+ const handleClick = () => {
+ let set: Set<string>;
+ if (selected) {
+ set = new Set(value.filter((sub) => sub !== item.value));
+ } else {
+ set = new Set(multiple ? [...value, item.value] : [item.value]);
+ }
+ const newValue: string[] = [];
+ options.forEach((opt) => {
+ if (set.has(opt.value)) {
+ newValue.push(opt.value);
}
- const newValue: string[] = [];
- options.forEach((opt) => {
- if (set.has(opt.value)) {
- newValue.push(opt.value);
- }
- });
+ });
- onChange(newValue);
- };
+ onChange(newValue);
+ };
- const index = item.label.toLocaleLowerCase().indexOf(lowerFilter!);
- return (
- <div key={i} className={styles.dvListDropdownItemContainer} onClick={handleClick}>
- <div className={styles.dvListDropdownItem} style={{ background: item.color }}>
- {lowerFilter && item.label.toLowerCase().includes(lowerFilter)
- ? (
- <>
- <span>{item.label.substring(0, index)}</span>
- <span style={{ fontWeight: 'bold' }}>{item.label.substring(index, index + lowerFilter.length)}</span>
- <span>{item.label.substring(index + lowerFilter.length)}</span>
- </>
- )
- : item.label}
- </div>
- <div className={styles.dvListDropdownSelectedIcon}>
- {selected ? <CheckMarkSingle /> : null}
- </div>
+ const index = item.label.toLocaleLowerCase().indexOf(lowerFilter!);
+ return (
+ <div key={i} className={styles.dvListDropdownItemContainer} onClick={handleClick}>
+ <div className={styles.dvListDropdownItem} style={{ background: item.color }}>
+ {lowerFilter && item.label.toLowerCase().includes(lowerFilter)
+ ? (
+ <>
+ <span>{item.label.substring(0, index)}</span>
+ <span style={{ fontWeight: 'bold' }}>{item.label.substring(index, index + lowerFilter.length)}</span>
+ <span>{item.label.substring(index + lowerFilter.length)}</span>
+ </>
+ )
+ : item.label}
</div>
- );
- })}
- </div>
+ <div className={styles.dvListDropdownSelectedIcon}>
+ {selected ? <CheckMarkSingle /> : null}
+ </div>
+ </div>
+ );
+ })}
</div>
{showEditOnDropdown && hasPermission
? (