Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[PBNTR-888] Adding Select Validity Message #4327

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions playbook/app/entrypoints/playbook-rails.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ PbRadio.start()
import PbDraggable from 'kits/pb_draggable'
PbDraggable.start()

import PbSelect from 'kits/pb_select'
PbSelect.start()

import 'flatpickr'

// React-Rendered Rails Kits =====
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<%= form.url_field :example_url_field_validation, props: { label: true, required: true } %>
<%= form.text_area :example_text_area_validation, props: { label: true, required: true } %>
<%= form.dropdown_field :example_dropdown_validation, props: { label: true, options: example_dropdown_options, required: true } %>
<%= form.select :example_select_validation, [ ["Yes", 1], ["No", 2] ], props: { label: true, blank_selection: "Select One...", required: true } %>
<%= form.select :example_select_validation, [ ["Yes", 1], ["No", 2] ], props: { label: true, blank_selection: "Select One...", required: true, validation_message: "Please, select an option." } %>
<%= form.collection_select :example_collection_select_validation, example_collection, :value, :name, props: { label: true, blank_selection: "Select One...", required: true } %>
<%= form.check_box :example_checkbox, props: { text: "Example Checkbox", label: true, required: true } %>
<%= form.date_picker :example_date_picker_2, props: { label: true, required: true } %>
Expand Down
38 changes: 38 additions & 0 deletions playbook/app/pb_kits/playbook/pb_select/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import PbEnhancedElement from "../pb_enhanced_element";

const SELECT_WRAPPER_SELECTOR = "[data-pb-select]";
const SELECT_VALIDATION_MESSAGE_CLASS = ".pb_body_kit_negative";

export default class PbSelect extends PbEnhancedElement {
static get selector() {
return SELECT_WRAPPER_SELECTOR;
}

connect() {
this.setValidationMessage();
}

setValidationMessage() {
const validationMessage = this.element.dataset?.validationMessage;

if (validationMessage) {
const selectElement = this.element.querySelector("select");
const setErrorTextContent = (text, timeout) => {
setTimeout(() => {
const errorMessageElement = this.element.querySelector(SELECT_VALIDATION_MESSAGE_CLASS);
if (errorMessageElement) {
errorMessageElement.textContent = text;
} else {
setErrorTextContent(text, 100);
}
}, timeout);
};

selectElement.addEventListener("change", (e) => {
if (!e.target.checkValidity()) {
setErrorTextContent(validationMessage, 300);
}
});
}
}
}
8 changes: 8 additions & 0 deletions playbook/app/pb_kits/playbook/pb_select/select.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Select < Playbook::KitBase
prop :options, type: Playbook::Props::HashArray, required: false, default: []
prop :show_arrow, type: Playbook::Props::Boolean, default: false
prop :required, type: Playbook::Props::Boolean, default: false
prop :validation_message, type: Playbook::Props::String, default: ""

def classnames
classname + inline_class + compact_class + show_arrow_class
Expand Down Expand Up @@ -88,6 +89,13 @@ def angle_down_path
"app/pb_kits/playbook/utilities/icons/angle-down.svg"
end

def data_attributes
data = attributes[:data] || {}
data.merge!("data-pb-select" => true)
data.merge!("data-validation-message" => validation_message) if validation_message.present?
data
end

private

def error_class
Expand Down
Loading