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

+[LabelOnlySelectSelf] #394

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions docs/components/LabelOnlySelectSelf.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<template>
<div>
<treeselect
:multiple="true"
:flat="true"
:labelOnlySelectSelf="true"
:autoSelectDescendants="true"
:autoDeselectDescendants="true"
:options="options"
:alwaysOpen="true"
placeholder="Select your favourite(s)..."
v-model="value"
/>
<treeselect-value :value="value" />
</div>
</template>

<script>
export default {
data: () => ({
value: [],
options: [ {
id: 'fruits',
label: 'Fruits',
isExpanded: true,
children: [ {
id: 'apple',
label: 'Apple 🍎',
isNew: true,
}, {
id: 'grapes',
label: 'Grapes 🍇',
}, {
id: 'pear',
label: 'Pear 🍐',
}, {
id: 'strawberry',
label: 'Strawberry 🍓',
}, {
id: 'watermelon',
label: 'Watermelon 🍉',
} ],
}, {
id: 'vegetables',
label: 'Vegetables',
children: [ {
id: 'corn',
label: 'Corn 🌽',
}, {
id: 'carrot',
label: 'Carrot 🥕',
}, {
id: 'eggplant',
label: 'Eggplant 🍆',
}, {
id: 'tomato',
label: 'Tomato 🍅',
} ],
} ],
}),
}
</script>
5 changes: 5 additions & 0 deletions docs/partials/guides.pug
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@
- `"INDEX"` - Index of option: AAA 🡒 BB 🡒 C
+demo('FlatModeAndSortValues')

+subsection('Label Only Select Self')
:markdown-it
For some cases, you will need to keep the choice of (related/unrelated) when click on node.
+demo('LabelOnlySelectSelf')

+subsection('Prevent Value Combining')
:markdown-it
For non-flat & multi-select mode, if a branch node and its all descendants are checked, vue-treeselect will combine them into a single item in the value array, as demonstrated in the following example. By using `valueConsistsOf` prop you can change that behavior. This prop has four options:
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@riophae/vue-treeselect",
"version": "0.4.0",
"name": "@hanye9895/vue-treeselect",
"version": "0.5.4",
"description": "A multi-select component with nested options support for Vue.js",
"author": "Riophae Lee <riophaelee@gmail.com>",
"license": "MIT",
Expand Down
17 changes: 14 additions & 3 deletions src/components/Option.vue
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@

renderLabelContainer(children) {
return (
<div class="vue-treeselect__label-container" onMousedown={this.handleMouseDownOnLabelContainer}>
<div class="vue-treeselect__label-container">
{children}
</div>
)
Expand All @@ -124,7 +124,7 @@
if (instance.disableBranchNodes && node.isBranch) return null

return (
<div class="vue-treeselect__checkbox-container">
<div class="vue-treeselect__checkbox-container" onMousedown={this.handleMouseDownOnCheckboxContainer}>
{children}
</div>
)
Expand Down Expand Up @@ -182,7 +182,7 @@
})

return (
<label class={labelClassName}>
<label class={labelClassName} onMousedown={this.handleMouseDownOnLabelContainer}>
{node.label}
{shouldShowCount && (
<span class={countClassName}>({count})</span>
Expand Down Expand Up @@ -258,10 +258,21 @@
if (node.isBranch && instance.disableBranchNodes) {
instance.toggleExpanded(node)
} else {
// fromLabel
instance.select(node)
}
}),

handleMouseDownOnCheckboxContainer: onLeftClick(function handleMouseDownOnCheckboxContainer() {
const { instance, node } = this

if (node.isBranch && instance.disableBranchNodes) {
instance.toggleExpanded(node)
} else {
instance.select(node, true)
}
}),

handleMouseDownOnRetry: onLeftClick(function handleMouseDownOnRetry() {
const { instance, node } = this

Expand Down
24 changes: 18 additions & 6 deletions src/mixins/treeselectMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,14 @@ export default {
default: false,
},

/**
* Set `true` to allow only select self when click on label.
*/
labelOnlySelectSelf: {
type: Boolean,
default: false,
},

/**
* Generates a hidden <input /> tag with this field name for html forms.
*/
Expand Down Expand Up @@ -1563,7 +1571,7 @@ export default {
})
this.$set(normalized, 'isExpanded', typeof isDefaultExpanded === 'boolean'
? isDefaultExpanded
: level < this.defaultExpandLevel)
: (level < this.defaultExpandLevel || node.isExpanded))
this.$set(normalized, 'hasMatchedDescendants', false)
this.$set(normalized, 'hasDisabledDescendants', false)
this.$set(normalized, 'isExpandedOnSearch', false)
Expand Down Expand Up @@ -1749,7 +1757,7 @@ export default {
)
},

select(node) {
select(node, fromLabel) {
if (this.disabled || node.isDisabled) {
return
}
Expand All @@ -1763,9 +1771,9 @@ export default {
: !this.isSelected(node)

if (nextState) {
this._selectNode(node)
this._selectNode(node, fromLabel && this.labelOnlySelectSelf)
} else {
this._deselectNode(node)
this._deselectNode(node, fromLabel && this.labelOnlySelectSelf)
}

this.buildForestState()
Expand Down Expand Up @@ -1805,14 +1813,16 @@ export default {
},

// This is meant to be called only by `select()`.
_selectNode(node) {
_selectNode(node, onlySelf) {
if (this.single || this.disableBranchNodes) {
return this.addValue(node)
}

if (this.flat) {
this.addValue(node)

if (onlySelf) return

if (this.autoSelectAncestors) {
node.ancestors.forEach(ancestor => {
if (!this.isSelected(ancestor) && !ancestor.isDisabled) this.addValue(ancestor)
Expand Down Expand Up @@ -1853,14 +1863,16 @@ export default {
},

// This is meant to be called only by `select()`.
_deselectNode(node) {
_deselectNode(node, onlySelf) {
if (this.disableBranchNodes) {
return this.removeValue(node)
}

if (this.flat) {
this.removeValue(node)

if (onlySelf) return

if (this.autoDeselectAncestors) {
node.ancestors.forEach(ancestor => {
if (this.isSelected(ancestor) && !ancestor.isDisabled) this.removeValue(ancestor)
Expand Down