forked from ontoportal/ontoportal_web_ui
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #310 from ontoportal-lirmm/feature/update-select-c…
…omponent Feature: update select component
- Loading branch information
1 parent
dbb14a0
commit a9d04f2
Showing
7 changed files
with
82 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 5 additions & 10 deletions
15
app/components/select_input_component/select_input_component.html.haml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,5 @@ | ||
%div{:data => { controller: "select-input", 'select-input': {'multiple-value': @multiple.to_s}}} | ||
= select_tag(@name, options_values, { multiple: @multiple, class: "form-control", id: "select_#{@id}", data: { action: "select-input#toggleOtherValue", "select-input-target": "selectedValues" } }) | ||
|
||
%div.d-flex.mt-1{style: "display:#{!@open_to_add_values ? 'none !important;' : 'block;'}"} | ||
= text_field_tag("add_#{@id}", nil, :style => "margin-right: 1em;width: 16em;display: none;", :placeholder => "Or provide the value", | ||
data: {action: "keydown.enter->select-input#addValue", "select-input-target": "inputValueField"}, class: 'metadataInput form-control form-control-sm') | ||
|
||
%button.btn.btn-primary.btn-sm.add-value-btn{id: "btnAdd#{@id}", style: "display: none;", | ||
data: { action: "select-input#addValue", "select-input-target": "btnValueField"}} | ||
Add new value | ||
%select{id: "select_#{@id}", autocomplete:"off", multiple: @multiple ,data: {controller: "select-input", 'select-input':{'multiple-value':@multiple, 'open-add-value':@open_to_add_values}}} | ||
- @values.each do |value| | ||
%option{value:@value, selected: value.eql?(@selected)} | ||
= value | ||
|
137 changes: 8 additions & 129 deletions
137
app/components/select_input_component/select_input_component_controller.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,137 +1,16 @@ | ||
import {Controller} from "@hotwired/stimulus" | ||
import {useChosen} from "../../javascript/mixins/useChosen"; | ||
import { Controller } from "@hotwired/stimulus" | ||
import TomSelect from "tom-select" | ||
|
||
export default class extends Controller { | ||
|
||
static values = { | ||
other: {type: Boolean, default: true}, | ||
multiple: {type: Boolean, default: false} | ||
} | ||
|
||
static targets = ["btnValueField", "inputValueField", "selectedValues"] | ||
|
||
connect() { | ||
this.initMultipleSelect() | ||
this.#displayOtherValueField() | ||
} | ||
|
||
toggleOtherValue() { | ||
if (this.otherValue && !this.multipleValue) { | ||
this.#toggle() | ||
let myOptions = {} | ||
if (this.data.get("multipleValue")) { | ||
myOptions['plugins'] = ['remove_button']; | ||
} | ||
} | ||
|
||
addValue(event) { | ||
event.preventDefault() | ||
|
||
if (this.inputValueFieldTarget.value) { | ||
let newOption = this.inputValueFieldTarget.value; | ||
this.#addNewOption(newOption) | ||
this.#selectNewOption(newOption) | ||
if (!this.multipleValue) { | ||
this.#hideOtherValueField() | ||
} | ||
if (this.data.get("openAddValue")) { | ||
myOptions['create'] = true; | ||
} | ||
new TomSelect(this.element, myOptions); | ||
} | ||
|
||
|
||
initMultipleSelect() { | ||
this.#addEmptyOption() | ||
useChosen(this.selectedValuesTarget, { | ||
width: '100%', | ||
search_contains: true, | ||
allow_single_deselect: !this.multipleValue, | ||
}, (event) => { | ||
if(this.multipleValue){ | ||
let selected = event.target.selectedOptions | ||
if (selected.length === 0) { | ||
this.#selectEmptyOption() | ||
} else { | ||
this.#unSelectEmptyOption() | ||
} | ||
} | ||
}) | ||
} | ||
|
||
#selectEmptyOption() { | ||
this.emptyOption.selected = true | ||
this.emptyOption.disabled = false | ||
} | ||
|
||
#unSelectEmptyOption() { | ||
this.emptyOption.selected = false | ||
this.emptyOption.disabled = true | ||
} | ||
|
||
#addEmptyOption() { | ||
this.emptyOption = document.createElement("option") | ||
this.emptyOption.innerHTML = '' | ||
this.emptyOption.value = '' | ||
this.selectedValuesTarget.prepend(this.emptyOption) | ||
} | ||
|
||
#selectNewOption(newOption) { | ||
let selectedOptions = this.#selectedOptions(); | ||
|
||
|
||
if (Array.isArray(selectedOptions)) { | ||
selectedOptions.push(newOption); | ||
} else { | ||
selectedOptions = []; | ||
selectedOptions.push(newOption) | ||
} | ||
|
||
this.selectedValuesTarget.value = selectedOptions | ||
if (this.multipleValue) { | ||
const options = this.selectedValuesTarget.options | ||
for (const element of options) { | ||
element.selected = selectedOptions.indexOf(element.value) >= 0; | ||
} | ||
jQuery(this.selectedValuesTarget).trigger("chosen:updated") | ||
} | ||
|
||
} | ||
|
||
#addNewOption(newOption) { | ||
let option = document.createElement("option"); | ||
option.value = newOption; | ||
option.text = newOption; | ||
this.selectedValuesTarget.add(option) | ||
} | ||
|
||
#selectedOptions() { | ||
if (this.multipleValue) { | ||
const selectedOptions = []; | ||
for (let option of this.selectedValuesTarget.options) { | ||
if (option.selected) { | ||
selectedOptions.push(option.value); | ||
} | ||
} | ||
return selectedOptions | ||
} else { | ||
return this.selectedValuesTarget.value | ||
} | ||
} | ||
|
||
#toggle() { | ||
if (this.selectedValuesTarget.value === 'other') { | ||
this.#displayOtherValueField() | ||
} else { | ||
this.#hideOtherValueField() | ||
} | ||
} | ||
|
||
#displayOtherValueField() { | ||
this.inputValueFieldTarget.value = "" | ||
this.btnValueFieldTarget.style.display = 'block' | ||
this.inputValueFieldTarget.style.display = 'block' | ||
} | ||
|
||
#hideOtherValueField() { | ||
this.inputValueFieldTarget.value = "" | ||
this.btnValueFieldTarget.style.display = 'none' | ||
this.inputValueFieldTarget.style.display = 'none' | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters