Skip to content

Commit

Permalink
Merge pull request #45 from wolffe/revert-43-main
Browse files Browse the repository at this point in the history
Revert "Implement min and max number of selected options for multiple select"
  • Loading branch information
wolffe authored Apr 3, 2024
2 parents 36c6b6e + 020b53d commit f595cf0
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 47 deletions.
2 changes: 1 addition & 1 deletion css/tail.select.css
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
border-color: transparent;
border-radius: 3px;
box-shadow: none;
cursor: pointer;

color: var(--tail-select--text);
border-color: var(--tail-select--meta);
background-color: transparent;
Expand Down
46 changes: 1 addition & 45 deletions js/tail.select.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ const tail = {
// Default options
const defaultOptions = {
multiTags: false,
multiLimit: Infinity,
minSelectionLimit: 0,
multiCounter: true,
theme: 'light', // light|dark
classNames: 'tail-default',
Expand All @@ -23,27 +21,15 @@ const tail = {
none: "None",
placeholder: "Select an option...",
search: "Type in to search...",
limitExceded: "You can only select {0} options!",
minimumReached: "You need to select at least {0} options!",
}
};

// Merge default options with provided options
const opts = { ...defaultOptions, ...options };

// Extract options
const { multiTags, multiLimit, minSelectionLimit, multiCounter, theme, classNames, strings } = opts;
const { multiTags, multiCounter, theme, classNames, strings } = opts;

//Fill placeholder strings
if (strings != null) {
if (strings.limitExceded != null && multiLimit < Infinity) {
strings.limitExceded = strings.limitExceded.replace("{0}", multiLimit);
}

if (strings.minimumReached != null && minSelectionLimit > 0) {
strings.minimumReached = strings.minimumReached.replace("{0}", minSelectionLimit);
}
}
//
const originalSelects = document.querySelectorAll(selector);

Expand Down Expand Up @@ -297,11 +283,6 @@ const tail = {
}

function toggleAll(originalSelect, toggleAllCheckbox) {
if (originalSelect.options.length > multiLimit) {
showErrorMessage(strings.limitExceded || `You can only select ${multiLimit} options!`);
return;
}

const isChecked = toggleAllCheckbox.checked;
const optionCheckboxes = nestedList.querySelectorAll(
'input[type="checkbox"]'
Expand Down Expand Up @@ -334,15 +315,6 @@ const tail = {

function toggleOption(checkbox) {
if (originalSelect.multiple) {
if (checkSelectConstraints(checkbox.checked)) {
checkbox.checked = !checkbox.checked;
hideDropdown();
const errorMessage = originalSelect.selectedOptions.length >= multiLimit
? strings.limitExceded || `You can only select ${multiLimit} options!`
: strings.minimumReached || `You need to select at least ${minSelectionLimit} options!`;
showErrorMessage(errorMessage);
return;
}
updateOriginalOptionState(originalSelect, checkbox);
} else {
// For single-select, uncheck all and check the current one
Expand All @@ -355,22 +327,6 @@ const tail = {
}
}

function checkSelectConstraints(checked) {
return checked ? originalSelect.selectedOptions.length >= multiLimit : originalSelect.selectedOptions.length == minSelectionLimit;
}

function showErrorMessage(msg) {
const paragraph = document.createElement('p');
paragraph.classList.add('mt-2', 'text-sm', 'text-red-600');
paragraph.textContent = msg;
customDropdown.parentNode.appendChild(paragraph);

// Remove the paragraph after 2 seconds
setTimeout(() => {
customDropdown.parentNode.removeChild(paragraph);
}, 2000);
}

function toggleOptgroup(optgroupCheckbox) {
const isChecked = optgroupCheckbox.checked;
const nestedOptionsList = optgroupCheckbox
Expand Down
Loading

0 comments on commit f595cf0

Please sign in to comment.