|
202 | 202 | <i ref="openIndicator" role="presentation" class="open-indicator"></i>
|
203 | 203 |
|
204 | 204 | <slot name="spinner">
|
205 |
| - <div class="spinner" v-show="showLoading">Loading...</div> |
| 205 | + <div class="spinner" v-show="mutableLoading">Loading...</div> |
206 | 206 | </slot>
|
207 | 207 | </div>
|
208 | 208 |
|
|
237 | 237 | /**
|
238 | 238 | * Contains the currently selected value. Very similar to a
|
239 | 239 | * `value` attribute on an <input>. You can listen for changes
|
240 |
| - * using 'change' event using v-on |
| 240 | + * using 'change' event using v-on |
241 | 241 | * @type {Object||String||null}
|
242 | 242 | */
|
243 | 243 | value: {
|
|
349 | 349 | * @type {Function}
|
350 | 350 | * @default {null}
|
351 | 351 | */
|
352 |
| - onChange: Function, |
353 |
| - |
| 352 | + onChange: { |
| 353 | + type: Function, |
| 354 | + default: function(val) { |
| 355 | + this.$emit('input', val) |
| 356 | + } |
| 357 | + }, |
354 | 358 |
|
355 | 359 | /**
|
356 | 360 | * Enable/disable creating options from searchInput.
|
|
378 | 382 | createOption: {
|
379 | 383 | type: Function,
|
380 | 384 | default: function (newOption) {
|
381 |
| - if (typeof this.currentOptions[0] === 'object') { |
| 385 | + if (typeof this.mutableOptions[0] === 'object') { |
382 | 386 | return {[this.label]: newOption}
|
383 | 387 | }
|
384 | 388 | return newOption
|
|
399 | 403 | return {
|
400 | 404 | search: '',
|
401 | 405 | open: false,
|
402 |
| - currentSelection: null, |
403 |
| - currentOptions: [], |
404 |
| - showLoading: false |
| 406 | + mutableValue: null, |
| 407 | + mutableOptions: [], |
| 408 | + mutableLoading: false |
405 | 409 | }
|
406 | 410 | },
|
407 | 411 |
|
408 | 412 | watch: {
|
409 |
| - value(val, old) { |
410 |
| - this.currentSelection = val |
| 413 | + /** |
| 414 | + * When the value prop changes, update |
| 415 | + * the internal mutableValue. |
| 416 | + * @param {mixed} val |
| 417 | + * @return {void} |
| 418 | + */ |
| 419 | + value(val) { |
| 420 | + this.mutableValue = val |
411 | 421 | },
|
412 |
| - currentSelection(val, old) { |
| 422 | +
|
| 423 | + /** |
| 424 | + * Maybe run the onChange callback. |
| 425 | + * @param {string|object} val |
| 426 | + * @param {string|object} old |
| 427 | + * @return {void} |
| 428 | + */ |
| 429 | + mutableValue(val, old) { |
413 | 430 | if (this.multiple) {
|
414 | 431 | this.onChange ? this.onChange(val) : null
|
415 |
| - this.$emit('change', val) |
416 | 432 | } else {
|
417 |
| - if(val !== old) { |
418 |
| - this.onChange? this.onChange(val) : null |
419 |
| - this.$emit('change', val) |
420 |
| - } |
| 433 | + this.onChange && val !== old ? this.onChange(val) : null |
421 | 434 | }
|
422 | 435 | },
|
| 436 | +
|
| 437 | + /** |
| 438 | + * When options change, update |
| 439 | + * the internal mutableOptions. |
| 440 | + * @param {array} val |
| 441 | + * @return {void} |
| 442 | + */ |
423 | 443 | options(val) {
|
424 |
| - this.currentOptions = val |
| 444 | + this.mutableOptions = val |
425 | 445 | },
|
426 |
| - currentOptions() { |
| 446 | +
|
| 447 | + /** |
| 448 | + * Maybe reset the mutableValue |
| 449 | + * when mutableOptions change. |
| 450 | + * @return {[type]} [description] |
| 451 | + */ |
| 452 | + mutableOptions() { |
427 | 453 | if (!this.taggable && this.resetOnOptionsChange) {
|
428 |
| - this.currentSelection = this.multiple ? [] : null |
| 454 | + this.mutableValue = this.multiple ? [] : null |
429 | 455 | }
|
430 | 456 | },
|
431 |
| - multiple(val) { |
432 |
| - this.currentSelection = val ? [] : null |
| 457 | +
|
| 458 | + /** |
| 459 | + * Always reset the mutableValue when |
| 460 | + * the multiple prop changes. |
| 461 | + * @param {Boolean} val |
| 462 | + * @return {void} |
| 463 | + */ |
| 464 | + multiple(val) { |
| 465 | + this.mutableValue = val ? [] : null |
433 | 466 | }
|
434 | 467 | },
|
435 | 468 |
|
| 469 | + created() { |
| 470 | + this.mutableValue = this.value |
| 471 | + this.mutableOptions = this.options.slice(0) |
| 472 | + this.mutableLoading = this.loading |
| 473 | + }, |
| 474 | +
|
436 | 475 | methods: {
|
437 | 476 |
|
438 | 477 | /**
|
439 | 478 | * Select a given option.
|
440 |
| - * @param {Object||String} option |
| 479 | + * @param {Object|String} option |
441 | 480 | * @return {void}
|
442 | 481 | */
|
443 | 482 | select(option) {
|
|
448 | 487 | option = this.createOption(option)
|
449 | 488 |
|
450 | 489 | if (this.pushTags) {
|
451 |
| - console.log("adding " + option +" to "+ this.currentOptions) |
452 |
| - this.currentOptions.push(option) |
| 490 | + this.mutableOptions.push(option) |
453 | 491 | }
|
454 | 492 | }
|
455 | 493 |
|
456 | 494 | if (this.multiple) {
|
457 |
| - if (!this.currentSelection) { |
458 |
| - this.currentSelection = [option] |
| 495 | + if (!this.mutableValue) { |
| 496 | + this.mutableValue = [option] |
459 | 497 | } else {
|
460 |
| - this.currentSelection.push(option) |
| 498 | + this.mutableValue.push(option) |
461 | 499 | }
|
462 | 500 | } else {
|
463 |
| - this.currentSelection = option |
| 501 | + this.mutableValue = option |
464 | 502 | }
|
465 | 503 | }
|
466 | 504 |
|
|
469 | 507 |
|
470 | 508 | /**
|
471 | 509 | * De-select a given option.
|
472 |
| - * @param {Object||String} option |
| 510 | + * @param {Object|String} option |
473 | 511 | * @return {void}
|
474 | 512 | */
|
475 | 513 | deselect(option) {
|
476 | 514 | if (this.multiple) {
|
477 | 515 | let ref = -1
|
478 |
| - this.currentSelection.forEach((val) => { |
| 516 | + this.mutableValue.forEach((val) => { |
479 | 517 | if (val === option || typeof val === 'object' && val[this.label] === option[this.label]) {
|
480 | 518 | ref = val
|
481 | 519 | }
|
482 | 520 | })
|
483 |
| - var index = this.currentSelection.indexOf(ref) |
484 |
| - this.currentSelection.splice(index, 1) |
| 521 | + var index = this.mutableValue.indexOf(ref) |
| 522 | + this.mutableValue.splice(index, 1) |
485 | 523 | } else {
|
486 |
| - this.currentSelection = null |
| 524 | + this.mutableValue = null |
487 | 525 | }
|
488 | 526 | },
|
489 | 527 |
|
490 | 528 | /**
|
491 | 529 | * Called from this.select after each selection.
|
492 |
| - * @param {Object||String} option |
| 530 | + * @param {Object|String} option |
493 | 531 | * @return {void}
|
494 | 532 | */
|
495 | 533 | onAfterSelect(option) {
|
|
521 | 559 |
|
522 | 560 | /**
|
523 | 561 | * Check if the given option is currently selected.
|
524 |
| - * @param {Object||String} option |
525 |
| - * @return {Boolean} True when selected || False otherwise |
| 562 | + * @param {Object|String} option |
| 563 | + * @return {Boolean} True when selected | False otherwise |
526 | 564 | */
|
527 | 565 | isOptionSelected(option) {
|
528 |
| - if (this.multiple && this.currentSelection) { |
| 566 | + if (this.multiple && this.mutableValue) { |
529 | 567 | let selected = false
|
530 |
| - this.currentSelection.forEach(opt => { |
| 568 | + this.mutableValue.forEach(opt => { |
531 | 569 | if (typeof opt === 'object' && opt[this.label] === option[this.label]) {
|
532 | 570 | selected = true
|
533 | 571 | } else if (opt === option) {
|
|
537 | 575 | return selected
|
538 | 576 | }
|
539 | 577 |
|
540 |
| - return this.currentSelection === option |
| 578 | + return this.mutableValue === option |
541 | 579 | },
|
542 | 580 |
|
543 | 581 | /**
|
|
559 | 597 | * @return {this.value}
|
560 | 598 | */
|
561 | 599 | maybeDeleteValue() {
|
562 |
| - if (!this.$refs.search.value.length && this.currentSelection) { |
563 |
| - return this.multiple ? this.currentSelection.pop() : this.currentSelection = null |
| 600 | + if (!this.$refs.search.value.length && this.mutableValue) { |
| 601 | + return this.multiple ? this.mutableValue.pop() : this.mutableValue = null |
564 | 602 | }
|
565 | 603 | },
|
566 | 604 |
|
567 | 605 | /**
|
568 | 606 | * Determine if an option exists
|
569 |
| - * within this.currentOptions array. |
| 607 | + * within this.mutableOptions array. |
570 | 608 | *
|
571 | 609 | * @param {Object || String} option
|
572 | 610 | * @return {boolean}
|
573 | 611 | */
|
574 | 612 | optionExists(option) {
|
575 | 613 | let exists = false
|
576 | 614 |
|
577 |
| - this.currentOptions.forEach(opt => { |
| 615 | + this.mutableOptions.forEach(opt => { |
578 | 616 | if (typeof opt === 'object' && opt[this.label] === option) {
|
579 | 617 | exists = true
|
580 | 618 | } else if (opt === option) {
|
|
596 | 634 | return {
|
597 | 635 | open: this.open,
|
598 | 636 | searchable: this.searchable,
|
599 |
| - loading: this.showLoading |
| 637 | + loading: this.mutableLoading |
600 | 638 | }
|
601 | 639 | },
|
602 | 640 |
|
|
620 | 658 | * @return {array}
|
621 | 659 | */
|
622 | 660 | filteredOptions() {
|
623 |
| - let options = this.$options.filters.filterBy?this.$options.filters.filterBy(this.currentOptions, this.search):this.currentOptions |
| 661 | +
|
| 662 | + let options = this.mutableOptions.filter((option) => { |
| 663 | + if( typeof option === 'object' ) { |
| 664 | + return option[this.label].indexOf(this.search) > -1 |
| 665 | + } |
| 666 | + return option.indexOf(this.search) > -1 |
| 667 | + }) |
624 | 668 | if (this.taggable && this.search.length && !this.optionExists(this.search)) {
|
625 | 669 | options.unshift(this.search)
|
626 | 670 | }
|
|
632 | 676 | * @return {Boolean}
|
633 | 677 | */
|
634 | 678 | isValueEmpty() {
|
635 |
| - if (this.currentSelection) { |
636 |
| - if (typeof this.currentSelection === 'object') { |
637 |
| - return !Object.keys(this.currentSelection).length |
| 679 | + if (this.mutableValue) { |
| 680 | + if (typeof this.mutableValue === 'object') { |
| 681 | + return !Object.keys(this.mutableValue).length |
638 | 682 | }
|
639 |
| - return !this.currentSelection.length |
| 683 | + return !this.mutableValue.length |
640 | 684 | }
|
641 | 685 |
|
642 | 686 | return true;
|
|
648 | 692 | */
|
649 | 693 | valueAsArray() {
|
650 | 694 | if (this.multiple) {
|
651 |
| - return this.currentSelection |
652 |
| - } else if (this.currentSelection) { |
653 |
| - return [this.currentSelection] |
| 695 | + return this.mutableValue |
| 696 | + } else if (this.mutableValue) { |
| 697 | + return [this.mutableValue] |
654 | 698 | }
|
655 | 699 |
|
656 | 700 | return []
|
657 | 701 | }
|
658 | 702 | },
|
659 |
| - created: function() { |
660 |
| - this.currentSelection = this.value |
661 |
| - this.currentOptions = this.options.slice(0) |
662 |
| - this.showLoading = this.loading |
663 |
| - } |
664 | 703 |
|
665 | 704 | }
|
666 | 705 | </script>
|
0 commit comments