Skip to content

Commit

Permalink
✨ feat: support titleRender
Browse files Browse the repository at this point in the history
  • Loading branch information
huangkairan committed Nov 15, 2023
1 parent e85d617 commit ea02759
Show file tree
Hide file tree
Showing 5 changed files with 43 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}
titleRender={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,
titleRender,
} = 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={titleRender}
{...treeProps}
// Proxy event out
onActiveChange={setActiveKey}
Expand Down
7 changes: 6 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;
titleRender?: (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,
titleRender,

...restProps
} = props;
Expand Down Expand Up @@ -438,9 +440,10 @@ 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);
const label = titleRender ? titleRender(targetItem) : targetItem?.label;
return {
value: val,
label: targetItem?.label,
label,
};
});

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

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;
titleRender?: (node: any) => React.ReactNode;
}

const TreeSelectContext = React.createContext<TreeSelectContextProps>(null as any);
Expand Down
23 changes: 22 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,26 @@ 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'}
titleRender={node => node.value}
treeData={treeData}
/>
</div>,
);
// expect(wrapper.find('.rc-tree-select-tree-title').text()).toEqual('Value 0-0');
expect(wrapper.getSelection(0).text()).toBe('Value 0-0');
});
});
});
});

0 comments on commit ea02759

Please sign in to comment.