@@ -49,6 +49,7 @@ function Multiselect(element) {
4949 let _placeholder ;
5050 let _model ;
5151 let _options ;
52+ let _config ; // Configuration object
5253
5354 // Markup elems, convert this to templating engine in the future.
5455 let _containerDom ;
@@ -285,10 +286,13 @@ function Multiselect(element) {
285286 const dataOptionSel = '[data-option="' + option . value + '"]' ;
286287 const _selectionsItemDom = _selectionsDom . querySelector ( dataOptionSel ) ;
287288
288- if ( typeof _selectionsItemDom !== 'undefined' ) {
289- _selectionsDom . removeChild ( _selectionsItemDom ) ;
289+ // If the <Tag> exists
290+ if ( typeof _selectionsItemDom !== 'undefined' && _selectionsItemDom ) {
291+ _selectionsDom ?. removeChild ( _selectionsItemDom ) ;
290292 }
291- } else {
293+ }
294+ // Else, if we are configured to display <Tag>s then render them
295+ else if ( _config ?. renderTags && _selectionsDom ) {
292296 _createSelectedItem ( _selectionsDom , option ) ;
293297 }
294298 _model . toggleOption ( optionIndex ) ;
@@ -512,7 +516,8 @@ function Multiselect(element) {
512516
513517 _optionItemDoms . push ( optionsItemDom ) ;
514518
515- if ( isChecked ) {
519+ // Create <Tag> if enabled
520+ if ( isChecked && _config ?. renderTags ) {
516521 _createSelectedItem ( _selectionsDom , option ) ;
517522 }
518523 }
@@ -527,9 +532,10 @@ function Multiselect(element) {
527532
528533 /**
529534 * Set up and create the multiselect.
535+ * @param _modelConfig Multiselect configuration options
530536 * @returns {Multiselect } An instance.
531537 */
532- function init ( ) {
538+ function init ( _modelConfig ) {
533539 if ( ! setInitFlag ( _dom ) ) {
534540 return this ;
535541 }
@@ -544,7 +550,9 @@ function Multiselect(element) {
544550 _options = _dom . options || [ ] ;
545551
546552 if ( _options . length > 0 ) {
547- _model = new MultiselectModel ( _options , _name ) . init ( ) ;
553+ // Store underlying model so we can expose it externally
554+ _model = new MultiselectModel ( _options , _name , _modelConfig ) . init ( ) ;
555+ _config = _modelConfig ;
548556 const newDom = _populateMarkup ( ) ;
549557
550558 /* Removes <select> element,
@@ -562,6 +570,14 @@ function Multiselect(element) {
562570 return this ;
563571 }
564572
573+ /**
574+ * Allow external access to the underlying model for integration/customization when used in other applications.
575+ * @returns {object } Model
576+ */
577+ function getModel ( ) {
578+ return _model ;
579+ }
580+
565581 // Attach public events.
566582 this . init = init ;
567583 this . expand = expand ;
@@ -571,11 +587,16 @@ function Multiselect(element) {
571587 this . addEventListener = eventObserver . addEventListener ;
572588 this . removeEventListener = eventObserver . removeEventListener ;
573589 this . dispatchEvent = eventObserver . dispatchEvent ;
590+ this . getModel = getModel ;
591+ this . updateSelections = _updateSelections ;
592+ this . selectionClickHandler = _selectionClickHandler ;
593+ this . selectionKeyDownHandler = _selectionKeyDownHandler ;
574594
575595 return this ;
576596}
577597
578598Multiselect . BASE_CLASS = BASE_CLASS ;
579- Multiselect . init = ( ) => instantiateAll ( `.${ BASE_CLASS } ` , Multiselect ) ;
599+ Multiselect . init = ( config ) =>
600+ instantiateAll ( `.${ BASE_CLASS } ` , Multiselect , undefined , config ) ;
580601
581602export { Multiselect } ;
0 commit comments