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.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
feat(sheets-data-validation): move draggable-list to design (#1822)
zhangw3acf286af967
diff --git a/packages/design/package.json b/packages/design/package.jsonindex 7c06488d5f..855b7e2b4e 100644--- a/packages/design/package.json+++ b/packages/design/package.json@@ -87,12 +87,14 @@"rc-tooltip": "^6.2.0","rc-util": "^5.39.1","react-draggable": "^4.4.6",+ "react-grid-layout": "^1.4.4","react-transition-group": "^4.4.5"},"devDependencies": {"@testing-library/react": "^14.2.2","@types/react": "^18.2.73","@types/react-dom": "^18.2.23",+ "@types/react-grid-layout": "^1.3.5","@types/react-transition-group": "^4.4.10","@univerjs/shared": "workspace:*","clsx": "^2.1.0",diff --git a/packages/design/src/components/draggable-list/DraggableList.stories.tsx b/packages/design/src/components/draggable-list/DraggableList.stories.tsxnew file mode 100644index 0000000000..870b161f81--- /dev/null+++ b/packages/design/src/components/draggable-list/DraggableList.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, { useState } from 'react';++import { DraggableList } from './DraggableList';++const meta: Meta<typeof DraggableList> = {+ title: 'Components / DraggableList',+ component: DraggableList,+ parameters: {+ layout: 'centered',+ },+ tags: ['autodocs'],+};++export default meta;++export const DraggableListDemo = {+ render() {+ const [list, setList] = useState([{ title: '1', key: '1' }, { title: '11', key: '2' }]);++ return (+ <DraggableList+ list={list}+ onListChange={setList}+ idKey="key"+ itemRender={(item) => (+ <div+ style={{ width: 120, border: '1px solid #ccc', borderRadius: 4 }}+ >+ {item.title}+ </div>+ )}+ rowHeight={32}+ margin={[0, 12]}+ />+ );+ },+};diff --git a/packages/design/src/components/draggable-list/DraggableList.tsx b/packages/design/src/components/draggable-list/DraggableList.tsxnew file mode 100644index 0000000000..65bb5d7e9d--- /dev/null+++ b/packages/design/src/components/draggable-list/DraggableList.tsx@@ -0,0 +1,72 @@+/**+ * 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 React, { useMemo } from 'react';+import RGL, { WidthProvider } from 'react-grid-layout';++const ReactGridLayout = WidthProvider(RGL);++export interface IDraggableListProps<T> extends Omit<RGL.ReactGridLayoutProps, 'layout' | 'onLayoutChange' | 'cols' | 'isResizable'> {+ list: T[];+ onListChange: (list: T[]) => void;+ idKey: keyof T;+ itemRender: (item: T, index: number) => React.ReactNode;+}++export function DraggableList<T = any>(props: IDraggableListProps<T>) {+ const { list, onListChange, idKey, itemRender, ...gridProps } = props;++ const listMap = useMemo(() => {+ const listMap = new Map<unknown, T>();+ list.forEach((item: T) => {+ const key = item[idKey];+ listMap.set(key, item);+ });+ return listMap;+ }, [idKey, list]);++ const layouts: RGL.Layout[] = useMemo(() => {+ return list.map((item, index) => ({+ i: item[idKey] as string,+ w: 12,+ h: 1,+ x: 0,+ y: index,+ col: 12,+ }));+ }, [idKey, list]);++ return (+ <ReactGridLayout+ useCSSTransforms={false}+ {...gridProps}+ cols={12}+ preventCollision={false}+ isResizable={false}+ isDraggable+ onLayoutChange={(layout) => {+ const newList = layout.sort((prev, aft) => prev.y - aft.y).map((item) => listMap.get(item.i)!);+ onListChange(newList);+ }}+ >+ {layouts.map((item, index) => (+ <div key={item.i} data-grid={item}>+ {itemRender(listMap.get(item.i)!, index)}+ </div>+ ))}+ </ReactGridLayout>+ );+}diff --git a/packages/design/src/components/draggable-list/index.ts b/packages/design/src/components/draggable-list/index.tsnew file mode 100644index 0000000000..b0c81ad2db--- /dev/null+++ b/packages/design/src/components/draggable-list/index.ts@@ -0,0 +1,18 @@+/**+ * 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.+ */++export { DraggableList } from './DraggableList';+export type { IDraggableListProps } from './DraggableList';diff --git a/packages/design/src/index.ts b/packages/design/src/index.tsindex d3a3d247d3..4759eeb74f 100644--- a/packages/design/src/index.ts+++ b/packages/design/src/index.ts@@ -47,3 +47,4 @@ export { type ITreeNodeProps, type ITreeProps, Tree, TreeSelectionMode } from '.export { enUS, zhCN } from './locale';export { type ILocale } from './locale/interface';export { defaultTheme, greenTheme, themeInstance } from './themes';+export { DraggableList, type IDraggableListProps } from './components/draggable-list';diff --git a/packages/sheets-data-validation/package.json b/packages/sheets-data-validation/package.jsonindex 4540ce50c6..be90980894 100644--- a/packages/sheets-data-validation/package.json+++ b/packages/sheets-data-validation/package.json@@ -57,6 +57,7 @@"test:watch": "vitest","coverage": "vitest run --coverage","build": "tsc && vite build"+},"peerDependencies": {"@univerjs/core": "workspace:*",@@ -69,13 +70,13 @@"@univerjs/sheets-ui": "workspace:*","@univerjs/ui": "workspace:*","@wendellhu/redi": "0.13.0",+ "clsx": ">=2.0.0","react": "^16.9.0 || ^17.0.0 || ^18.0.0","rxjs": ">=7.0.0"},"dependencies": {"@univerjs/icons": "^0.1.43",- "dayjs": "^1.11.10",- "react-draggable-list": "^4.2.0"+ "dayjs": "^1.11.10"},"devDependencies": {"@univerjs/core": "workspace:*",@@ -89,6 +90,7 @@"@univerjs/sheets-ui": "workspace:*","@univerjs/ui": "workspace:*","@wendellhu/redi": "^0.13.0",+ "clsx": "^2.1.0","less": "^4.2.0","react": "^18.2.0","rxjs": "^7.8.1",diff --git a/packages/sheets-data-validation/src/views/formula-input/list-formula-input.tsx b/packages/sheets-data-validation/src/views/formula-input/list-formula-input.tsxindex 718a6164ba..7ff4dc3b09 100644--- a/packages/sheets-data-validation/src/views/formula-input/list-formula-input.tsx+++ b/packages/sheets-data-validation/src/views/formula-input/list-formula-input.tsx@@ -16,14 +16,14 @@import { RangeSelector, useEvent } from '@univerjs/ui';import React, { useEffect, useMemo, useRef, useState } from 'react';-import { FormLayout, Input, Radio, RadioGroup, Select } from '@univerjs/design';+import { DraggableList, FormLayout, Input, Radio, RadioGroup, Select } from '@univerjs/design';import { deserializeRangeWithSheet, isReferenceString, serializeRangeWithSheet } from '@univerjs/engine-formula';import { useDependency } from '@wendellhu/redi/react-bindings';import type { IRange } from '@univerjs/core';import { IUniverInstanceService, LocaleService, Tools } from '@univerjs/core';import type { IFormulaInputProps } from '@univerjs/data-validation';import { DeleteSingle, IncreaseSingle, SequenceSingle } from '@univerjs/icons';-import DraggableList from 'react-draggable-list';+import cs from 'clsx';import { deserializeListOptions, getSheetRangeValueSet, serializeListOptions } from '../../validators/util';import { DROP_DOWN_DEFAULT_COLOR } from '../../common/const';import styles from './index.module.less';@@ -108,25 +108,15 @@ const ColorSelect = (props: IColorSelectProps) => {);};-const Template = (props: { item: IDropdownItem; dragHandleProps: any; commonProps: any }) => {- const { item, dragHandleProps, commonProps } = props;- const { onMouseDown, onTouchStart } = dragHandleProps;+const Template = (props: { item: IDropdownItem; commonProps: any; style?: React.CSSProperties }) => {+ const { item, commonProps, style } = props;const { onItemChange, onItemDelete } = commonProps;return (- <div className={styles.dataValidationFormulaListItem}>+ <div className={styles.dataValidationFormulaListItem} style={style}>{!item.isRef? (- <div- className={styles.dataValidationFormulaListItemDrag}- onTouchStart={(e) => {- e.preventDefault();- onTouchStart(e);- }}- onMouseDown={(e) => {- onMouseDown(e);- }}- >+ <div className={cs(styles.dataValidationFormulaListItemDrag, 'draggableHandle')}><SequenceSingle /></div>)@@ -136,6 +126,7 @@ const Template = (props: { item: IDropdownItem; dragHandleProps: any; commonProponChange={(color) => {onItemChange(item.id, item.label, color);}}+/><Inputdisabled={item.isRef}@@ -276,76 +267,73 @@ export function ListFormulaInput(props: IFormulaInputProps) {<Radio value="1">{localeService.t('dataValidation.list.refOptions')}</Radio></RadioGroup></FormLayout>- {isRefRange === '1' ? (- <>+ {isRefRange === '1'+ ? (+ <>+ <FormLayout error={formula1Res}>+ <RangeSelector+ id={`list-ref-range-${unitId}-${subUnitId}`}+ value={refRange}+ openForSheetUnitId={unitId}+ openForSheetSubUnitId={subUnitId}+ onChange={(ranges) => {+ const range = ranges[0];+ if (!range || isRangeInValid(range.range)) {+ onChange?.({+ formula1: '',+ formula2,+ });+ setRefRange('');+ } else {+ const workbook = univerInstanceService.getUniverSheetInstance(range.unitId) ?? univerInstanceService.getCurrentUniverSheetInstance();+ const worksheet = workbook?.getSheetBySheetId(range.sheetId) ?? workbook.getActiveSheet();+ const rangeStr = serializeRangeWithSheet(worksheet.getName(), range.range);+ onChange?.({+ formula1: rangeStr,+ formula2,+ });+ setRefRange(rangeStr);+ }+ }}+ isSingleChoice+ />+ </FormLayout>+ <FormLayout>+ <div ref={containerRef}>+ {refFinalList.map((item) => {+ return <Template key={item.id} item={item} commonProps={{ onItemChange: handleRefItemChange }} style={{ marginBottom: 12 }} />;+ })}+ </div>+ </FormLayout>+ </>+ )+ : (<FormLayout error={formula1Res}>- <RangeSelector- id={`list-ref-range-${unitId}-${subUnitId}`}- value={refRange}- openForSheetUnitId={unitId}- openForSheetSubUnitId={subUnitId}- onChange={(ranges) => {- const range = ranges[0];- if (!range || isRangeInValid(range.range)) {- onChange?.({- formula1: '',- formula2,- });- setRefRange('');- } else {- const workbook = univerInstanceService.getUniverSheetInstance(range.unitId) ?? univerInstanceService.getCurrentUniverSheetInstance();- const worksheet = workbook?.getSheetBySheetId(range.sheetId) ?? workbook.getActiveSheet();- const rangeStr = serializeRangeWithSheet(worksheet.getName(), range.range);- onChange?.({- formula1: rangeStr,- formula2,- });- setRefRange(rangeStr);- }- }}- isSingleChoice- />- </FormLayout>- <FormLayout>- <div ref={containerRef}>+ <div ref={containerRef} style={{ margin: '-12px 0' }}><DraggableList- itemKey="id"- // @ts-ignore- template={Template}- list={refFinalList}- container={() => containerRef.current}- commonProps={{- onItemChange: handleRefItemChange,- }}+ list={strList}+ onListChange={setStrList}+ rowHeight={32}+ margin={[0, 12]}+ itemRender={(item) => (+ <Template+ key={item.id}+ item={item}+ commonProps={{+ onItemChange: handleStrItemChange,+ onItemDelete: handleStrItemDelete,+ }}+ />+ )}+ idKey="id"/>+ <a className={styles.dataValidationFormulaListAdd} onClick={handleAdd}>+ <IncreaseSingle />+ {localeService.t('dataValidation.list.add')}+ </a></div></FormLayout>- </>- ) : (- <FormLayout error={formula1Res}>- <div ref={containerRef}>- <DraggableList- itemKey="id"- // @ts-ignore- template={Template}- list={strList}- onMoveEnd={(newList) => {- // @ts-ignore- setStrList(newList);- }}- container={() => containerRef.current}- commonProps={{- onItemChange: handleStrItemChange,- onItemDelete: handleStrItemDelete,- }}- />- <a className={styles.dataValidationFormulaListAdd} onClick={handleAdd}>- <IncreaseSingle />- {localeService.t('dataValidation.list.add')}- </a>- </div>- </FormLayout>- )}+ )}</>);}diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 6f9fe218a6..245700caec 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -432,6 +432,9 @@ importers:react-draggable:specifier: ^4.4.6version: 4.4.6([email protected])([email protected])+ react-grid-layout:+ specifier: ^1.4.4+ version: 1.4.4([email protected])([email protected])react-transition-group:specifier: ^4.4.5version: 4.4.5([email protected])([email protected])@@ -445,6 +448,9 @@ importers:'@types/react-dom':specifier: ^18.2.23version: 18.2.23+ '@types/react-grid-layout':+ specifier: ^1.3.5+ version: 1.3.5'@types/react-transition-group':specifier: ^4.4.10version: 4.4.10@@ -949,9 +955,6 @@ importers:dayjs:specifier: ^1.11.10version: 1.11.10- react-draggable-list:- specifier: ^4.2.0- version: 4.2.0([email protected])devDependencies:'@univerjs/core':specifier: workspace:*@@ -986,6 +989,9 @@ importers:'@wendellhu/redi':specifier: ^0.13.0version: 0.13.0+ clsx:+ specifier: ^2.1.0+ version: 2.1.0less:specifier: ^4.2.0version: 4.2.0@@ -12589,20 +12595,6 @@ packages:react: 18.2.0scheduler: 0.23.0- resolution: {integrity: sha512-3UG1cVNWdQHsqcqtuLRAtkkOkvB8seF8cY/IZQJbcyqT1Fbv7Hk8KwatAg6Ud1dWGbvrDuT9M+fkCyD3P+WHlw==}- peerDependencies:- react: ^16.6.0 || ^17.0.0- dependencies:- '@babel/runtime': 7.24.1- '@types/prop-types': 15.7.12- immutability-helper: r2.cnpmjs.org/[email protected]- prop-types: 15.8.1- react: 18.2.0- react-motion: r2.cnpmjs.org/[email protected]([email protected])- react-multi-ref: 1.0.1- dev: false-resolution: {integrity: sha512-LtY5Xw1zTPqHkVmtM3X8MUOxNDOUhv/khTgBgrUvwaS064bwVvxT+q5El0uUFNx5IEPKXuRejr7UqLwBIg5pdw==}peerDependencies:@@ -12650,7 +12642,7 @@ packages:react-dom: 18.2.0([email protected])react-draggable: 4.4.6([email protected])([email protected])react-resizable: 3.0.5([email protected])([email protected])- resize-observer-polyfill: 1.5.1+ resize-observer-polyfill: r2.cnpmjs.org/[email protected]dev: false@@ -12689,12 +12681,6 @@ packages:- react-domdev: false- resolution: {integrity: sha512-zgQKmduv95vtXIkze6583pRW7Y+mNj7R0bYgxIRWOrsEfxBQaK+MZ6yjTiZ/qcFV4bYGM74nE9isb+YRBNIw2g==}- dependencies:- '@babel/runtime': 7.24.1- dev: false-resolution: {integrity: sha512-wViHqhAd8OHeLS/IRMJjTSDHF3U9eWi62F/MledQGPdJGDhodXJ9PBLNGr6WWL7qlH12Mt3TyTpbS+hGXMjCzQ==}engines: {node: '>=0.10.0'}@@ -15417,12 +15403,6 @@ packages:requiresBuild: trueoptional: true- r2.cnpmjs.org/[email protected]:- resolution: {integrity: sha512-Q0QaXjPjwIju/28TsugCHNEASwoCcJSyJV3uO1sOIQGI0jKgm9f41Lvz0DZj3n46cNCyAZTsEYoY4C2bVRUzyQ==, registry: http://registry.npmjs.org/, tarball: https://r2.cnpmjs.org/immutability-helper/-/immutability-helper-3.1.1.tgz}- name: immutability-helper- version: 3.1.1- dev: false-r2.cnpmjs.org/[email protected]:resolution: {integrity: sha512-rKtvo6a868b5Hu3heneU+L4yEQ4jYKLtjpnPeUdK7h0yzXGmyBTypknlkCvHFBqfX9YlorEiMM6Dnq/5atfHkw==, registry: http://registry.npmjs.org/, tarball: https://r2.cnpmjs.org/import-lazy/-/import-lazy-4.0.0.tgz}name: import-lazy@@ -15506,7 +15486,7 @@ packages:yallist: r2.cnpmjs.org/[email protected]r2.cnpmjs.org/[email protected]:- resolution: {integrity: sha512-LS9X+dc8KLxXCb8dni79fLIIUA5VyZoyjSMCwTluaXA0o27cCK0bhXkpgw+sTXVpPy/lSO57ilRixqk0vDmtRA==, registry: http://registry.npmjs.org/, tarball: https://r2.cnpmjs.org/make-dir/-/make-dir-2.1.0.tgz}+ resolution: {integrity: sha512-LS9X+dc8KLxXCb8dni79fLIIUA5VyZoyjSMCwTluaXA0o27cCK0bhXkpgw+sTXVpPy/lSO57ilRixqk0vDmtRA==, tarball: https://r2.cnpmjs.org/make-dir/-/make-dir-2.1.0.tgz}name: make-dirversion: 2.1.0engines: {node: '>=6'}@@ -15524,7 +15504,7 @@ packages:semver: 6.3.1r2.cnpmjs.org/[email protected]:- resolution: {integrity: sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg==, registry: http://registry.npmjs.org/, tarball: https://r2.cnpmjs.org/mime/-/mime-1.6.0.tgz}+ resolution: {integrity: sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg==, tarball: https://r2.cnpmjs.org/mime/-/mime-1.6.0.tgz}name: mimeversion: 1.6.0engines: {node: '>=4'}@@ -15550,18 +15530,6 @@ packages:version: 1.0.7dev: false- r2.cnpmjs.org/[email protected]:- resolution: {integrity: sha512-YHk5ez1hmMR5LOkb9iJkLKqoBlL7WD5M8ljC75ZfzXriuBIVNuecaXuU7e+hOwyqf24Wxhh7Vxgt7Hnw9288Tg==, registry: http://registry.npmjs.org/, tarball: https://r2.cnpmjs.org/performance-now/-/performance-now-0.2.0.tgz}- name: performance-now- version: 0.2.0- dev: false-- r2.cnpmjs.org/[email protected]:- resolution: {integrity: sha512-7EAHlyLHI56VEIdK57uwHdHKIaAGbnXPiw0yWbarQZOKaKpvUIgW0jWRVLiatnM+XXlSwsanIBH/hzGMJulMow==, registry: http://registry.npmjs.org/, tarball: https://r2.cnpmjs.org/performance-now/-/performance-now-2.1.0.tgz}- name: performance-now- version: 2.1.0- dev: false-r2.cnpmjs.org/[email protected]:resolution: {integrity: sha512-1fygroTLlHu66zi26VoTDv8yRgm0Fccecssto+MhsZ0D/DGW2sm8E8AjW7NU5VVTRt5GxbeZ5qBuJr+HyLYkjQ==, registry: http://registry.npmjs.org/, tarball: https://r2.cnpmjs.org/picocolors/-/picocolors-1.0.0.tgz}name: picocolors@@ -15581,26 +15549,10 @@ packages:requiresBuild: trueoptional: true- r2.cnpmjs.org/[email protected]:- resolution: {integrity: sha512-Sq4CW4QhwOHE8ucn6J34MqtZCeWFP2aQSmrlroYgqAV1PjStIhJXxYuTgUIfkEk7zTLjmIjLmU5q+fbD1NnOJA==, registry: http://registry.npmjs.org/, tarball: https://r2.cnpmjs.org/raf/-/raf-3.4.1.tgz}- name: raf- version: 3.4.1- dependencies:- performance-now: r2.cnpmjs.org/[email protected]- dev: false-- r2.cnpmjs.org/[email protected]([email protected]):- resolution: {integrity: sha512-9q3YAvHoUiWlP3cK0v+w1N5Z23HXMj4IF4YuvjvWegWqNPfLXsOBE/V7UvQGpXxHFKRQQcNcVQE31g9SB/6qgQ==, registry: http://registry.npmjs.org/, tarball: https://r2.cnpmjs.org/react-motion/-/react-motion-0.5.2.tgz}- id: r2.cnpmjs.org/react-motion/0.5.2- name: react-motion- version: 0.5.2- peerDependencies:- react: ^0.14.9 || ^15.3.0 || ^16.0.0- dependencies:- performance-now: r2.cnpmjs.org/[email protected]- prop-types: 15.8.1- raf: r2.cnpmjs.org/[email protected]- react: 18.2.0+ r2.cnpmjs.org/[email protected]:+ resolution: {integrity: sha512-LwZrotdHOo12nQuZlHEmtuXdqGoOD0OhaxopaNFxWzInpEgaLWoVuAMbTzixuosCx2nEG58ngzW3vxdWoxIgdg==, tarball: https://r2.cnpmjs.org/resize-observer-polyfill/-/resize-observer-polyfill-1.5.1.tgz}+ name: resize-observer-polyfill+ version: 1.5.1dev: falser2.cnpmjs.org/[email protected]: