Skip to content

Commit

Permalink
feat: support titleRender (#447)
Browse files Browse the repository at this point in the history
* ✨ feat: support titleRender

* 🐳 chore: update test case

* 🐳 chore: update prop name

* 🐳 chore: update `label` logic

* 🌈 style: format
  • Loading branch information
huangkairan committed Dec 4, 2023
1 parent e4d0bfa commit 5f5be87
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 6 deletions.
16 changes: 12 additions & 4 deletions examples/basic.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import '../assets/index.less';
import React from 'react';
import 'rc-dialog/assets/index.css';
import Dialog from 'rc-dialog';
import TreeSelect, { TreeNode, SHOW_PARENT } from '../src';
import 'rc-dialog/assets/index.css';
import React from 'react';
import '../assets/index.less';
import TreeSelect, { SHOW_PARENT, TreeNode } from '../src';
import { gData } from './utils/dataUtil';

function isLeaf(value) {
Expand Down Expand Up @@ -381,6 +381,14 @@ class Demo extends React.Component {
</TreeNode>
<TreeNode value="same value3" title="same title" key="0-3" />
</TreeSelect>

<h2>title render</h2>
<TreeSelect<{ label: string }>
open
style={{ width: 300 }}
treeData={gData}
treeTitleRender={node => node.label + 'ok'}
/>
</div>
);
}
Expand Down
2 changes: 2 additions & 0 deletions src/OptionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const OptionList: React.RefForwardingComponent<ReviseRefOptionListProps> = (_, r
onSelect,
dropdownMatchSelectWidth,
treeExpandAction,
treeTitleRender,
} = React.useContext(TreeSelectContext);

const {
Expand Down Expand Up @@ -241,6 +242,7 @@ const OptionList: React.RefForwardingComponent<ReviseRefOptionListProps> = (_, r
checkedKeys={mergedCheckedKeys}
selectedKeys={!checkable ? checkedKeys : []}
defaultExpandAll={treeDefaultExpandAll}
titleRender={treeTitleRender}
{...treeProps}
// Proxy event out
onActiveChange={setActiveKey}
Expand Down
13 changes: 12 additions & 1 deletion src/TreeSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ export interface TreeSelectProps<
listItemHeight?: number;
listItemScrollOffset?: number;
onDropdownVisibleChange?: (open: boolean) => void;
treeTitleRender?: (node: ValueType) => React.ReactNode;

// >>> Tree
treeLine?: boolean;
Expand Down Expand Up @@ -237,6 +238,7 @@ const TreeSelect = React.forwardRef<BaseSelectRef, TreeSelectProps>((props, ref)
showTreeIcon,
switcherIcon,
treeMotion,
treeTitleRender,

...restProps
} = props;
Expand Down Expand Up @@ -438,9 +440,16 @@ const TreeSelect = React.forwardRef<BaseSelectRef, TreeSelectProps>((props, ref)
// Back fill with origin label
const labeledValues = values.map(val => {
const targetItem = rawLabeledValues.find(item => item.value === val);
let label;
// Ensure that when labelInValue is true, if label is undefined, it remains undefined.
if (labelInValue && targetItem.label !== undefined) {
label = targetItem.label;
} else if (!labelInValue && treeTitleRender) {
label = treeTitleRender(targetItem);
}
return {
value: val,
label: targetItem?.label,
label,
};
});

Expand Down Expand Up @@ -671,6 +680,7 @@ const TreeSelect = React.forwardRef<BaseSelectRef, TreeSelectProps>((props, ref)
fieldNames: mergedFieldNames,
onSelect: onOptionSelect,
treeExpandAction,
treeTitleRender,
}),
[
virtual,
Expand All @@ -682,6 +692,7 @@ const TreeSelect = React.forwardRef<BaseSelectRef, TreeSelectProps>((props, ref)
mergedFieldNames,
onOptionSelect,
treeExpandAction,
treeTitleRender,
],
);

Expand Down
1 change: 1 addition & 0 deletions src/TreeSelectContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface TreeSelectContextProps {
fieldNames: InternalFieldName;
onSelect: OnInternalSelect;
treeExpandAction?: ExpandAction;
treeTitleRender?: (node: any) => React.ReactNode;
}

const TreeSelectContext = React.createContext<TreeSelectContextProps>(null as any);
Expand Down
22 changes: 21 additions & 1 deletion tests/Select.props.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-undef, react/no-multi-comp, no-console */
import React from 'react';
import { mount } from 'enzyme';
import Tree, { TreeNode } from 'rc-tree';
import React from 'react';
import TreeSelect, { SHOW_ALL, SHOW_CHILD, SHOW_PARENT, TreeNode as SelectNode } from '../src';

// Promisify timeout to let jest catch works
Expand Down Expand Up @@ -612,5 +612,25 @@ describe('TreeSelect.props', () => {
});
});
});

describe('title render', () => {
const treeData = [
{ label: 'Label 0-0', value: 'Value 0-0', key: 'key 0-0' },
{ label: 'Label 0-1', value: 'Value 0-1', key: 'key 0-1' },
{ label: 'Label 1-0', value: 'Value 1-0', key: 'key 1-0' },
];
it('basic', () => {
const wrapper = mount(
<div>
<TreeSelect
defaultValue={'Value 0-0'}
treeTitleRender={node => node.value}
treeData={treeData}
/>
</div>,
);
expect(wrapper.getSelection(0).text()).toBe('Value 0-0');
});
});
});
});

0 comments on commit 5f5be87

Please sign in to comment.