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

improvement(treepicker): always using renderValue when value is exist #1139

Merged
merged 3 commits into from
Jul 2, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 19 additions & 13 deletions src/CheckTreePicker/CheckTreePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1210,19 +1210,25 @@ class CheckTreePicker extends React.Component<CheckTreePickerProps, CheckTreePic
const selectedItems = this.getSelectedItems(selectedValues);
let selectedElement: React.ReactNode = placeholder;

if (hasValue && selectedValues.length) {
selectedElement = (
<SelectedElement
selectedItems={selectedItems}
countable={countable}
valueKey={valueKey}
labelKey={labelKey}
prefix={this.addPrefix}
cascade={cascade}
locale={locale}
/>
);
if (renderValue) {
/**
* 如果 value 不合法,同时没有设置 renderValue, 则忽略值,显示 placeholder
* 如果 value 不合法,但是设置了 renderValue, 则执行 renderValue 并显示
*/
if (selectedValues.length) {
if (hasValue) {
selectedElement = (
<SelectedElement
selectedItems={selectedItems}
countable={countable}
valueKey={valueKey}
labelKey={labelKey}
prefix={this.addPrefix}
cascade={cascade}
locale={locale}
/>
);
}
if (_.isFunction(renderValue)) {
superman66 marked this conversation as resolved.
Show resolved Hide resolved
selectedElement = renderValue(selectedValues, selectedItems, selectedElement);
}
}
Expand Down
32 changes: 23 additions & 9 deletions src/TreePicker/TreePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,18 @@ class TreePicker extends React.Component<TreePickerProps, TreePickerState> {

this.focusNode(activeNode);
this.unserializeLists('expand', expandItemValues);

let newState = {};
if (activeNode) {
newState = { activeNode: activeNode };
}
this.setState({
data: nextData,
filterData,
activeNode,
expandItemValues: this.serializeList('expand')
...{
data: nextData,
filterData,
expandItemValues: this.serializeList('expand')
},
...newState
});
}
}
Expand Down Expand Up @@ -1107,15 +1114,22 @@ class TreePicker extends React.Component<TreePickerProps, TreePickerState> {
style,
...rest
} = this.props;
const { activeNode } = this.state;
const { selectedValue, activeNode } = this.state;
const classes = getToggleWrapperClassName('tree', this.addPrefix, this.props, !!activeNode);

let selectedElement: React.ReactNode = placeholder;
const hasValue = !!activeNode;
if (hasValue) {
selectedElement = activeNode?.[labelKey];
if (renderValue && activeNode) {
selectedElement = renderValue(activeNode[valueKey], activeNode, selectedElement);
/**
* 如果 value 不合法,同时没有设置 renderValue, 则忽略值,显示 placeholder
* 如果 value 不合法,但是设置了 renderValue, 则执行 renderValue 并显示
*/
if (!_.isNil(selectedValue)) {
if (hasValue) {
selectedElement = activeNode[labelKey];
}
if (_.isFunction(renderValue)) {
const node = activeNode ?? {};
selectedElement = renderValue(node[valueKey], node, selectedElement);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/TreePicker/test/TreePickerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ describe('TreePicker', () => {
{ label: '2', value: '2' }
]}
value={'2'}
renderValue={(value, item, selectedElement) => `Selected: ${item.label}`}
renderValue={(value, item) => `Selected: ${item.label}`}
/>
);

Expand Down