From c7b388099b2777ef6e7bf4cf83dc91f0f257387c Mon Sep 17 00:00:00 2001 From: "zhangyumei.0319" Date: Wed, 18 Oct 2023 19:50:41 +0800 Subject: [PATCH 1/2] chore: add change treedata case in tree/treeSelect --- packages/semi-ui/tree/_story/tree.stories.jsx | 60 ++++++++++++++++++- .../treeSelect/_story/treeSelect.stories.jsx | 59 +++++++++++++++++- 2 files changed, 116 insertions(+), 3 deletions(-) diff --git a/packages/semi-ui/tree/_story/tree.stories.jsx b/packages/semi-ui/tree/_story/tree.stories.jsx index 8ae00acaa2..19054740ea 100644 --- a/packages/semi-ui/tree/_story/tree.stories.jsx +++ b/packages/semi-ui/tree/_story/tree.stories.jsx @@ -1,4 +1,4 @@ -import React, { useRef, useState, useCallback } from 'react'; +import React, { useRef, useState, useCallback, useMemo } from 'react'; import { cloneDeep, difference, isEqual } from 'lodash'; import { IconEdit, IconMapPin, IconMore } from '@douyinfe/semi-icons'; import Tree from '../index'; @@ -2763,4 +2763,60 @@ export const UnRelatedAndAsyncLoad = () => { /> ); -}; \ No newline at end of file +}; + +const constructLargeData = () => { + const newArray = (new Array(10)).fill(0).map((item, m) => { + const parent = { + key: `key-${m}`, + label: `node-${m}`, + children: [] + } + new Array(100).fill(0).map((item, n) => { + const children = { + key: `key-${m}-${n}`, + label: `value-${m}-${n}`, + children: [] + } + new Array(10).fill(0).map((item, o) => { + const grandChildren = { + key: `key-${m}-${n}-${o}`, + label: `value-${m}-${n}-${o}`, + } + children.children.push(grandChildren); + }); + parent.children.push(children); + }); + return parent; + }); + return newArray; +} + +export const ChangeTreeData = () => { + const [sign, setSign] = useState(true); + + const treeData1 = useMemo(() => { + return constructLargeData(); + }, []); + + const treeData2 = useMemo(() => { + return constructLargeData(); + }, []); + + const onButtonClick = useCallback(() => { + setSign((sign) => { + return !sign; + }) + }, []); + + return <> + +

+ + +} diff --git a/packages/semi-ui/treeSelect/_story/treeSelect.stories.jsx b/packages/semi-ui/treeSelect/_story/treeSelect.stories.jsx index 9bcc907104..a330a2481c 100644 --- a/packages/semi-ui/treeSelect/_story/treeSelect.stories.jsx +++ b/packages/semi-ui/treeSelect/_story/treeSelect.stories.jsx @@ -5,6 +5,7 @@ import { flattenDeep } from 'lodash'; import CustomTrigger from './CustomTrigger'; import { IconCreditCard, IconChevronDown, IconClose } from '@douyinfe/semi-icons'; import { setFocusToPreviousMenuItem } from '@douyinfe/semi-foundation/utils/a11y'; +import { node } from 'prop-types'; const TreeNode = TreeSelect.TreeNode; const { Title } = Typography; @@ -2416,4 +2417,60 @@ export const UnRelatedAndAsyncLoad = () => { /> ); -}; \ No newline at end of file +}; + +const constructLargeData = () => { + const newArray = (new Array(10)).fill(0).map((item, m) => { + const parent = { + key: `key-${m}`, + label: `node-${m}`, + children: [] + } + new Array(100).fill(0).map((item, n) => { + const children = { + key: `key-${m}-${n}`, + label: `value-${m}-${n}`, + children: [] + } + new Array(10).fill(0).map((item, o) => { + const grandChildren = { + key: `key-${m}-${n}-${o}`, + label: `value-${m}-${n}-${o}`, + } + children.children.push(grandChildren); + }); + parent.children.push(children); + }); + return parent; + }); + return newArray; +} + +export const ChangeTreeData = () => { + const [sign, setSign] = useState(true); + + const treeData1 = useMemo(() => { + return constructLargeData(); + }, []); + + const treeData2 = useMemo(() => { + return constructLargeData(); + }, []); + + const onButtonClick = useCallback(() => { + setSign((sign) => { + return !sign; + }) + }, []); + + return <> + +

+ + +} From 574fd29c96b24052cfac1979753361b02645f865 Mon Sep 17 00:00:00 2001 From: YyumeiZhang <101110131+YyumeiZhang@users.noreply.github.com> Date: Mon, 30 Oct 2023 21:01:43 +0800 Subject: [PATCH 2/2] fix: [Cascader] Fixed an error caused by untimely update of data in loadingKeys (#1876) --- .../semi-foundation/cascader/foundation.ts | 26 +++++++++++++------ packages/semi-ui/cascader/index.tsx | 16 ++++++++++++ 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/packages/semi-foundation/cascader/foundation.ts b/packages/semi-foundation/cascader/foundation.ts index 3c4d3a7991..7ee5493c1e 100644 --- a/packages/semi-foundation/cascader/foundation.ts +++ b/packages/semi-foundation/cascader/foundation.ts @@ -230,7 +230,11 @@ export interface CascaderAdapter extends DefaultAdapter void; notifyOnExceed: (data: BasicEntity[]) => void; toggleInputShow: (show: boolean, cb: () => void) => void; - updateFocusState: (focus: boolean) => void + updateFocusState: (focus: boolean) => void; + updateLoadingKeyRefValue: (keys: Set) => void; + getLoadingKeyRefValue: () => Set; + updateLoadedKeyRefValue: (keys: Set) => void; + getLoadedKeyRefValue: () => Set } export default class CascaderFoundation extends BaseFoundation { @@ -242,6 +246,8 @@ export default class CascaderFoundation extends BaseFoundation loadingKeys.has(i)); @@ -672,18 +678,19 @@ export default class CascaderFoundation extends BaseFoundation { clickOutsideHandler: any; mergeType: string; context: ContextValue; + loadingKeysRef: React.RefObject | null>; + loadedKeysRef: React.RefObject | null>; constructor(props: CascaderProps) { super(props); @@ -269,6 +271,8 @@ class Cascader extends BaseComponent { this.optionsRef = React.createRef(); this.clickOutsideHandler = null; this.foundation = new CascaderFoundation(this.adapter); + this.loadingKeysRef = React.createRef(); + this.loadedKeysRef = React.createRef(); } get adapter(): CascaderAdapter { @@ -389,6 +393,18 @@ class Cascader extends BaseComponent { updateFocusState: (isFocus: boolean) => { this.setState({ isFocus }); }, + updateLoadingKeyRefValue: (keys: Set) => { + (this.loadingKeysRef as any).current = keys; + }, + getLoadingKeyRefValue: () => { + return this.loadingKeysRef.current; + }, + updateLoadedKeyRefValue: (keys: Set) => { + (this.loadedKeysRef as any).current = keys; + }, + getLoadedKeyRefValue: () => { + return this.loadedKeysRef.current; + } }; }