(null)
function doClick (e: MouseEvent): void {
const { onClick } = props
@@ -31,15 +36,24 @@ export default defineComponent({
}
return {
selfRef,
+ renderLabel: renderLabelRef,
+ renderPrefix: renderPrefixRef,
+ renderSuffix: renderSuffixRef,
handleClick
}
},
render () {
const {
clsPrefix,
+ checked = false,
+ selected = false,
+ renderLabel,
+ renderPrefix,
+ renderSuffix,
handleClick,
onDragstart,
tmNode: {
+ rawNode,
rawNode: { prefix, label, suffix }
}
} = this
@@ -51,17 +65,35 @@ export default defineComponent({
draggable={onDragstart === undefined ? undefined : true}
onDragstart={onDragstart}
>
- {prefix ? (
+ {renderPrefix || prefix ? (
- {render(prefix)}
+ {renderPrefix
+ ? renderPrefix({
+ option: rawNode,
+ selected,
+ checked
+ })
+ : render(prefix)}
) : null}
- {render(label)}
+ {renderLabel
+ ? renderLabel({
+ option: rawNode,
+ selected,
+ checked
+ })
+ : render(label)}
- {suffix ? (
+ {renderSuffix || suffix ? (
- {render(suffix)}
+ {renderSuffix
+ ? renderSuffix({
+ option: rawNode,
+ selected,
+ checked
+ })
+ : render(suffix)}
) : null}
diff --git a/src/tree/src/interface.ts b/src/tree/src/interface.ts
index 8de1e286d..4e27303f6 100644
--- a/src/tree/src/interface.ts
+++ b/src/tree/src/interface.ts
@@ -20,6 +20,24 @@ export type TreeOption = TreeOptionBase & { [k: string]: unknown }
export type TreeOptions = TreeOption[]
+export interface TreeRenderProps {
+ option: TreeOption
+ checked: boolean
+ selected: boolean
+}
+
+type RenderTreePart = ({
+ option,
+ checked,
+ selected
+}: TreeRenderProps) => VNodeChild
+
+export type RenderLabel = RenderTreePart
+
+export type RenderPrefix = RenderTreePart
+
+export type RenderSuffix = RenderTreePart
+
export interface TreeDragInfo {
event: DragEvent
node: TreeOption
@@ -78,6 +96,9 @@ export interface TreeInjection {
pendingNodeKeyRef: Ref
internalScrollableRef: Ref
internalCheckboxFocusableRef: Ref
+ renderLabelRef: Ref
+ renderPrefixRef: Ref
+ renderSuffixRef: Ref
handleSwitcherClick: (node: TreeNode) => void
handleSelect: (node: TreeNode) => void
handleCheck: (node: TreeNode, checked: boolean) => void
diff --git a/src/tree/tests/Tree.spec.ts b/src/tree/tests/Tree.spec.ts
index 372a80fee..c986774d1 100644
--- a/src/tree/tests/Tree.spec.ts
+++ b/src/tree/tests/Tree.spec.ts
@@ -63,4 +63,33 @@ describe('n-tree', () => {
expect(wrapper.find('.n-tree-node-content__suffix').exists()).toBe(true)
expect(wrapper.find('.n-tree-node-content__suffix').text()).toBe('suffix')
})
+
+ it('should work with `render-label`, `render-prefix` and `render-suffix`', async () => {
+ const wrapper = mount(NTree, {
+ props: {
+ data: [
+ {
+ label: 'test',
+ key: '123',
+ children: [
+ {
+ label: '123',
+ key: '123'
+ }
+ ]
+ }
+ ],
+ renderPrefix: () => 'prefix',
+ renderLabel: () => 'label',
+ renderSuffix: () => 'suffix'
+ }
+ })
+
+ expect(wrapper.find('.n-tree-node-content__prefix').exists()).toBe(true)
+ expect(wrapper.find('.n-tree-node-content__prefix').text()).toBe('prefix')
+ expect(wrapper.find('.n-tree-node-content__text').exists()).toBe(true)
+ expect(wrapper.find('.n-tree-node-content__text').text()).toBe('label')
+ expect(wrapper.find('.n-tree-node-content__suffix').exists()).toBe(true)
+ expect(wrapper.find('.n-tree-node-content__suffix').text()).toBe('suffix')
+ })
})