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

feat(TreeSelect): support customValue props #3400

Merged
merged 1 commit into from
Dec 26, 2024
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
39 changes: 23 additions & 16 deletions src/tree-select/tree-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ export default class TreeSelect extends SuperComponent {
scrollIntoView: null,
};

properties = props;
properties = {
...props,
customValue: {
// 用于自定义选中值,优先级高于value,用于弥补value为[]场景
type: null,
value: null,
},
};

controlledProps = [
{
Expand All @@ -39,7 +46,7 @@ export default class TreeSelect extends SuperComponent {
];

observers = {
'value, options, keys, multiple'() {
'value, customValue, options, keys, multiple'() {
this.buildTreeOptions();
},
};
Expand All @@ -52,9 +59,8 @@ export default class TreeSelect extends SuperComponent {

methods = {
buildTreeOptions() {
const { options, value, multiple, keys } = this.data;
const { options, value, defaultValue, customValue, multiple, keys } = this.data;
const treeOptions = [];
const innerValue = [];

let level = -1;
let node = { children: options };
Expand All @@ -68,7 +74,7 @@ export default class TreeSelect extends SuperComponent {
value: item[keys?.value || 'value'],
children: item.children,
}));
const thisValue = value?.[level];
const thisValue = customValue?.[level] || value?.[level];

treeOptions.push([...list]);

Expand All @@ -83,33 +89,34 @@ export default class TreeSelect extends SuperComponent {

const leafLevel = Math.max(0, level);

treeOptions?.forEach((ele, idx) => {
const v = idx === treeOptions.length - 1 && multiple ? [ele[0].value] : ele[0].value;
innerValue.push(value?.[idx] || v);
});

if (multiple) {
const finalValue = this.data.value || this.data.defaultValue;
const finalValue = customValue || value || defaultValue;
if (finalValue[leafLevel] != null && !Array.isArray(finalValue[leafLevel])) {
throw TypeError('应传入数组类型的 value');
}
}

this.setData({
innerValue,
innerValue:
customValue ||
treeOptions?.map((ele, idx) => {
const v = idx === treeOptions.length - 1 && multiple ? [ele[0].value] : ele[0].value;
return value?.[idx] || v;
}),
leafLevel,
treeOptions,
});
},

getScrollIntoView(status: string) {
const { value, scrollIntoView } = this.data;
const { value, customValue, scrollIntoView } = this.data;
if (status === 'init') {
const scrollIntoView = Array.isArray(value)
? value.map((item) => {
const _value = customValue || value;
const scrollIntoView = Array.isArray(_value)
? _value.map((item) => {
return Array.isArray(item) ? item[0] : item;
})
: [value];
: [_value];
this.setData({
scrollIntoView,
});
Expand Down
2 changes: 1 addition & 1 deletion src/tree-select/tree-select.wxml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
<t-checkbox-group
wx:else
class="{{classPrefix}}__checkbox {{prefix}}-class-right-column"
value="{{innerValue[level]}}"
value="{{innerValue[level] || []}}"
bind:change="handleRadioChange"
data-level="{{level}}"
>
Expand Down
Loading