Skip to content

Commit

Permalink
[PBNTR-405] Dropdown Kit in Rails Form Fixes (#3536)
Browse files Browse the repository at this point in the history
**What does this PR do?** A clear and concise description with your
runway ticket url.
[Runway Story](https://runway.powerhrg.com/backlog_items/PBNTR-405)

**Screenshots:** 


**How to test?** Steps to confirm the desired behavior:
1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
4. See addition/change
  • Loading branch information
nidaqg authored Jul 25, 2024
1 parent 01f01fb commit b458deb
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 35 deletions.
10 changes: 7 additions & 3 deletions playbook/app/pb_kits/playbook/pb_dropdown/dropdown.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
<%= pb_rails("caption", props: {text: object.label, margin_bottom:"xs"}) %>
<% end %>
<div class="dropdown_wrapper<%= error_class %>" style="position: relative">
<input type="hidden" name="<%= object.name %>" id="dropdown-selected-option" value="<%= input_default_value %>" />
<input id="dropdown-form-validation" name="<%= object.name %>_form_validation" value="<%= input_default_value %>" style="display: none" <%= object.required ? "required" : ""%> />

<input
data-default-value="<%= input_default_value %>"
id="dropdown-selected-option"
name="<%= object.name %>"
style="display: none"
<%= object.required ? "required" : ""%>
/>
<% if content.present? %>
<%= content.presence %>
<%= pb_rails("body", props: { status: "negative", text: object.error }) %>
Expand Down
2 changes: 1 addition & 1 deletion playbook/app/pb_kits/playbook/pb_dropdown/dropdown.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def error_class
end

def input_default_value
default_value.present? ? default_value.transform_keys(&:to_s) : ""
default_value.present? ? default_value.transform_keys(&:to_s)["id"] : ""
end

def options_with_blank
Expand Down
68 changes: 37 additions & 31 deletions playbook/app/pb_kits/playbook/pb_dropdown/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ const DOWN_ARROW_SELECTOR = "#dropdown_open_icon";
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]";
const DROPDOWN_INPUT = "#dropdown-selected-option";

export default class PbDropdown extends PbEnhancedElement {
static get selector() {
Expand Down Expand Up @@ -47,14 +47,13 @@ export default class PbDropdown extends PbEnhancedElement {

handleOptionClick(event) {
const option = event.target.closest(OPTION_SELECTOR);
const hiddenInput = this.element.querySelector("#dropdown-selected-option");
const inputFormValidation = this.element.querySelector(INPUT_FORM_VALIDATION);
const hiddenInput = this.element.querySelector(DROPDOWN_INPUT);

if (option) {
const value = option.dataset.dropdownOptionLabel;
hiddenInput.value = JSON.parse(value).id;
inputFormValidation.value = JSON.parse(value).id;
this.clearFormValidation(inputFormValidation);
this.clearFormValidation(hiddenInput);

this.onOptionSelected(value, option);
}
}
Expand Down Expand Up @@ -160,46 +159,51 @@ export default class PbDropdown extends PbEnhancedElement {
}

handleFormValidation() {
const inputFormValidation = this.element.querySelector(INPUT_FORM_VALIDATION);

inputFormValidation.addEventListener("invalid", function (event) {
if (inputFormValidation.hasAttribute("required") && inputFormValidation.value === "") {
event.preventDefault();
inputFormValidation.closest(".dropdown_wrapper").classList.add("error");
}
}, true);
const hiddenInput = this.element.querySelector(DROPDOWN_INPUT);

hiddenInput.addEventListener(
"invalid",
function (event) {
if (hiddenInput.hasAttribute("required") && hiddenInput.value === "") {
event.preventDefault();
hiddenInput.closest(".dropdown_wrapper").classList.add("error");
}
},
true
);
}

clearFormValidation(input) {
if (input.checkValidity()) {
const dropdownWrapperElement = input.closest(".dropdown_wrapper");
dropdownWrapperElement.classList.remove("error");

const errorLabelElement = dropdownWrapperElement.querySelector(".pb_body_kit_negative");
const errorLabelElement = dropdownWrapperElement.querySelector(
".pb_body_kit_negative"
);
if (errorLabelElement) {
errorLabelElement.remove();
}
}
}

setDefaultValue() {
const hiddenInput = this.element.querySelector("#dropdown-selected-option");
const hiddenInput = this.element.querySelector(DROPDOWN_INPUT);
const options = this.element.querySelectorAll(OPTION_SELECTOR);

if (hiddenInput.value) {
const inputFormValidation = this.element.querySelector(INPUT_FORM_VALIDATION);
const defaultValue = JSON.parse(hiddenInput.value.replaceAll("=>", ":"));
const options = this.element.querySelectorAll(OPTION_SELECTOR);
const defaultValue = hiddenInput.dataset.defaultValue || "";
hiddenInput.value = defaultValue;

options.forEach((option) => {
if (defaultValue.id === JSON.parse(option.dataset.dropdownOptionLabel).id) {
option.classList.add("pb_dropdown_option_selected");
}
if (defaultValue) {
const selectedOption = Array.from(options).find((option) => {
return (
JSON.parse(option.dataset.dropdownOptionLabel).id === defaultValue
);
});

this.setTriggerElementText(defaultValue.label);

hiddenInput.value = defaultValue.id;
inputFormValidation.value = defaultValue.id;
selectedOption.classList.add("pb_dropdown_option_selected");
this.setTriggerElementText(
JSON.parse(selectedOption.dataset.dropdownOptionLabel).label
);
}
}

Expand All @@ -214,11 +218,13 @@ export default class PbDropdown extends PbEnhancedElement {
}

resetDropdownValue() {
const hiddenInput = this.element.querySelector("#dropdown-selected-option");
const inputFormValidation = this.element.querySelector(INPUT_FORM_VALIDATION);
const hiddenInput = this.element.querySelector(DROPDOWN_INPUT);
const options = this.element.querySelectorAll(OPTION_SELECTOR);
options.forEach((option) => {
option.classList.remove("pb_dropdown_option_selected");
});

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

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

0 comments on commit b458deb

Please sign in to comment.