diff --git a/docs/en-US/component/cascader.md b/docs/en-US/component/cascader.md index f6c53fb9db..3561c3f12f 100644 --- a/docs/en-US/component/cascader.md +++ b/docs/en-US/component/cascader.md @@ -148,6 +148,16 @@ cascader/custom-tag ::: +## Click to Select Node ^(2.10.5) + +Allows selecting nodes by clicking, with optional control visibility. + +:::demo Select nodes on click via `checkOnClickNode`. You can also hide the selection control with `showPrefix`. + +cascader/check-on-click-node + +::: + ## Cascader API ### Cascader Attributes @@ -250,20 +260,22 @@ cascader/custom-tag ## CascaderProps -| Attribute | Description | Type | Default | -| -------------- | ---------------------------------------------------------------------------------------------------------- | --------------------------------------------------- | -------- | -| expandTrigger | trigger mode of expanding options | ^[enum]`'click' \| 'hover'` | click | -| multiple | whether multiple selection is enabled | ^[boolean] | false | -| checkStrictly | whether checked state of a node not affects its parent and child nodes | ^[boolean] | false | -| emitPath | when checked nodes change, whether to emit an array of node's path, if false, only emit the value of node. | ^[boolean] | true | -| lazy | whether to dynamic load child nodes, use with `lazyload` attribute | ^[boolean] | false | -| lazyLoad | method for loading child nodes data, only works when `lazy` is true | ^[Function]`(node: Node, resolve: Resolve) => void` | — | -| value | specify which key of node object is used as the node's value | ^[string] | value | -| label | specify which key of node object is used as the node's label | ^[string] | label | -| children | specify which key of node object is used as the node's children | ^[string] | children | -| disabled | specify which key of node object is used as the node's disabled | ^[string] | disabled | -| leaf | specify which key of node object is used as the node's leaf field | ^[string] | leaf | -| hoverThreshold | hover threshold of expanding options | ^[number] | 500 | +| Attribute | Description | Type | Default | +| -------------------------- | ---------------------------------------------------------------------------------------------------------- | --------------------------------------------------- | -------- | +| expandTrigger | trigger mode of expanding options | ^[enum]`'click' \| 'hover'` | click | +| multiple | whether multiple selection is enabled | ^[boolean] | false | +| checkStrictly | whether checked state of a node not affects its parent and child nodes | ^[boolean] | false | +| emitPath | when checked nodes change, whether to emit an array of node's path, if false, only emit the value of node. | ^[boolean] | true | +| lazy | whether to dynamic load child nodes, use with `lazyload` attribute | ^[boolean] | false | +| lazyLoad | method for loading child nodes data, only works when `lazy` is true | ^[Function]`(node: Node, resolve: Resolve) => void` | — | +| value | specify which key of node object is used as the node's value | ^[string] | value | +| label | specify which key of node object is used as the node's label | ^[string] | label | +| children | specify which key of node object is used as the node's children | ^[string] | children | +| disabled | specify which key of node object is used as the node's disabled | ^[string] | disabled | +| leaf | specify which key of node object is used as the node's leaf field | ^[string] | leaf | +| hoverThreshold | hover threshold of expanding options | ^[number] | 500 | +| checkOnClickNode ^(2.10.5) | whether to check or uncheck node when clicking on the node | ^[boolean] | false | +| showPrefix ^(2.10.5) | whether to show the radio or checkbox prefix | ^[boolean] | true | ## Type Declarations diff --git a/docs/examples/cascader/check-on-click-node.vue b/docs/examples/cascader/check-on-click-node.vue new file mode 100644 index 0000000000..e8e1b84bf7 --- /dev/null +++ b/docs/examples/cascader/check-on-click-node.vue @@ -0,0 +1,309 @@ + + + diff --git a/packages/components/cascader-panel/src/config.ts b/packages/components/cascader-panel/src/config.ts index c77007553a..da147f72cb 100644 --- a/packages/components/cascader-panel/src/config.ts +++ b/packages/components/cascader-panel/src/config.ts @@ -84,6 +84,14 @@ export const DefaultProps: CascaderConfig = { * @description hover threshold of expanding options */ hoverThreshold: 500, + /** + * @description whether to check or uncheck node when clicking on the node + */ + checkOnClickNode: false, + /** + * @description whether to show the radio or checkbox prefix + */ + showPrefix: true, } export const cascaderPanelProps = buildProps({ diff --git a/packages/components/cascader-panel/src/node-content.tsx b/packages/components/cascader-panel/src/node-content.tsx index 6acb72e130..3ef459bbc9 100644 --- a/packages/components/cascader-panel/src/node-content.tsx +++ b/packages/components/cascader-panel/src/node-content.tsx @@ -19,17 +19,26 @@ export default defineComponent({ required: true, }, renderLabelFn: Function as PropType, + checkOnClickNode: Boolean, + disabled: Boolean, }, - setup(props) { + setup(props, { emit }) { const ns = useNamespace('cascader-node') - const { renderLabelFn, node } = props + const { renderLabelFn, node, disabled, checkOnClickNode } = props const { data, label: nodeLabel } = node const label = () => { const renderLabel = renderLabelFn?.({ node, data }) return isVNodeEmpty(renderLabel) ? nodeLabel : renderLabel ?? nodeLabel } - - return () => {label()} + function handleClick() { + if (disabled || !checkOnClickNode) return + emit('handleSelectCheck', !node.checked) + } + return () => ( + + {label()} + + ) }, }) diff --git a/packages/components/cascader-panel/src/node.ts b/packages/components/cascader-panel/src/node.ts index e11e43b967..eb59ba3ae0 100644 --- a/packages/components/cascader-panel/src/node.ts +++ b/packages/components/cascader-panel/src/node.ts @@ -40,6 +40,8 @@ export interface CascaderProps { disabled?: string | isDisabled leaf?: string | isLeaf hoverThreshold?: number + checkOnClickNode?: boolean + showPrefix?: boolean } export type Nullable = null | T diff --git a/packages/components/cascader-panel/src/node.vue b/packages/components/cascader-panel/src/node.vue index 015e56f0a3..7293e38f34 100644 --- a/packages/components/cascader-panel/src/node.vue +++ b/packages/components/cascader-panel/src/node.vue @@ -20,7 +20,7 @@ > - - + +