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.

feat(design): support rtl for input number (#6780)

白熱2640d638d244
diff --git a/common/debugger/src/views/Fab.tsx b/common/debugger/src/views/Fab.tsx
index fc5a98af0a..054ae65ad0 100644
--- a/common/debugger/src/views/Fab.tsx
+++ b/common/debugger/src/views/Fab.tsx
@@ -29,6 +29,7 @@ import { useFloatingDom } from './use-floating-dom';
import { useLocale } from './use-locale';
import { useMessage } from './use-message';
import { useNotification } from './use-notification';
+import { useRTL } from './use-rtl';
import { useSidebar } from './use-sidebar';
import { useSnapshot } from './use-snapshot';
import { useTheme } from './use-theme';
@@ -43,6 +44,7 @@ export function Fab() {
const fabEntryUnitType = configs?.fabEntryUnitType;
const locale = useLocale();
+ const rtl = useRTL();
const darkMode = useDarkMode();
const theme = useTheme();
const watermark = useWatermark();
@@ -64,6 +66,7 @@ export function Fab() {
const items: IDropdownMenuProps['items'] = [
locale,
+ rtl,
darkMode,
theme,
watermark,
diff --git a/common/debugger/src/views/use-rtl.ts b/common/debugger/src/views/use-rtl.ts
new file mode 100644
index 0000000000..c0fe07cc73
--- /dev/null
+++ b/common/debugger/src/views/use-rtl.ts
@@ -0,0 +1,45 @@
+/**
+ * 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 { useEffect } from 'react';
+
+function applyDirection(value: 'ltr' | 'rtl') {
+ document.documentElement.setAttribute('dir', value);
+}
+
+export function useRTL() {
+ useEffect(() => {
+ const dir = localStorage.getItem('local.direction');
+
+ if (dir === 'rtl' || dir === 'ltr') {
+ applyDirection(dir);
+ }
+ }, []);
+
+ const onSelect = () => {
+ const current = document.documentElement.getAttribute('dir');
+ const nextDirection = current === 'rtl' ? 'ltr' : 'rtl';
+
+ applyDirection(nextDirection);
+ localStorage.setItem('local.direction', nextDirection);
+ };
+
+ return {
+ type: 'item' as const,
+ children: '↔️ Toggle RTL',
+ onSelect,
+ };
+}
diff --git a/packages/design/src/components/input-number/InputNumber.stories.tsx b/packages/design/src/components/input-number/InputNumber.stories.tsx
index ebf159b4d9..f51fffc1ad 100644
--- a/packages/design/src/components/input-number/InputNumber.stories.tsx
+++ b/packages/design/src/components/input-number/InputNumber.stories.tsx
@@ -66,3 +66,13 @@ export const InputNumberHideControls = {
);
},
};
+
+export const InputNumberRtl = {
+ render() {
+ return (
+ <div dir="rtl">
+ <InputNumber defaultValue={12} />
+ </div>
+ );
+ },
+};
diff --git a/packages/design/src/components/input-number/InputNumber.tsx b/packages/design/src/components/input-number/InputNumber.tsx
index fd1f7a7bc8..e440ce1532 100644
--- a/packages/design/src/components/input-number/InputNumber.tsx
+++ b/packages/design/src/components/input-number/InputNumber.tsx
@@ -16,7 +16,7 @@
import type { FocusEvent, InputHTMLAttributes, KeyboardEvent } from 'react';
import { forwardRef, useEffect, useRef, useState } from 'react';
-import { borderLeftClassName } from '../../helper/class-utilities';
+import { borderLeftClassName, borderLeftRTLClassName } from '../../helper/class-utilities';
import { clsx } from '../../helper/clsx';
import { Input } from '../input/Input';
@@ -339,9 +339,12 @@ export const InputNumber = forwardRef<HTMLInputElement, IInputNumberProps>(
before:univer-absolute before:univer-top-1/2 before:univer-block before:univer-h-px
before:univer-w-full before:-univer-translate-y-1/2 before:univer-bg-gray-200
before:univer-content-[""]
+ rtl:univer-left-px rtl:univer-right-auto rtl:univer-rounded-l-md
+ rtl:univer-rounded-r-none
dark:before:!univer-bg-gray-600
`,
borderLeftClassName,
+ borderLeftRTLClassName,
controlsClassName
)}
>
diff --git a/packages/design/src/components/input-number/__tests__/index.spec.tsx b/packages/design/src/components/input-number/__tests__/index.spec.tsx
index 9293633f69..3f143cf2e4 100644
--- a/packages/design/src/components/input-number/__tests__/index.spec.tsx
+++ b/packages/design/src/components/input-number/__tests__/index.spec.tsx
@@ -299,4 +299,28 @@ describe('InputNumber', () => {
expect(onKeyDown).not.toHaveBeenCalled();
expect(onLocalChange).not.toHaveBeenCalled();
});
+
+ it('should support rtl layout classes and keep increment/decrement behavior', () => {
+ const onLocalChange = vi.fn();
+ const { container } = render(
+ <div dir="rtl">
+ <InputNumber defaultValue={1} onChange={onLocalChange} />
+ </div>
+ );
+
+ const incrementButton = container.querySelector('[aria-label="increment"]') as HTMLElement;
+ const decrementButton = container.querySelector('[aria-label="decrement"]') as HTMLElement;
+ const controlsWrapper = incrementButton.parentElement as HTMLElement;
+
+ expect(controlsWrapper.className).toContain('rtl:univer-left-px');
+ expect(controlsWrapper.className).toContain('rtl:univer-right-auto');
+ expect(controlsWrapper.className).toContain('rtl:univer-rounded-l-md');
+ expect(controlsWrapper.className).toContain('rtl:univer-border-r');
+
+ fireEvent.click(incrementButton);
+ expect(onLocalChange).toHaveBeenCalledWith(2);
+
+ fireEvent.click(decrementButton);
+ expect(onLocalChange).toHaveBeenCalledWith(1);
+ });
});
diff --git a/packages/design/src/helper/class-utilities.ts b/packages/design/src/helper/class-utilities.ts
index b6860c5c5b..a73d58075b 100644
--- a/packages/design/src/helper/class-utilities.ts
+++ b/packages/design/src/helper/class-utilities.ts
@@ -22,6 +22,7 @@ const borderBasicClassName = 'univer-border-gray-200 dark:!univer-border-gray-60
export const borderClassName = clsx(borderBasicClassName, 'univer-border');
export const borderLeftBottomClassName = clsx(borderBasicClassName, 'univer-border-l univer-border-b univer-border-t-0 univer-border-r-0');
export const borderLeftClassName = clsx(borderBasicClassName, 'univer-border-l univer-border-b-0 univer-border-t-0 univer-border-r-0');
+export const borderLeftRTLClassName = clsx(borderBasicClassName, 'univer-border-b-0 univer-border-t-0 rtl:univer-border-l-0 rtl:univer-border-r');
export const borderTopClassName = clsx(borderBasicClassName, 'univer-border-l-0 univer-border-b-0 univer-border-t univer-border-r-0');
export const borderBottomClassName = clsx(borderBasicClassName, 'univer-border-l-0 univer-border-b univer-border-t-0 univer-border-r-0');
export const borderRightClassName = clsx(borderBasicClassName, 'univer-border-l-0 univer-border-b-0 univer-border-t-0 univer-border-r');