diff --git a/docs/components/CustomArrowIcon.vue b/docs/components/CustomArrowIcon.vue new file mode 100644 index 00000000..ec8e7302 --- /dev/null +++ b/docs/components/CustomArrowIcon.vue @@ -0,0 +1,66 @@ + + + diff --git a/docs/partials/guides.pug b/docs/partials/guides.pug index 106f2051..b6b763b4 100644 --- a/docs/partials/guides.pug +++ b/docs/partials/guides.pug @@ -41,6 +41,7 @@ This demonstrates more features. +demo('MoreFeatures') + +subsection('Delayed Loading') :markdown-it If you have a large number of deeply nested options, you might want to load options only of the most top levels on initial load, and load the rest only when needed. You can achieve that by following these steps: @@ -130,6 +131,14 @@ - `node` - a normalized node object (note that, this is differnt from what you return from `normalizer()` prop) +demo('CustomizeValueLabel') + ++subsection('Custom Arrow Icon') + :markdown-it + You can customize the arrow icon. vue-treeselect utilizes Vue's [scoped slot](https://vuejs.org/v2/guide/components.html#Scoped-Slots) feature and provides some props you should use in your customized template: + + - `isOpen` - true if the menu is open, false otherwise + +demo('CustomArrowIcon') + +subsection('Vuex Support') :markdown-it In all previous examples, we used `v-model` to sync value between application state and vue-treeselect, a.k.a two-way data binding. If you are using Vuex, we could make use of `:value` and `@input`, since `v-model` is just a syntax sugar for them in Vue 2. diff --git a/src/components/Control.vue b/src/components/Control.vue index efc67ddc..5227fde7 100644 --- a/src/components/Control.vue +++ b/src/components/Control.vue @@ -57,12 +57,18 @@ methods: { renderX() { const { instance } = this - const title = instance.multiple ? instance.clearAllText : instance.clearValueText + const title = instance.multiple + ? instance.clearAllText + : instance.clearValueText if (!this.shouldShowX) return null return ( -
+
) @@ -77,9 +83,17 @@ if (!this.shouldShowArrow) return null + const arrowIconRenderer = instance.$scopedSlots['arrow-icon'] return ( -
- +
+ {arrowIconRenderer ? ( + arrowIconRenderer({ isOpen: instance.menu.isOpen }) + ) : ( + + )}
) }, @@ -129,11 +143,7 @@ // This is meant to be called by child `` component. renderValueContainer(children) { - return ( -
- {children} -
- ) + return
{children}
}, }, @@ -142,7 +152,10 @@ const ValueContainer = instance.single ? SingleValue : MultiValue return ( -
+
{this.renderX()} {this.renderArrow()} diff --git a/test/unit/specs/Control.spec.js b/test/unit/specs/Control.spec.js index 14825585..f324b638 100644 --- a/test/unit/specs/Control.spec.js +++ b/test/unit/specs/Control.spec.js @@ -18,4 +18,35 @@ describe('Control', () => { leftClick(arrow) expect(wrapper.vm.menu.isOpen).toBe(false) }) + + it('should render custom slot if provided', () => { + const openLabel = 'O' + const closeLabel = 'C' + const template = ` + + + + ` + + const wrapper = mount({ + template, + components: { Treeselect }, + data() { + return { + options: [], + value: '', + } + }, + }) + + const treeselect = wrapper.vm.$refs.treeselect + expect(treeselect.menu.isOpen).toBe(false) + const arrowLabel = wrapper.find('.vue-treeselect__control-arrow-container') + expect(arrowLabel.text()).toBe(closeLabel) + leftClick(arrowLabel) + expect(treeselect.menu.isOpen).toBe(true) + expect(arrowLabel.text()).toBe(openLabel) + }) })