Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cascader): fix multiple load usage #3376

Merged
merged 1 commit into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions src/cascader/_example/load.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import React, { useState } from 'react';
import { Cascader } from 'tdesign-react';
import { Cascader, Space } from 'tdesign-react';
import type { CascaderProps, CascaderValue } from 'tdesign-react';

import type { TreeOptionData } from '../../common';

export default function Example() {
const [value1, setValue1] = useState<CascaderValue>([]);
const [singleValue, setSingleValue] = useState<CascaderValue>('');
const [multipleValue, setMultipleValue] = useState<CascaderValue>([]);

const options = [
{
label: '选项1',
Expand All @@ -19,8 +21,12 @@ export default function Example() {
},
];

const onChange1: CascaderProps['onChange'] = (value) => {
setValue1(value);
const onSingleChange: CascaderProps['onChange'] = (value) => {
setSingleValue(value);
};

const onMultipleChange: CascaderProps['onChange'] = (value) => {
setMultipleValue(value);
};

const load: CascaderProps['load'] = (node) =>
Expand All @@ -40,8 +46,13 @@ export default function Example() {
];
}
resolve(nodes);
}, 1000);
}, 300);
});

return <Cascader options={options} value={value1} onChange={onChange1} load={load} />;
return (
<Space direction="vertical">
<Cascader options={options} value={singleValue} onChange={onSingleChange} load={load} />
<Cascader multiple options={options} value={multipleValue} onChange={onMultipleChange} load={load} />
</Space>
);
}
17 changes: 14 additions & 3 deletions src/cascader/hooks.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { useState, useEffect, useMemo } from 'react';
import { useState, useEffect, useMemo, useRef } from 'react';

import { isEqual , isFunction } from 'lodash-es';
import { isEqual, isFunction } from 'lodash-es';

import TreeStore from '../_common/js/tree-v1/tree-store';
import { getTreeValue, getCascaderValue, isEmptyValues, isValueInvalid } from './core/helper';
import { treeNodesEffect, treeStoreExpendEffect } from './core/effect';

import useControlled from '../hooks/useControlled';

import type {
TreeNode,
TreeNodeValue,
Expand Down Expand Up @@ -83,6 +84,15 @@ export const useCascaderContext = (props: TdCascaderProps) => {
*/

const { disabled, options = [], keys = {}, checkStrictly = false, lazy = true, load, valueMode = 'onlyLeaf' } = props;

const optionCurrent = useRef(options);

useEffect(() => {
if (!isEqual(optionCurrent.current, options)) {
optionCurrent.current = options;
}
}, [options]);

useEffect(() => {
if (!treeStore) {
if (!options.length) return;
Expand All @@ -99,6 +109,7 @@ export const useCascaderContext = (props: TdCascaderProps) => {
},
});
store.append(options as Array<TypeTreeNodeData>);

setTreeStore(store);
} else {
treeStore.reload(options);
Expand All @@ -107,7 +118,7 @@ export const useCascaderContext = (props: TdCascaderProps) => {
treeNodesEffect(inputVal, treeStore, setTreeNodes, props.filter, checkStrictly);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [options]);
}, [optionCurrent.current]);

useEffect(() => {
if (!treeStore) return;
Expand Down
117 changes: 89 additions & 28 deletions test/snap/__snapshots__/csr.test.jsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -19502,42 +19502,103 @@ exports[`csr snapshot test > csr test src/cascader/_example/keys.tsx 1`] = `
exports[`csr snapshot test > csr test src/cascader/_example/load.tsx 1`] = `
<div>
<div
class="t-cascader t-select-input t-select-input--empty"
class="t-space t-space-vertical"
style="gap: 16px;"
>
<div
class="t-input__wrap"
spellcheck="false"
value=""
class="t-space-item"
>
<div
class="t-input t-is-readonly t-align-left t-input--suffix"
class="t-cascader t-select-input t-select-input--empty"
>
<input
class="t-input__inner"
placeholder="select cascader data"
readonly=""
type="text"
<div
class="t-input__wrap"
spellcheck="false"
value=""
/>
<span
class="t-input__suffix t-input__suffix-icon"
>
<svg
class="t-fake-arrow t-cascader__icon"
fill="none"
height="16"
viewBox="0 0 16 16"
width="16"
xmlns="http://www.w3.org/2000/svg"
<div
class="t-input t-is-readonly t-align-left t-input--suffix"
>
<path
d="M3.75 5.7998L7.99274 10.0425L12.2361 5.79921"
stroke="black"
stroke-opacity="0.9"
stroke-width="1.3"
<input
class="t-input__inner"
placeholder="select cascader data"
readonly=""
type="text"
value=""
/>
</svg>
</span>
<span
class="t-input__suffix t-input__suffix-icon"
>
<svg
class="t-fake-arrow t-cascader__icon"
fill="none"
height="16"
viewBox="0 0 16 16"
width="16"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M3.75 5.7998L7.99274 10.0425L12.2361 5.79921"
stroke="black"
stroke-opacity="0.9"
stroke-width="1.3"
/>
</svg>
</span>
</div>
</div>
</div>
</div>
<div
class="t-space-item"
>
<div
class="t-cascader t-select-input t-select-input--multiple t-select-input--empty"
>
<div
class="t-input__wrap t-tag-input t-tag-input--break-line t-tag-input__with-suffix-icon t-is-empty"
spellcheck="false"
value=""
>
<div
class="t-input t-is-readonly t-align-left t-input--prefix t-input--suffix"
>
<div
class="t-input__prefix"
/>
<input
class="t-input__inner"
placeholder="select cascader data"
readonly=""
type="text"
value=""
/>
<span
class="t-input__input-pre"
>
select cascader data
</span>
<span
class="t-input__suffix t-input__suffix-icon"
>
<svg
class="t-fake-arrow t-cascader__icon"
fill="none"
height="16"
viewBox="0 0 16 16"
width="16"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M3.75 5.7998L7.99274 10.0425L12.2361 5.79921"
stroke="black"
stroke-opacity="0.9"
stroke-width="1.3"
/>
</svg>
</span>
</div>
</div>
</div>
</div>
</div>
Expand Down Expand Up @@ -136715,7 +136776,7 @@ exports[`ssr snapshot test > ssr test src/cascader/_example/filterable.tsx 1`] =

exports[`ssr snapshot test > ssr test src/cascader/_example/keys.tsx 1`] = `"<div style="gap:16px" class="t-space t-space-vertical"><div class="t-space-item"><div class="t-cascader t-select-input t-select-input--empty"><div class="t-input__wrap" value="" spellcheck="false"><div class="t-input t-is-readonly t-align-left t-input--suffix"><input placeholder="请选择" type="text" class="t-input__inner" readonly="" value=""/><span class="t-input__suffix t-input__suffix-icon"><svg class="t-fake-arrow t-cascader__icon" width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M3.75 5.7998L7.99274 10.0425L12.2361 5.79921" stroke="black" stroke-opacity="0.9" stroke-width="1.3"></path></svg></span></div></div></div></div><div class="t-space-item"><div class="t-cascader t-select-input t-select-input--multiple t-select-input--empty"><div class="t-input__wrap t-tag-input t-tag-input--break-line t-tag-input__with-suffix-icon t-is-empty" value="" spellcheck="false"><div class="t-input t-is-readonly t-align-left t-input--prefix t-input--suffix"><div class="t-input__prefix"></div><input placeholder="请选择" type="text" class="t-input__inner" readonly="" value=""/><span class="t-input__input-pre">请选择</span><span class="t-input__suffix t-input__suffix-icon"><svg class="t-fake-arrow t-cascader__icon" width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M3.75 5.7998L7.99274 10.0425L12.2361 5.79921" stroke="black" stroke-opacity="0.9" stroke-width="1.3"></path></svg></span></div></div></div></div></div>"`;

exports[`ssr snapshot test > ssr test src/cascader/_example/load.tsx 1`] = `"<div class="t-cascader t-select-input t-select-input--empty"><div class="t-input__wrap" value="" spellcheck="false"><div class="t-input t-is-readonly t-align-left t-input--suffix"><input placeholder="请选择" type="text" class="t-input__inner" readonly="" value=""/><span class="t-input__suffix t-input__suffix-icon"><svg class="t-fake-arrow t-cascader__icon" width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M3.75 5.7998L7.99274 10.0425L12.2361 5.79921" stroke="black" stroke-opacity="0.9" stroke-width="1.3"></path></svg></span></div></div></div>"`;
exports[`ssr snapshot test > ssr test src/cascader/_example/load.tsx 1`] = `"<div style="gap:16px" class="t-space t-space-vertical"><div class="t-space-item"><div class="t-cascader t-select-input t-select-input--empty"><div class="t-input__wrap" value="" spellcheck="false"><div class="t-input t-is-readonly t-align-left t-input--suffix"><input placeholder="请选择" type="text" class="t-input__inner" readonly="" value=""/><span class="t-input__suffix t-input__suffix-icon"><svg class="t-fake-arrow t-cascader__icon" width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M3.75 5.7998L7.99274 10.0425L12.2361 5.79921" stroke="black" stroke-opacity="0.9" stroke-width="1.3"></path></svg></span></div></div></div></div><div class="t-space-item"><div class="t-cascader t-select-input t-select-input--multiple t-select-input--empty"><div class="t-input__wrap t-tag-input t-tag-input--break-line t-tag-input__with-suffix-icon t-is-empty" value="" spellcheck="false"><div class="t-input t-is-readonly t-align-left t-input--prefix t-input--suffix"><div class="t-input__prefix"></div><input placeholder="请选择" type="text" class="t-input__inner" readonly="" value=""/><span class="t-input__input-pre">请选择</span><span class="t-input__suffix t-input__suffix-icon"><svg class="t-fake-arrow t-cascader__icon" width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M3.75 5.7998L7.99274 10.0425L12.2361 5.79921" stroke="black" stroke-opacity="0.9" stroke-width="1.3"></path></svg></span></div></div></div></div></div>"`;

exports[`ssr snapshot test > ssr test src/cascader/_example/max.tsx 1`] = `"<div class="t-cascader t-select-input t-select-input--multiple t-select-input--empty"><div class="t-input__wrap t-tag-input t-tag-input--break-line t-tag-input__with-suffix-icon t-is-empty" value="" spellcheck="false"><div class="t-input t-is-readonly t-align-left t-input--prefix t-input--suffix"><div class="t-input__prefix"></div><input placeholder="请选择" type="text" class="t-input__inner" readonly="" value=""/><span class="t-input__input-pre">请选择</span><span class="t-input__suffix t-input__suffix-icon"><svg class="t-fake-arrow t-cascader__icon" width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M3.75 5.7998L7.99274 10.0425L12.2361 5.79921" stroke="black" stroke-opacity="0.9" stroke-width="1.3"></path></svg></span></div></div></div>"`;

Expand Down
2 changes: 1 addition & 1 deletion test/snap/__snapshots__/ssr.test.jsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ exports[`ssr snapshot test > ssr test src/cascader/_example/filterable.tsx 1`] =

exports[`ssr snapshot test > ssr test src/cascader/_example/keys.tsx 1`] = `"<div style="gap:16px" class="t-space t-space-vertical"><div class="t-space-item"><div class="t-cascader t-select-input t-select-input--empty"><div class="t-input__wrap" value="" spellcheck="false"><div class="t-input t-is-readonly t-align-left t-input--suffix"><input placeholder="请选择" type="text" class="t-input__inner" readonly="" value=""/><span class="t-input__suffix t-input__suffix-icon"><svg class="t-fake-arrow t-cascader__icon" width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M3.75 5.7998L7.99274 10.0425L12.2361 5.79921" stroke="black" stroke-opacity="0.9" stroke-width="1.3"></path></svg></span></div></div></div></div><div class="t-space-item"><div class="t-cascader t-select-input t-select-input--multiple t-select-input--empty"><div class="t-input__wrap t-tag-input t-tag-input--break-line t-tag-input__with-suffix-icon t-is-empty" value="" spellcheck="false"><div class="t-input t-is-readonly t-align-left t-input--prefix t-input--suffix"><div class="t-input__prefix"></div><input placeholder="请选择" type="text" class="t-input__inner" readonly="" value=""/><span class="t-input__input-pre">请选择</span><span class="t-input__suffix t-input__suffix-icon"><svg class="t-fake-arrow t-cascader__icon" width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M3.75 5.7998L7.99274 10.0425L12.2361 5.79921" stroke="black" stroke-opacity="0.9" stroke-width="1.3"></path></svg></span></div></div></div></div></div>"`;

exports[`ssr snapshot test > ssr test src/cascader/_example/load.tsx 1`] = `"<div class="t-cascader t-select-input t-select-input--empty"><div class="t-input__wrap" value="" spellcheck="false"><div class="t-input t-is-readonly t-align-left t-input--suffix"><input placeholder="请选择" type="text" class="t-input__inner" readonly="" value=""/><span class="t-input__suffix t-input__suffix-icon"><svg class="t-fake-arrow t-cascader__icon" width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M3.75 5.7998L7.99274 10.0425L12.2361 5.79921" stroke="black" stroke-opacity="0.9" stroke-width="1.3"></path></svg></span></div></div></div>"`;
exports[`ssr snapshot test > ssr test src/cascader/_example/load.tsx 1`] = `"<div style="gap:16px" class="t-space t-space-vertical"><div class="t-space-item"><div class="t-cascader t-select-input t-select-input--empty"><div class="t-input__wrap" value="" spellcheck="false"><div class="t-input t-is-readonly t-align-left t-input--suffix"><input placeholder="请选择" type="text" class="t-input__inner" readonly="" value=""/><span class="t-input__suffix t-input__suffix-icon"><svg class="t-fake-arrow t-cascader__icon" width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M3.75 5.7998L7.99274 10.0425L12.2361 5.79921" stroke="black" stroke-opacity="0.9" stroke-width="1.3"></path></svg></span></div></div></div></div><div class="t-space-item"><div class="t-cascader t-select-input t-select-input--multiple t-select-input--empty"><div class="t-input__wrap t-tag-input t-tag-input--break-line t-tag-input__with-suffix-icon t-is-empty" value="" spellcheck="false"><div class="t-input t-is-readonly t-align-left t-input--prefix t-input--suffix"><div class="t-input__prefix"></div><input placeholder="请选择" type="text" class="t-input__inner" readonly="" value=""/><span class="t-input__input-pre">请选择</span><span class="t-input__suffix t-input__suffix-icon"><svg class="t-fake-arrow t-cascader__icon" width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M3.75 5.7998L7.99274 10.0425L12.2361 5.79921" stroke="black" stroke-opacity="0.9" stroke-width="1.3"></path></svg></span></div></div></div></div></div>"`;

exports[`ssr snapshot test > ssr test src/cascader/_example/max.tsx 1`] = `"<div class="t-cascader t-select-input t-select-input--multiple t-select-input--empty"><div class="t-input__wrap t-tag-input t-tag-input--break-line t-tag-input__with-suffix-icon t-is-empty" value="" spellcheck="false"><div class="t-input t-is-readonly t-align-left t-input--prefix t-input--suffix"><div class="t-input__prefix"></div><input placeholder="请选择" type="text" class="t-input__inner" readonly="" value=""/><span class="t-input__input-pre">请选择</span><span class="t-input__suffix t-input__suffix-icon"><svg class="t-fake-arrow t-cascader__icon" width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M3.75 5.7998L7.99274 10.0425L12.2361 5.79921" stroke="black" stroke-opacity="0.9" stroke-width="1.3"></path></svg></span></div></div></div>"`;

Expand Down
Loading