Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

[PBNTR-504] Adding Rails Indeterminate Checkbox variant doc #3918

Merged
merged 2 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions playbook/app/pb_kits/playbook/pb_checkbox/checkbox.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
) do %>
<%= content.presence || object.input %>
<% if object.indeterminate %>
<span class="pb_checkbox_indeterminate">
<span data-pb-checkbox-icon-span="true" class="pb_checkbox_indeterminate">
<%= pb_rails("icon", props: { icon: "minus", classname: "indeterminate_icon", fixed_width: true}) %>
<%= pb_rails("icon", props: { icon: "check", classname: "check_icon hidden", fixed_width: true}) %>
</span>
<% else %>
<span class="pb_checkbox_checkmark">
<span data-pb-checkbox-icon-span="true" class="pb_checkbox_checkmark">
carloslimasd marked this conversation as resolved.
Show resolved Hide resolved
<%= pb_rails("icon", props: { icon: "check", classname: "check_icon", fixed_width: true}) %>
<%= pb_rails("icon", props: { icon: "minus", classname: "indeterminate_icon hidden", fixed_width: true}) %>
</span>
Expand Down
4 changes: 0 additions & 4 deletions playbook/app/pb_kits/playbook/pb_checkbox/checkbox.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ class Checkbox < Playbook::KitBase
prop :form_spacing, type: Playbook::Props::Boolean,
default: false

def checked_html
checked ? "checked='true'" : nil
end

def classname
generate_classname("pb_checkbox_kit", checked_class) + indeterminate_class + error_class
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,84 @@
<%= pb_rails("checkbox" , props: {
text: "Select ",
value: "checkbox-value",
name: "main",
indeterminate: true,
id: "test-indeterminate-js"
}) %>
<% checkboxes = [
{ name: 'Coffee', id: 'coffee', checked: false },
{ name: 'Ice Cream', id: 'ice-cream', checked: false },
{ name: 'Chocolate', id: 'chocolate', checked: true }
] %>

<%= pb_rails("table", props: { container: false, size: "md" }) do %>
<thead>
<tr>
<th>
<%= pb_rails("checkbox", props: {
checked: true,
text: "Uncheck All",
value: "checkbox-value",
name: "main-checkbox",
indeterminate: true,
id: "indeterminate-checkbox"
}) %>
</th>
</tr>
</thead>

<tbody>
<% checkboxes.each do |checkbox| %>
<tr>
<td>
<%= pb_rails("checkbox", props: {
checked: checkbox[:checked],
text: checkbox[:name],
value: checkbox[:id],
name: "#{checkbox[:id]}-indeterminate-checkbox",
id: "#{checkbox[:id]}-indeterminate-checkbox",
}) %>
</td>
</tr>
<% end %>
</tbody>
<% end %>

<script>
document.addEventListener('DOMContentLoaded', function() {
const mainCheckboxWrapper = document.getElementById('indeterminate-checkbox');
const mainCheckbox = document.getElementsByName("main-checkbox")[0];
const childCheckboxes = document.querySelectorAll('input[type="checkbox"][id$="indeterminate-checkbox"]');

const updateMainCheckbox = () => {
// Count the number of checked child checkboxes
const checkedCount = Array.from(childCheckboxes).filter(cb => cb.checked).length;
// Determine if the main checkbox should be in an indeterminate state
const indeterminate = checkedCount > 0 && checkedCount < childCheckboxes.length;

// Set the main checkbox states
mainCheckbox.indeterminate = indeterminate;
mainCheckbox.checked = checkedCount > 0;

// Determine the main checkbox label based on the number of checked checkboxes
const text = checkedCount === 0 ? 'Check All' : 'Uncheck All';

// Determine the icon class to add and remove based on the number of checked checkboxes
const iconClassToAdd = checkedCount === 0 ? 'pb_checkbox_checkmark' : 'pb_checkbox_indeterminate';
const iconClassToRemove = checkedCount === 0 ? 'pb_checkbox_indeterminate' : 'pb_checkbox_checkmark';

// Update main checkbox label
mainCheckboxWrapper.getElementsByClassName('pb_body_kit')[0].textContent = text;

// Add and remove the icon class to the main checkbox wrapper
mainCheckboxWrapper.querySelector('[data-pb-checkbox-icon-span]').classList.add(iconClassToAdd);
mainCheckboxWrapper.querySelector('[data-pb-checkbox-icon-span]').classList.remove(iconClassToRemove);

// Toggle the visibility of the checkbox icon based on the indeterminate state
mainCheckboxWrapper.getElementsByClassName("indeterminate_icon")[0].classList.toggle('hidden', !indeterminate);
mainCheckboxWrapper.getElementsByClassName("check_icon")[0].classList.toggle('hidden', indeterminate);
};

mainCheckbox.addEventListener('change', function() {
childCheckboxes.forEach(cb => cb.checked = this.checked);
updateMainCheckbox();
});

childCheckboxes.forEach(cb => {
cb.addEventListener('change', updateMainCheckbox);
});
});
</script>
Loading