treeselectjs 0.2.2 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -120,8 +120,10 @@ Name | Params | Discription
120
120
  ------------- | ------------- | -------------
121
121
  updateValue | Array[String] | Update selected values.
122
122
  mount | None | Helps to remount and update settings.
123
+ destroy | None | Deletes elements from the DOM and clears all the data. Call mount() to recreate.
123
124
 
124
125
  ### Notes
125
- 1) If you want to change the padding of the element you can use CSS selector. I've added 'group' and 'level' attributes, but you have to use !important.
126
- 2) If you want to update props, set props to the entity of the class and then call mount() method.
127
- 3) Use updateValue method to update only the value.
126
+ 1) If you want to change the padding of the element you can use CSS selector. I've added **'group'** and **'level'** attributes, but you have to use **!important**.
127
+ 2) If you want to update props, set props to the entity of the class and then call **mount()** method.
128
+ 3) Use **updateValue()** method to update only the value.
129
+ 4) If you need to delete List from the DOM when you don't need treeselect anymore - call **destroy()**.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "treeselectjs",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "description": "Treeselect JS",
5
5
  "main": "dist/treeselect-js.js",
6
6
  "repository": {
package/src/list.js CHANGED
@@ -142,10 +142,11 @@ const updateDOM = (flatOptions, srcElement) => {
142
142
  const updateLeftPaddingItems = (option, listItem, flatOptions) => {
143
143
  const isZeroLevel = option.level === 0
144
144
  const defaultPadding = 20
145
+ const zeroLevelItemPadding = 5
145
146
 
146
147
  if (isZeroLevel) {
147
148
  const isGroupsExistOnLevel = flatOptions.some(item => item.isGroup && item.level === option.level)
148
- const itemPadding = !option.isGroup && isGroupsExistOnLevel ? `${defaultPadding}px` : '0'
149
+ const itemPadding = !option.isGroup && isGroupsExistOnLevel ? `${defaultPadding}px` : `${zeroLevelItemPadding}px`
149
150
  listItem.style.paddingLeft = option.isGroup ? '0' : itemPadding
150
151
  } else {
151
152
  listItem.style.paddingLeft = option.isGroup
@@ -16,6 +16,7 @@
16
16
  left: 0;
17
17
  border-radius: 4px;
18
18
  box-sizing: border-box;
19
+ z-index: 1010;
19
20
  }
20
21
 
21
22
  .treeselect .treeselect-list {
@@ -13,6 +13,11 @@ class Treeselect {
13
13
  #containerResizer = null
14
14
  #containerWidth = 0
15
15
 
16
+ // Outside listeners
17
+ #scrollEvent = null
18
+ #focusEvent = null
19
+ #blurEvent = null
20
+
16
21
  constructor ({
17
22
  parentHtmlContainer,
18
23
  value,
@@ -42,15 +47,10 @@ class Treeselect {
42
47
  this.grouped = grouped ?? true
43
48
  this.listSlotHtmlComponent = listSlotHtmlComponent ?? null
44
49
  this.disabled = disabled ?? false
45
- this.emptyText = emptyText
50
+ this.emptyText = emptyText ?? 'No results found...'
46
51
 
47
52
  this.srcElement = null
48
53
 
49
- // Outside listeners
50
- this.scrollEvent = null
51
- this.focusEvent = null
52
- this.blurEvent = null
53
-
54
54
  this.mount()
55
55
  }
56
56
 
@@ -65,9 +65,9 @@ class Treeselect {
65
65
 
66
66
  this.srcElement = this.#createTreeselect()
67
67
 
68
- this.scrollEvent = this.scrollWindowHandler.bind(this)
69
- this.focusEvent = this.focusWindowHandler.bind(this)
70
- this.blurEvent = this.blurWindowHandler.bind(this)
68
+ this.#scrollEvent = this.scrollWindowHandler.bind(this)
69
+ this.#focusEvent = this.focusWindowHandler.bind(this)
70
+ this.#blurEvent = this.blurWindowHandler.bind(this)
71
71
 
72
72
  if (this.alwaysOpen) {
73
73
  this.#treeselectInput.openClose()
@@ -86,6 +86,15 @@ class Treeselect {
86
86
  this.#treeselectInput.updateValue(inputNewValue)
87
87
  }
88
88
 
89
+ destroy () {
90
+ if (this.srcElement) {
91
+ this.#closeList()
92
+ this.srcElement.innerHTML = ''
93
+ this.srcElement = null
94
+ this.#removeOutsideListeners()
95
+ }
96
+ }
97
+
89
98
  #createTreeselect () {
90
99
  const container = this.parentHtmlContainer
91
100
  container.classList.add('treeselect')
@@ -132,9 +141,9 @@ class Treeselect {
132
141
  })
133
142
  input.srcElement.addEventListener('focus', () => {
134
143
  this.#updateFocusClasses(true)
135
- document.addEventListener('mousedown', this.focusEvent, true)
136
- document.addEventListener('focus', this.focusEvent, true)
137
- window.addEventListener('blur', this.blurEvent)
144
+ document.addEventListener('mousedown', this.#focusEvent, true)
145
+ document.addEventListener('focus', this.#focusEvent, true)
146
+ window.addEventListener('blur', this.#blurEvent)
138
147
  }, true)
139
148
 
140
149
  if (!this.alwaysOpen) {
@@ -167,7 +176,7 @@ class Treeselect {
167
176
  }
168
177
 
169
178
  #openList () {
170
- window.addEventListener('scroll', this.scrollEvent, true)
179
+ window.addEventListener('scroll', this.#scrollEvent, true)
171
180
 
172
181
  if (this.appendToBody) {
173
182
  document.body.appendChild(this.#treeselectList.srcElement)
@@ -182,7 +191,14 @@ class Treeselect {
182
191
  }
183
192
 
184
193
  #closeList () {
185
- window.removeEventListener('scroll', this.scrollEvent, true)
194
+ window.removeEventListener('scroll', this.#scrollEvent, true)
195
+ const isElementExist = this.appendToBody
196
+ ? document.body.contains(this.#treeselectList.srcElement)
197
+ : this.#htmlContainer.contains(this.#treeselectList.srcElement)
198
+
199
+ if (!isElementExist) {
200
+ return
201
+ }
186
202
 
187
203
  if (this.appendToBody) {
188
204
  document.body.removeChild(this.#treeselectList.srcElement)
@@ -230,16 +246,16 @@ class Treeselect {
230
246
  }
231
247
 
232
248
  #removeOutsideListeners () {
233
- window.removeEventListener('scroll', this.scrollEvent, true)
249
+ window.removeEventListener('scroll', this.#scrollEvent, true)
234
250
 
235
- document.removeEventListener('click', this.focusEvent, true)
236
- document.removeEventListener('focus', this.focusEvent, true)
237
- window.removeEventListener('blur', this.blurEvent)
251
+ document.removeEventListener('click', this.#focusEvent, true)
252
+ document.removeEventListener('focus', this.#focusEvent, true)
253
+ window.removeEventListener('blur', this.#blurEvent)
238
254
  }
239
255
 
240
256
  // Outside Listeners
241
257
  scrollWindowHandler () {
242
- this.updateListPosition(this.#htmlContainer, this.#treeselectList.srcElement, false)
258
+ this.updateListPosition(this.#htmlContainer, this.#treeselectList.srcElement, true)
243
259
  }
244
260
 
245
261
  focusWindowHandler (e) {