Skip to content

Commit

Permalink
Implementing form reseting dropdown value
Browse files Browse the repository at this point in the history
  • Loading branch information
carloslimasd committed Jul 16, 2024
1 parent 313cff9 commit 3dac3b6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class DropdownTrigger < Playbook::KitBase
prop :custom_display

def data
Hash(prop(:data)).merge(dropdown_trigger: true)
Hash(prop(:data)).merge(dropdown_trigger: true, dropdown_placeholder: default_display_placeholder)
end

def classname
Expand Down
22 changes: 14 additions & 8 deletions playbook/app/pb_kits/playbook/pb_dropdown/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const UP_ARROW_SELECTOR = "#dropdown_close_icon";
const OPTION_SELECTOR = "[data-dropdown-option-label]";
const CUSTOM_DISPLAY_SELECTOR = "[data-dropdown-custom-trigger]";
const INPUT_FORM_VALIDATION = "#dropdown-form-validation";
const DROPDOWN_TRIGGER_DISPLAY = "#dropdown_trigger_display";
const DROPDOWN_PLACEHOLDER = "[data-dropdown-placeholder]";

export default class PbDropdown extends PbEnhancedElement {
static get selector() {
Expand Down Expand Up @@ -85,9 +87,7 @@ export default class PbDropdown extends PbEnhancedElement {
}

onOptionSelected(value, selectedOption) {
const triggerElement = this.element.querySelector(
"#dropdown_trigger_display"
);
const triggerElement = this.element.querySelector(DROPDOWN_TRIGGER_DISPLAY);
const customDisplayElement = this.element.querySelector(
"#dropdown_trigger_custom_display"
);
Expand Down Expand Up @@ -196,10 +196,7 @@ export default class PbDropdown extends PbEnhancedElement {
}
});

const triggerElement = this.element.querySelector("#dropdown_trigger_display");
if (triggerElement) {
triggerElement.textContent = defaultValue.label;
}
this.setTriggerElementText(defaultValue.label);

hiddenInput.value = defaultValue.id;
inputFormValidation.value = defaultValue.id;
Expand All @@ -211,7 +208,6 @@ export default class PbDropdown extends PbEnhancedElement {

if (form) {
form.addEventListener("reset", () => {
console.log("reset");
this.resetDropdownValue();
});
}
Expand All @@ -223,5 +219,15 @@ export default class PbDropdown extends PbEnhancedElement {

hiddenInput.value = "";
inputFormValidation.value = "";

const defaultPlaceholder = this.element.querySelector(DROPDOWN_PLACEHOLDER);
this.setTriggerElementText(defaultPlaceholder.dataset.dropdownPlaceholder);
}

setTriggerElementText(text) {
const triggerElement = this.element.querySelector(DROPDOWN_TRIGGER_DISPLAY);
if (triggerElement) {
triggerElement.textContent = text;
}
}
}

0 comments on commit 3dac3b6

Please sign in to comment.