|
342 | 342 | }
|
343 | 343 | },
|
344 | 344 |
|
| 345 | + /** |
| 346 | + * An optional callback function that is called each time the selected |
| 347 | + * value(s) change. When integrating with Vuex, use this callback to trigger |
| 348 | + * an action, rather than using :value.sync to retreive the selected value. |
| 349 | + * @type {Function} |
| 350 | + * @default {null} |
| 351 | + */ |
| 352 | + onChange: Function, |
| 353 | + |
| 354 | +
|
345 | 355 | /**
|
346 | 356 | * Enable/disable creating options from searchInput.
|
347 | 357 | * @type {Boolean}
|
|
368 | 378 | createOption: {
|
369 | 379 | type: Function,
|
370 | 380 | default: function (newOption) {
|
371 |
| - if (typeof this.actualOptions[0] === 'object') { |
| 381 | + if (typeof this.currentOptions[0] === 'object') { |
372 | 382 | return {[this.label]: newOption}
|
373 | 383 | }
|
374 | 384 | return newOption
|
|
389 | 399 | return {
|
390 | 400 | search: '',
|
391 | 401 | open: false,
|
392 |
| - actualValue: null, |
393 |
| - actualOptions: [], |
| 402 | + currentSelection: null, |
| 403 | + currentOptions: [], |
394 | 404 | showLoading: false
|
395 | 405 | }
|
396 | 406 | },
|
397 | 407 |
|
398 | 408 | watch: {
|
399 |
| - actualValue(val, old) { |
| 409 | + value(val, old) { |
| 410 | + this.currentSelection = val |
| 411 | + }, |
| 412 | + currentSelection(val, old) { |
400 | 413 | if (this.multiple) {
|
| 414 | + this.onChange ? this.onChange(val) : null |
401 | 415 | this.$emit('change', val)
|
402 | 416 | } else {
|
403 | 417 | if(val !== old) {
|
| 418 | + this.onChange? this.onChange(val) : null |
404 | 419 | this.$emit('change', val)
|
405 | 420 | }
|
406 | 421 | }
|
407 | 422 | },
|
408 |
| - actualOptions() { |
| 423 | + options(val) { |
| 424 | + this.currentOptions = val |
| 425 | + }, |
| 426 | + currentOptions() { |
409 | 427 | if (!this.taggable && this.resetOnOptionsChange) {
|
410 |
| - this.actualValue = this.multiple ? [] : null |
| 428 | + this.currentSelection = this.multiple ? [] : null |
411 | 429 | }
|
412 |
| - } |
| 430 | + }, |
| 431 | + multiple(val) { |
| 432 | + this.currentSelection = val ? [] : null |
| 433 | + } |
413 | 434 | },
|
414 | 435 |
|
415 | 436 | methods: {
|
|
427 | 448 | option = this.createOption(option)
|
428 | 449 |
|
429 | 450 | if (this.pushTags) {
|
430 |
| - this.actualOptions.push(option) |
| 451 | + console.log("adding " + option +" to "+ this.currentOptions) |
| 452 | + this.currentOptions.push(option) |
431 | 453 | }
|
432 | 454 | }
|
433 | 455 |
|
434 | 456 | if (this.multiple) {
|
435 |
| - if (!this.actualValue) { |
436 |
| - this.actualValue = [option] |
| 457 | + if (!this.currentSelection) { |
| 458 | + this.currentSelection = [option] |
437 | 459 | } else {
|
438 |
| - this.actualValue.push(option) |
| 460 | + this.currentSelection.push(option) |
439 | 461 | }
|
440 | 462 | } else {
|
441 |
| - this.actualValue = option |
| 463 | + this.currentSelection = option |
442 | 464 | }
|
443 | 465 | }
|
444 | 466 |
|
|
453 | 475 | deselect(option) {
|
454 | 476 | if (this.multiple) {
|
455 | 477 | let ref = -1
|
456 |
| - this.actualValue.forEach((val) => { |
| 478 | + this.currentSelection.forEach((val) => { |
457 | 479 | if (val === option || typeof val === 'object' && val[this.label] === option[this.label]) {
|
458 | 480 | ref = val
|
459 | 481 | }
|
460 | 482 | })
|
461 |
| - var index = this.actualValue.indexOf(ref) |
462 |
| - this.actualValue.splice(index, 1) |
| 483 | + var index = this.currentSelection.indexOf(ref) |
| 484 | + this.currentSelection.splice(index, 1) |
463 | 485 | } else {
|
464 |
| - this.actualValue = null |
| 486 | + this.currentSelection = null |
465 | 487 | }
|
466 | 488 | },
|
467 | 489 |
|
|
503 | 525 | * @return {Boolean} True when selected || False otherwise
|
504 | 526 | */
|
505 | 527 | isOptionSelected(option) {
|
506 |
| - if (this.multiple && this.actualValue) { |
| 528 | + if (this.multiple && this.currentSelection) { |
507 | 529 | let selected = false
|
508 |
| - this.actualValue.forEach(opt => { |
| 530 | + this.currentSelection.forEach(opt => { |
509 | 531 | if (typeof opt === 'object' && opt[this.label] === option[this.label]) {
|
510 | 532 | selected = true
|
511 | 533 | } else if (opt === option) {
|
|
515 | 537 | return selected
|
516 | 538 | }
|
517 | 539 |
|
518 |
| - return this.actualValue === option |
| 540 | + return this.currentSelection === option |
519 | 541 | },
|
520 | 542 |
|
521 | 543 | /**
|
|
537 | 559 | * @return {this.value}
|
538 | 560 | */
|
539 | 561 | maybeDeleteValue() {
|
540 |
| - if (!this.$refs.search.value.length && this.actualValue) { |
541 |
| - return this.multiple ? this.actualValue.pop() : this.actualValue = null |
| 562 | + if (!this.$refs.search.value.length && this.currentSelection) { |
| 563 | + return this.multiple ? this.currentSelection.pop() : this.currentSelection = null |
542 | 564 | }
|
543 | 565 | },
|
544 | 566 |
|
545 | 567 | /**
|
546 | 568 | * Determine if an option exists
|
547 |
| - * within this.actualOptions array. |
| 569 | + * within this.currentOptions array. |
548 | 570 | *
|
549 | 571 | * @param {Object || String} option
|
550 | 572 | * @return {boolean}
|
551 | 573 | */
|
552 | 574 | optionExists(option) {
|
553 | 575 | let exists = false
|
554 | 576 |
|
555 |
| - this.actualOptions.forEach(opt => { |
| 577 | + this.currentOptions.forEach(opt => { |
556 | 578 | if (typeof opt === 'object' && opt[this.label] === option) {
|
557 | 579 | exists = true
|
558 | 580 | } else if (opt === option) {
|
|
598 | 620 | * @return {array}
|
599 | 621 | */
|
600 | 622 | filteredOptions() {
|
601 |
| - let options = this.$options.filters.filterBy?this.$options.filters.filterBy(this.actualOptions, this.search):this.actualOptions |
| 623 | + let options = this.$options.filters.filterBy?this.$options.filters.filterBy(this.currentOptions, this.search):this.currentOptions |
602 | 624 | if (this.taggable && this.search.length && !this.optionExists(this.search)) {
|
603 | 625 | options.unshift(this.search)
|
604 | 626 | }
|
|
610 | 632 | * @return {Boolean}
|
611 | 633 | */
|
612 | 634 | isValueEmpty() {
|
613 |
| - if (this.actualValue) { |
614 |
| - if (typeof this.actualValue === 'object') { |
615 |
| - return !Object.keys(this.actualValue).length |
| 635 | + if (this.currentSelection) { |
| 636 | + if (typeof this.currentSelection === 'object') { |
| 637 | + return !Object.keys(this.currentSelection).length |
616 | 638 | }
|
617 |
| - return !this.actualValue.length |
| 639 | + return !this.currentSelection.length |
618 | 640 | }
|
619 | 641 |
|
620 | 642 | return true;
|
|
626 | 648 | */
|
627 | 649 | valueAsArray() {
|
628 | 650 | if (this.multiple) {
|
629 |
| - return this.actualValue |
630 |
| - } else if (this.actualValue) { |
631 |
| - return [this.actualValue] |
| 651 | + return this.currentSelection |
| 652 | + } else if (this.currentSelection) { |
| 653 | + return [this.currentSelection] |
632 | 654 | }
|
633 | 655 |
|
634 | 656 | return []
|
635 | 657 | }
|
636 | 658 | },
|
637 | 659 | created: function() {
|
638 |
| - this.actualValue = this.value |
639 |
| - this.actualOptions = this.options.slice(0) |
| 660 | + this.currentSelection = this.value |
| 661 | + this.currentOptions = this.options.slice(0) |
640 | 662 | this.showLoading = this.loading
|
641 | 663 | }
|
642 | 664 |
|
|
0 commit comments