Skip to content

Commit

Permalink
feat(ktreelist): add prop to filter draggable element selectors (#2610)
Browse files Browse the repository at this point in the history
* feat(ktreelist): add filter prop

Adds a new prop to pass in a string (or function that evaluates to a string) for
disabling drag behavior from elements in the DOM.

* chore: update sandbox example

* fix: array logic

* fix: use computed prop
  • Loading branch information
adamdehaven authored Feb 10, 2025
1 parent debf4c3 commit 23a27ca
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
10 changes: 10 additions & 0 deletions docs/components/tree-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,16 @@ Boolean to turn off drag-n-drop reordering of the list. Defaults to `false`.
<KTreeList disable-drag :items="items" />
```

### ignoreDragSelectors

A `string` or (`function` that returns a `string`) of selectors that should not result in dragging tree list items.

For multiple selectors, you **must** pass in a comma-delimited list of selectors.

```html
<KTreeList ignore-drag-selectors=".button-element, .another-element > .nested-property" :items="items" />
```

### maxDepth

Use this prop to customize the maximum supported depth of the tree. The default value is `3`. The maximum supported value for `maxDepth` is 5.
Expand Down
17 changes: 17 additions & 0 deletions sandbox/pages/SandboxTreeList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@
:items="items4"
/>
</SandboxSectionComponent>
<SandboxSectionComponent title="ignoreDragSelectors">
<KTreeList
ignore-drag-selectors=".internal-button"
:items="items12"
>
<template #item-label="{ item }">
<KButton
appearance="secondary"
class="internal-button"
size="small"
>
{{ item.name }}
</KButton>
</template>
</KTreeList>
</SandboxSectionComponent>
<SandboxSectionComponent title="maxDepth">
<KTreeList
:items="items5"
Expand Down Expand Up @@ -198,6 +214,7 @@ const items8 = ref<TreeListItem[]>(JSON.parse(JSON.stringify(defaultItems)))
const items9 = ref<TreeListItem[]>(JSON.parse(JSON.stringify(defaultItems)))
const items10 = ref<TreeListItem[]>(JSON.parse(JSON.stringify(defaultItems)))
const items11 = ref<TreeListItem[]>(JSON.parse(JSON.stringify(defaultItems)))
const items12 = ref<TreeListItem[]>(JSON.parse(JSON.stringify(defaultItems)))
const toggleItems = ref<boolean>(true)
const kTreeRef = useTemplateRef('k-tree-ref')
Expand Down
13 changes: 12 additions & 1 deletion src/components/KTreeList/KTreeDraggable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
class="tree-draggable"
direction="vertical"
:disabled="disableDrag"
filter=".tree-item-expanded-button"
:filter="computedDragFilter"
:group="{ name: group, pull: [group], put: maxLevelReached ? [] : [group] }"
:level="level"
:list="internalList"
Expand Down Expand Up @@ -62,6 +62,7 @@
}"
:collapsible="collapsible"
:disable-drag="disableDrag"
:filter="computedDragFilter"
:group="group"
:hide-icons="hideIcons"
:initial-collapse-all="initialCollapseAll"
Expand Down Expand Up @@ -121,6 +122,10 @@ const props = defineProps({
type: Boolean,
default: false,
},
filter: {
type: [String, Function] as PropType<string | (() => string)>,
default: () => '',
},
maxDepth: {
type: Number,
default: 3,
Expand Down Expand Up @@ -176,6 +181,12 @@ const draggableAttrs = {
}
const dragging = ref<boolean>(false)
const computedDragFilter = computed((): string => {
const selectorList: string = typeof props.filter === 'function' ? props.filter() : props.filter
// Always append the internal `.tree-item-expanded-button` selector and separate selectors with a comma
return ['.tree-item-expanded-button', ...(selectorList.split(',').map(s => s.trim()).filter(Boolean))].join(', ')
})
const hasNoChildren = (item: TreeListItem): boolean => {
return !internalList.value.filter(anItem => anItem.id === item.id)?.[0].children?.length
}
Expand Down
6 changes: 6 additions & 0 deletions src/components/KTreeList/KTreeList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
:collapsible="collapsible"
:data-testid="`k-tree-list-${group}`"
:disable-drag="disableDrag"
:filter="ignoreDragSelectors"
:group="group"
:hide-icons="hideIcons"
:initial-collapse-all="initialCollapseAll"
Expand Down Expand Up @@ -97,6 +98,11 @@ const props = defineProps({
type: Boolean,
default: false,
},
/** A `string` or `function` that returns a `string` of selectors that should not result in dragging tree list items. */
ignoreDragSelectors: {
type: [String, Function] as PropType<string | (() => string)>,
default: () => '',
},
maxDepth: {
type: Number,
default: 3,
Expand Down

0 comments on commit 23a27ca

Please sign in to comment.