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): implement InputWithSlot component (#1094)

Wenzhao Huda19aaf62a03
diff --git a/examples/package.json b/examples/package.json
index 39d03ce640..f31e98e603 100644
--- a/examples/package.json
+++ b/examples/package.json
@@ -16,7 +16,7 @@
"@univerjs/engine-formula": "workspace:*",
"@univerjs/find-replace": "workspace:*",
"@univerjs/engine-render": "workspace:*",
- "@univerjs/icons": "^0.1.24",
+ "@univerjs/icons": "^0.1.25",
"@univerjs/rpc": "workspace:*",
"@univerjs/sheets": "workspace:*",
"@univerjs/sheets-formula": "workspace:*",
diff --git a/packages/design/package.json b/packages/design/package.json
index 7aaf2e76b7..7ea6296772 100644
--- a/packages/design/package.json
+++ b/packages/design/package.json
@@ -49,7 +49,7 @@
"dependencies": {
"@rc-component/color-picker": "^1.4.1",
"@rc-component/trigger": "^1.18.2",
- "@univerjs/icons": "^0.1.24",
+ "@univerjs/icons": "^0.1.25",
"clsx": "^2.0.0",
"rc-dialog": "^9.3.4",
"rc-dropdown": "^4.1.0",
diff --git a/packages/design/src/components/input/InputWithSlot.stories.tsx b/packages/design/src/components/input/InputWithSlot.stories.tsx
new file mode 100644
index 0000000000..74093fdf59
--- /dev/null
+++ b/packages/design/src/components/input/InputWithSlot.stories.tsx
@@ -0,0 +1,54 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * 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 type { Meta } from '@storybook/react';
+import React from 'react';
+
+import { Pager } from '../pager';
+import { InputWithSlot } from './InputWithSlot';
+
+const meta: Meta<typeof InputWithSlot> = {
+ title: 'Components / InputWithSlot',
+ component: InputWithSlot,
+ parameters: {
+ layout: 'centered',
+ },
+ argTypes: {
+ slot: {
+ control: {
+ type: 'boolean',
+ default: true,
+ // options: ['empty', 'demo'],
+ },
+ mapping: {
+ false: [],
+ true: <Pager value={3} total={10} />,
+ },
+ },
+ },
+};
+
+export default meta;
+
+function Demo() {
+ return <InputWithSlot slot={<Pager value={3} total={10} />} />;
+}
+
+export const InputWithSlotDemo = {
+ return() {
+ return <Demo />;
+ },
+};
diff --git a/packages/design/src/components/input/InputWithSlot.tsx b/packages/design/src/components/input/InputWithSlot.tsx
new file mode 100644
index 0000000000..2fdd42c37d
--- /dev/null
+++ b/packages/design/src/components/input/InputWithSlot.tsx
@@ -0,0 +1,77 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * 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.
+ */
+
+/* eslint-disable no-magic-numbers */
+
+import React, { useEffect, useRef } from 'react';
+
+import styles from './index.module.less';
+import { type IInputProps, Input } from './Input';
+
+export interface IInputWithSlotProps extends IInputProps {
+ /**
+ * A React node that would be rendered in the slot of the input component.
+ */
+ slot: React.ReactNode;
+}
+
+function getPaddingRightBySize(size?: 'mini' | 'small' | 'middle' | 'large'): number {
+ switch (size) {
+ case 'mini':
+ return 2;
+ case 'small':
+ return 6;
+ case 'middle':
+ return 6;
+ case 'large':
+ return 8;
+ default:
+ return 6;
+ }
+}
+
+export function InputWithSlot(props: IInputWithSlotProps) {
+ const { slot, ...rest } = props;
+ const elementRef = useRef<HTMLDivElement>(null);
+ const [paddingRight, setPaddingRight] = React.useState(getPaddingRightBySize(props.size));
+
+ useEffect(() => {
+ let observer: ResizeObserver | null;
+ if (elementRef.current) {
+ observer = new ResizeObserver(() => {
+ // detectWidth
+ const rightPadding = getPaddingRightBySize(props.size) * 2 + elementRef.current!.offsetWidth;
+ setPaddingRight(rightPadding);
+ });
+
+ observer.observe(elementRef.current);
+ }
+
+ return () => {
+ observer?.disconnect();
+ };
+ }, [slot, elementRef.current, props.size]);
+
+ return (
+ <div className={styles.inputSlotContainer}>
+ <Input {...rest} affixWrapperStyle={{ paddingRight }} />
+ {/* Pointer down should not causing focus change. */}
+ <div ref={elementRef} className={styles.inputSlot} tabIndex={-1} onPointerDown={(e) => e.preventDefault()}>
+ {slot}
+ </div>
+ </div>
+ );
+}
diff --git a/packages/design/src/components/input/index.module.less b/packages/design/src/components/input/index.module.less
index cf6745a0e1..d7b39bd8ee 100644
--- a/packages/design/src/components/input/index.module.less
+++ b/packages/design/src/components/input/index.module.less
@@ -93,4 +93,19 @@
display: none;
}
}
+
+ &-slot-container {
+ position: relative;
+ display: inline-block;
+ }
+
+ &-slot {
+ position: absolute;
+ top: 0;
+ right: 8px;
+
+ display: inline-flex;
+
+ height: 100%;
+ }
}
diff --git a/packages/design/src/components/input/index.ts b/packages/design/src/components/input/index.ts
index e2b85cbf1f..561ca46fcb 100644
--- a/packages/design/src/components/input/index.ts
+++ b/packages/design/src/components/input/index.ts
@@ -15,3 +15,4 @@
*/
export { type IInputProps, Input } from './Input';
+export { type IInputWithSlotProps, InputWithSlot } from './InputWithSlot';
diff --git a/packages/design/src/components/pager/Pager.tsx b/packages/design/src/components/pager/Pager.tsx
index f0518aa829..cc458c94d2 100644
--- a/packages/design/src/components/pager/Pager.tsx
+++ b/packages/design/src/components/pager/Pager.tsx
@@ -14,6 +14,7 @@
* limitations under the License.
*/
+import { MoreLeftSingle, MoreRightSingle } from '@univerjs/icons';
import React, { useMemo } from 'react';
import styles from './index.module.less';
@@ -53,11 +54,11 @@ export function Pager(props: IPagerProps) {
return (
<div className={styles.pager}>
<div role="button" className={styles.pagerLeftArrow} onClick={onClickLeftArrow}>
- ⬅️
+ <MoreLeftSingle />
</div>
<div className={styles.pagerNumber}>{text}</div>
<div role="button" className={styles.pagerRightArrow} onClick={onClickRightArrow}>
- ➡️
+ <MoreRightSingle />
</div>
</div>
);
diff --git a/packages/design/src/components/pager/index.module.less b/packages/design/src/components/pager/index.module.less
index 27e20d996d..0c26db4d4e 100644
--- a/packages/design/src/components/pager/index.module.less
+++ b/packages/design/src/components/pager/index.module.less
@@ -16,6 +16,7 @@
height: 16px;
font-size: 8px;
+ color: rgb(var(--grey-400));
border-radius: 4px;
@@ -26,7 +27,7 @@
&-number {
margin: 0 4px;
- font-style: 13px;
+ font-size: 13px;
color: rgb(var(--text-color));
}
}
diff --git a/packages/sheets-formula/package.json b/packages/sheets-formula/package.json
index e36897dd37..cc130aea2c 100644
--- a/packages/sheets-formula/package.json
+++ b/packages/sheets-formula/package.json
@@ -47,7 +47,7 @@
"@univerjs/docs": "workspace:*",
"@univerjs/engine-formula": "workspace:*",
"@univerjs/engine-render": "workspace:*",
- "@univerjs/icons": "^0.1.24",
+ "@univerjs/icons": "^0.1.25",
"@univerjs/sheets": "workspace:*",
"@univerjs/sheets-ui": "workspace:*",
"@univerjs/ui": "workspace:*",
diff --git a/packages/sheets-numfmt/package.json b/packages/sheets-numfmt/package.json
index 0aa32d11ae..1a31b95cf2 100644
--- a/packages/sheets-numfmt/package.json
+++ b/packages/sheets-numfmt/package.json
@@ -49,7 +49,7 @@
"@univerjs/design": "workspace:*",
"@univerjs/engine-numfmt": "workspace:*",
"@univerjs/engine-render": "workspace:*",
- "@univerjs/icons": "^0.1.24",
+ "@univerjs/icons": "^0.1.25",
"@univerjs/sheets": "workspace:*",
"@univerjs/sheets-ui": "workspace:*",
"@univerjs/ui": "workspace:*",
diff --git a/packages/sheets-ui/package.json b/packages/sheets-ui/package.json
index 7f234f9be5..2c1e4b4503 100644
--- a/packages/sheets-ui/package.json
+++ b/packages/sheets-ui/package.json
@@ -52,7 +52,7 @@
"@univerjs/docs": "workspace:*",
"@univerjs/engine-formula": "workspace:*",
"@univerjs/engine-render": "workspace:*",
- "@univerjs/icons": "^0.1.24",
+ "@univerjs/icons": "^0.1.25",
"@univerjs/sheets": "workspace:*",
"@univerjs/ui": "workspace:*",
"@univerjs/engine-numfmt": "workspace:*",
diff --git a/packages/sheets-zen-editor/package.json b/packages/sheets-zen-editor/package.json
index 06702792fb..02b2e2905a 100644
--- a/packages/sheets-zen-editor/package.json
+++ b/packages/sheets-zen-editor/package.json
@@ -47,7 +47,7 @@
"@univerjs/design": "workspace:*",
"@univerjs/docs": "workspace:*",
"@univerjs/engine-render": "workspace:*",
- "@univerjs/icons": "^0.1.24",
+ "@univerjs/icons": "^0.1.25",
"@univerjs/sheets": "workspace:*",
"@univerjs/sheets-ui": "workspace:*",
"@univerjs/ui": "workspace:*",
diff --git a/packages/ui/package.json b/packages/ui/package.json
index dce2725114..3ec278557b 100644
--- a/packages/ui/package.json
+++ b/packages/ui/package.json
@@ -50,7 +50,7 @@
"@univerjs/core": "workspace:*",
"@univerjs/design": "workspace:*",
"@univerjs/engine-render": "workspace:*",
- "@univerjs/icons": "^0.1.24",
+ "@univerjs/icons": "^0.1.25",
"@wendellhu/redi": "^0.12.13",
"clsx": "^2.0.0",
"localforage": "^1.10.0",
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 4be0a8b13b..4baea9eefb 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -159,8 +159,8 @@ importers:
specifier: workspace:*
version: link:../packages/find-replace
'@univerjs/icons':
- specifier: ^0.1.24
+ specifier: ^0.1.25
'@univerjs/rpc':
specifier: workspace:*
version: link:../packages/rpc
@@ -290,8 +290,8 @@ importers:
specifier: ^1.18.2
'@univerjs/icons':
- specifier: ^0.1.24
+ specifier: ^0.1.25
clsx:
specifier: ^2.0.0
version: 2.0.0
@@ -882,8 +882,8 @@ importers:
specifier: workspace:*
version: link:../engine-render
'@univerjs/icons':
- specifier: ^0.1.24
+ specifier: ^0.1.25
'@univerjs/sheets':
specifier: workspace:*
version: link:../sheets
@@ -995,8 +995,8 @@ importers:
specifier: workspace:*
version: link:../engine-render
'@univerjs/icons':
- specifier: ^0.1.24
+ specifier: ^0.1.25
'@univerjs/sheets':
specifier: workspace:*
version: link:../sheets
@@ -1065,8 +1065,8 @@ importers:
specifier: workspace:*
version: link:../engine-render
'@univerjs/icons':
- specifier: ^0.1.24
+ specifier: ^0.1.25
'@univerjs/sheets':
specifier: workspace:*
version: link:../sheets
@@ -1132,8 +1132,8 @@ importers:
specifier: workspace:*
version: link:../engine-render
'@univerjs/icons':
- specifier: ^0.1.24
+ specifier: ^0.1.25
'@univerjs/sheets':
specifier: workspace:*
version: link:../sheets
@@ -1288,8 +1288,8 @@ importers:
specifier: workspace:*
version: link:../engine-render
'@univerjs/icons':
- specifier: ^0.1.24
+ specifier: ^0.1.25
'@wendellhu/redi':
specifier: ^0.12.13
version: 0.12.13
@@ -5850,8 +5850,8 @@ packages:
resolution: {integrity: sha512-zuVdFrMJiuCDQUMCzQaD6KL28MjnqqN8XnAqiEq9PNm/hCPTSGfrXCOfwj1ow4LFb/tNymJPwsNbVePc1xFqrQ==}
dev: true
- resolution: {integrity: sha512-+5MaebxDHTKWqpDI0avHgXZVWnyspqjgp5spM2WXJFNXjCcRB9H8o8du9IZloi3KWXMNz43J1hih++Z9j/gzfg==}
+ resolution: {integrity: sha512-9kJUZFKV2bcc6+SDn/6xJSefZX97EvyE1n9m7WOX2l8qclQE+HNjLt041NnsthTTXh/8JYq6bYQus2jmzJSHyw==}
peerDependencies:
react: '*'
react-dom: '*'