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.

fix(sheets-ui): sync border style selection (#7838)

白熱e7b5824c6f6c
diff --git a/packages/sheets-ui/src/views/border-panel/BorderPanel.tsx b/packages/sheets-ui/src/views/border-panel/BorderPanel.tsx
index e2c1171e50..99a4d8e201 100644
--- a/packages/sheets-ui/src/views/border-panel/BorderPanel.tsx
+++ b/packages/sheets-ui/src/views/border-panel/BorderPanel.tsx
@@ -19,29 +19,14 @@ import type { IBorderInfo } from '@univerjs/sheets';
import type { IBorderPanelProps } from './interface';
import { BorderStyleTypes } from '@univerjs/core';
import { clsx, ColorPicker, ConfigContext, Dropdown, Separator } from '@univerjs/design';
-import { CheckMarkIcon, MoreDownIcon, PaintBucketDoubleIcon } from '@univerjs/icons';
-import { BorderStyleManagerService, SheetsSelectionsService } from '@univerjs/sheets';
+import { MoreDownIcon, PaintBucketDoubleIcon } from '@univerjs/icons';
+import { SheetsSelectionsService } from '@univerjs/sheets';
import { IconManager, useDependency } from '@univerjs/ui';
import { useContext } from 'react';
+
import { BorderLine } from './border-line/BorderLine';
import { BORDER_LINE_CHILDREN, BORDER_SIZE_CHILDREN } from './interface';
-function getBorderColor(borderData: Nullable<IBorderData>): string | undefined {
- if (!borderData) return;
- for (const key in borderData) {
- const border = borderData[key as keyof IBorderData];
- if (border?.cl?.rgb) return border.cl.rgb;
- }
-}
-
-function getBorderStyle(borderData: Nullable<IBorderData>): BorderStyleTypes | undefined {
- if (!borderData) return;
- for (const key in borderData) {
- const border = borderData[key as keyof IBorderData];
- if (border?.cl?.rgb) return border.s;
- }
-}
-
export interface IResponsiveBorderPanelProps extends IBorderPanelProps {
ColorPickerComponent?: typeof ColorPicker;
DropdownComponent?: typeof Dropdown;
@@ -49,16 +34,22 @@ export interface IResponsiveBorderPanelProps extends IBorderPanelProps {
export function BorderPanel(props: IResponsiveBorderPanelProps) {
const iconManager = useDependency(IconManager);
- const borderStyleManagerService = useDependency(BorderStyleManagerService);
const selectionManagerService = useDependency(SheetsSelectionsService);
const { direction } = useContext(ConfigContext);
const { isAllValuesSame, value: currentValue } = selectionManagerService.getCellStylesProperty('bd');
const color = isAllValuesSame ? getBorderColor(currentValue as Nullable<IBorderData>) : undefined;
- const type = isAllValuesSame ? getBorderStyle(currentValue as Nullable<IBorderData>) : undefined;
const { onChange, value, ColorPickerComponent = ColorPicker, DropdownComponent = Dropdown } = props;
+ function getBorderColor(borderData: Nullable<IBorderData>): string | undefined {
+ if (!borderData) return;
+ for (const key in borderData) {
+ const border = borderData[key as keyof IBorderData];
+ if (border?.cl?.rgb) return border.cl.rgb;
+ }
+ }
+
function handleClick(v: string | number, type: keyof IBorderInfo) {
onChange?.({
...value,
@@ -89,7 +80,7 @@ export function BorderPanel(props: IResponsiveBorderPanelProps) {
hover:univer-bg-gray-100
dark:hover:!univer-bg-gray-700
`, {
- 'univer-bg-gray-200 dark:!univer-bg-gray-600': borderStyleManagerService.getBorderInfo().type === item.value,
+ 'univer-bg-gray-200 dark:!univer-bg-gray-600': value.type === item.value,
})}
onClick={(e) => {
e.stopPropagation();
@@ -139,28 +130,30 @@ export function BorderPanel(props: IResponsiveBorderPanelProps) {
<section className="univer-rounded-lg univer-p-1.5">
<ul className="univer-m-0 univer-grid univer-list-none univer-gap-1 univer-p-0">
{BORDER_SIZE_CHILDREN.map((item) => (
- <li
- key={item.value}
- className={`
- univer-flex univer-cursor-pointer univer-items-center
- univer-justify-center univer-rounded univer-px-1 univer-py-3
- hover:univer-bg-gray-100
- dark:hover:!univer-bg-gray-700
- `}
- onClick={() => handleClick(item.value, 'style')}
- >
- {item.value === type && (
- <CheckMarkIcon
- className="univer-absolute univer-left-3 univer-text-primary-600"
+ <li key={item.value}>
+ <button
+ type="button"
+ aria-label={BorderStyleTypes[item.value]}
+ aria-pressed={item.value === value.style}
+ className={clsx(`
+ univer-flex univer-w-full univer-cursor-pointer univer-items-center
+ univer-justify-center univer-rounded univer-border-none
+ univer-bg-transparent univer-px-1 univer-py-3
+ hover:univer-bg-gray-100
+ dark:hover:!univer-bg-gray-700
+ `, {
+ 'univer-bg-gray-200 dark:!univer-bg-gray-600': item.value === value.style,
+ })}
+ onClick={() => handleClick(item.value, 'style')}
+ >
+ <BorderLine
+ className={`
+ univer-fill-gray-900
+ dark:!univer-fill-gray-0
+ `}
+ type={item.value}
/>
- )}
- <BorderLine
- className={`
- univer-ml-6 univer-fill-gray-900
- dark:!univer-fill-gray-0
- `}
- type={item.value}
- />
+ </button>
</li>
))}
</ul>
@@ -181,7 +174,7 @@ export function BorderPanel(props: IResponsiveBorderPanelProps) {
univer-fill-gray-900
dark:!univer-fill-gray-0
`}
- type={type ?? BorderStyleTypes.THIN}
+ type={value.style}
/>
<MoreDownIcon className="dark:!univer-text-gray-0" />
</button>
diff --git a/packages/sheets-ui/src/views/border-panel/__tests__/BorderPanel.spec.tsx b/packages/sheets-ui/src/views/border-panel/__tests__/BorderPanel.spec.tsx
index 421afc3e2e..349c197e52 100644
--- a/packages/sheets-ui/src/views/border-panel/__tests__/BorderPanel.spec.tsx
+++ b/packages/sheets-ui/src/views/border-panel/__tests__/BorderPanel.spec.tsx
@@ -18,13 +18,17 @@ import type { IBorderInfo } from '@univerjs/sheets';
import type { Root } from 'react-dom/client';
import { BorderStyleTypes, BorderType, Univer } from '@univerjs/core';
import { ConfigProvider } from '@univerjs/design';
-import { BorderStyleManagerService, SheetsSelectionsService } from '@univerjs/sheets';
+import * as icons from '@univerjs/icons';
+import { SheetsSelectionsService } from '@univerjs/sheets';
import { IconManager, RediContext } from '@univerjs/ui';
import { act } from 'react';
import { createRoot } from 'react-dom/client';
-import { afterEach, describe, expect, it } from 'vitest';
+import { describe, expect, it, onTestFinished } from 'vitest';
+
import { BorderPanel } from '../BorderPanel';
+(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
+
class TestSheetsSelectionsService {
getCellStylesProperty() {
return {
@@ -39,57 +43,45 @@ class TestSheetsSelectionsService {
}
}
-class TestState {
- static changes: IBorderInfo[] = [];
-
- static reset(): void {
- this.changes = [];
- }
-}
-
function renderPanel(value: IBorderInfo, direction: 'ltr' | 'rtl' = 'ltr') {
const univer = new Univer();
const injector = univer.__getInjector();
- injector.add([BorderStyleManagerService]);
injector.add([IconManager]);
injector.add([SheetsSelectionsService, { useClass: TestSheetsSelectionsService as never }]);
+ injector.get(IconManager).register(icons);
const container = document.createElement('div');
document.body.appendChild(container);
const root: Root = createRoot(container);
+ const changes: IBorderInfo[] = [];
- act(() => {
+ function render(value: IBorderInfo) {
root.render(
<RediContext.Provider value={{ injector }}>
<ConfigProvider mountContainer={document.body} direction={direction}>
<BorderPanel
value={value}
- onChange={(nextValue) => TestState.changes.push(nextValue)}
+ onChange={(nextValue) => {
+ changes.push(nextValue);
+ render(nextValue);
+ }}
/>
</ConfigProvider>
</RediContext.Provider>
);
- });
+ }
- return {
- container,
- dispose: () => {
- act(() => root.unmount());
- container.remove();
- univer.dispose();
- },
- };
-}
+ act(() => render(value));
+ onTestFinished(() => {
+ act(() => root.unmount());
+ container.remove();
+ univer.dispose();
+ });
-function hasClassToken(element: HTMLElement, token: string): boolean {
- return element.className.split(/\s+/).includes(token);
+ return { container, changes };
}
describe('BorderPanel', () => {
- afterEach(() => {
- TestState.reset();
- });
-
it('keeps the current border color and style when selecting a new border type', () => {
const rendered = renderPanel({
type: BorderType.ALL,
@@ -103,7 +95,7 @@ describe('BorderPanel', () => {
(borderTypeItems[0] as HTMLElement).click();
});
- expect(TestState.changes).toEqual([
+ expect(rendered.changes).toEqual([
{
type: BorderType.TOP,
color: '#123456',
@@ -111,24 +103,38 @@ describe('BorderPanel', () => {
activeBorderType: true,
},
]);
- rendered.dispose();
});
- it('keeps color and line dropdown icon order compatible with rtl', () => {
+ it.each(['ltr', 'rtl'] as const)('keeps the line preview and selected option in sync in %s', async (direction) => {
const rendered = renderPanel({
type: BorderType.ALL,
color: '#123456',
- style: BorderStyleTypes.THIN,
+ style: BorderStyleTypes.DOTTED,
activeBorderType: true,
- }, 'rtl');
+ }, direction);
+
+ expect(rendered.container.querySelector('section')?.getAttribute('dir')).toBe(direction);
+ const trigger = rendered.container.querySelectorAll('button')[1];
+ await act(async () => trigger.click());
- expect(rendered.container.querySelector('section')?.getAttribute('dir')).toBe('rtl');
- const dropdownButtons = rendered.container.querySelectorAll('button');
+ const options = document.body.querySelectorAll('li');
+ const dottedPreview = options[2].querySelector('svg')!;
+ expect(trigger.querySelector('svg')?.innerHTML).toBe(dottedPreview.innerHTML);
+ expect(options[2].querySelector('button')?.getAttribute('aria-pressed')).toBe('true');
- expect(dropdownButtons).toHaveLength(2);
- dropdownButtons.forEach((button) => {
- expect(hasClassToken(button, 'rtl:univer-flex-row-reverse')).toBe(false);
+ const dashedPreview = options[3].querySelector('svg')!;
+ await act(async () => {
+ dashedPreview.dispatchEvent(new MouseEvent('click', { bubbles: true }));
});
- rendered.dispose();
+
+ expect(rendered.changes).toEqual([{
+ type: BorderType.ALL,
+ color: '#123456',
+ style: BorderStyleTypes.DASHED,
+ activeBorderType: true,
+ }]);
+ expect(trigger.querySelector('svg')?.innerHTML).toBe(dashedPreview.innerHTML);
+ expect(options[3].querySelector('button')?.getAttribute('aria-pressed')).toBe('true');
+ expect(options[2].querySelector('button')?.getAttribute('aria-pressed')).toBe('false');
});
});
diff --git a/packages/sheets-ui/src/views/border-panel/border-line/BorderLine.tsx b/packages/sheets-ui/src/views/border-panel/border-line/BorderLine.tsx
index a014c72a77..7f37af7492 100644
--- a/packages/sheets-ui/src/views/border-panel/border-line/BorderLine.tsx
+++ b/packages/sheets-ui/src/views/border-panel/border-line/BorderLine.tsx
@@ -19,6 +19,7 @@ import { BorderStyleTypes } from '@univerjs/core';
import { BorderDashDot } from './icons/BorderDashDot';
import { BorderDashDotDot } from './icons/BorderDashDotDot';
import { BorderDashed } from './icons/BorderDashed';
+import { BorderDotted } from './icons/BorderDotted';
import { BorderDouble } from './icons/BorderDouble';
import { BorderHair } from './icons/BorderHair';
import { BorderMedium } from './icons/BorderMedium';
@@ -43,6 +44,8 @@ export function BorderLine(props: IBorderLineProps) {
return <BorderDashDotDot className={className} />;
case BorderStyleTypes.DASHED:
return <BorderDashed className={className} />;
+ case BorderStyleTypes.DOTTED:
+ return <BorderDotted className={className} />;
case BorderStyleTypes.HAIR:
return <BorderHair className={className} />;
case BorderStyleTypes.MEDIUM:
diff --git a/packages/sheets-ui/src/views/border-panel/border-line/__tests__/BorderLine.spec.tsx b/packages/sheets-ui/src/views/border-panel/border-line/__tests__/BorderLine.spec.tsx
new file mode 100644
index 0000000000..f9767c50b8
--- /dev/null
+++ b/packages/sheets-ui/src/views/border-panel/border-line/__tests__/BorderLine.spec.tsx
@@ -0,0 +1,31 @@
+/**
+ * Copyright 2023-present DreamNum Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import { renderToStaticMarkup } from 'react-dom/server';
+import { describe, expect, it } from 'vitest';
+
+import { BORDER_SIZE_CHILDREN } from '../../interface';
+import { BorderLine } from '../BorderLine';
+
+describe('BorderLine', () => {
+ it('renders a distinct preview for every selectable border style', () => {
+ const previews = BORDER_SIZE_CHILDREN.map(({ value }) => renderToStaticMarkup(
+ <BorderLine type={value} className="" />
+ ));
+
+ expect(new Set(previews).size).toBe(previews.length);
+ });
+});