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

Remove select2 and uses choices.js #4319

Merged
merged 12 commits into from
Jan 27, 2025
Merged

Conversation

sandeepsajan0
Copy link
Member

@sandeepsajan0 sandeepsajan0 commented Jan 6, 2025

Fixes #4218

Test Steps

  • Check all filters for Project list, Invoice list and reporting all page. It should working as expected but using the new UI of choices js.
  • Try category question in any streamfield form, it should be working fine and similar but for more than 32 options there should be a slight Ui difference but it should be working fine.
  • Play around all submissions or round/lab filters and dropdowns, everything should be working fine with choices.js

@sandeepsajan0 sandeepsajan0 force-pushed the enhancement/gh-4218-select2-choicejs branch from d657275 to e60e2a1 Compare January 7, 2025 10:43
@sandeepsajan0 sandeepsajan0 changed the title Update projects, invoices and reporting filters to use choices.js Remove select2 and uses choices.js Jan 9, 2025
@sandeepsajan0 sandeepsajan0 marked this pull request as ready for review January 9, 2025 09:02
@frjo frjo added Type: Enhancement This is an improvement of an existing thing (not a new thing, which would be a feature). Type: Minor Minor change, used in release drafter labels Jan 9, 2025
@frjo
Copy link
Member

frjo commented Jan 10, 2025

I made use of htmx.onLoad() in file-uploads.js and it works really well. It gets loaded on "DOMContentLoaded" and after any htmx action.

// We use htmx.onLoad() so it will initilise file uploads in htmx dialogs.
htmx.onLoad(function () {
// Initialize django-file-form
document.querySelectorAll("form").forEach(function (form) {
// Prevent initializing it multiple times and run it for forms
// that have a `form_id` field added by django-file-form.
if (!form.initUploadFieldsDone && form.querySelector("[name=form_id]")) {
init(form);
form.initUploadFieldsDone = true;
}
});
/**
* Initialize django-file-form for a form.
* @param {object} form The form to initialize.
*/
function init(form) {
if (document.querySelectorAll(".form__group--file").length) {
window.initUploadFields(form);
// Hide wrapper elements for hidden inputs added by django-file-form
document.querySelectorAll("input[type=hidden]").forEach(function (input) {
const closestFormGroup = input.closest(".form__group");
if (closestFormGroup) {
closestFormGroup.style.display = "none";
}
});
}
}
});

@sandeepsajan0 sandeepsajan0 force-pushed the enhancement/gh-4218-select2-choicejs branch from e2c0acb to 9f4ef09 Compare January 13, 2025 06:51
@sandeepsajan0
Copy link
Member Author

@frjo It is ready for testing. I'll create a separate PR for the dynamicity of properties of the Choices element in base.html while removing similar code form other places like reviewers etc.

@sandeepsajan0 sandeepsajan0 requested review from theskumar and frjo and removed request for theskumar January 13, 2025 06:56
Copy link
Member

@frjo frjo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works well in all the places I tested it, project table, meta terms, reminders and batch actions. The implementation look good as well, clean and neat.

Some comments on the styling of selects.

@frjo
Copy link
Member

frjo commented Jan 13, 2025

choices

  1. The placeholder acts lika a label but is light grey. A separate label or placeholder text that is darker and maybe semibold until you focus the field?
  2. Can the dropdown be made wider so most of the items do not need to wrap?
  3. Add white background so we do not see the page behind inside the select box.

@theskumar
Copy link
Member

The implementation details looks much better now, neat and clean. Thanks for making that change.

I've not checked for styling issues, yet. @frjo seems to have recommendation, plz consider using them or consider an alternative dropdown ui similar to how multiple select works in the submission all page.

@sandeepsajan0
Copy link
Member Author

@frjo The above issues have been fixed, you may check it again. It is ready for testing.

@frjo frjo added Status: Needs testing Tickets that need testing/qa Status: Needs dev testing 🧑‍💻 Tasks that should be tested by the dev team labels Jan 20, 2025
@frjo
Copy link
Member

frjo commented Jan 20, 2025

Found this warnings in the console.

Skärmavbild 2025-01-20 kl  08 37 07

Seems to not affect anything badly but would be neater to have them gone.

Adding a check to see if the element has been activated like below seems to work.

        <!-- choices select -->
        <script type="module">
            import Choices from "{% static 'js/esm/choices.js-10-2-0.js' %}";
            let selectActivated = [];

            htmx.onLoad(function() {
                const selectElements = document.querySelectorAll("[data-js-choices]");

                selectElements.forEach((selectElement) => {
                    if (!selectActivated.includes(selectElement.id)) {
                        new Choices(selectElement, {
                            shouldSort: false,
                            allowHTML: true,
                            removeItemButton: true,
                        });
                        selectActivated.push(selectElement.id);
                    }
                });

                const choicesElements = document.querySelectorAll(".choices__input--cloned")
                choicesElements.forEach((choiceElement) => {
                    const inputPlaceholder = choiceElement.getAttribute("placeholder");
                    // Get the computed min-width of the input element otherwise it reset to 1
                    const minWidth = window.getComputedStyle(choiceElement).getPropertyValue("min-width");
                    choiceElement.addEventListener("focus", () => {
                        if (choiceElement) {
                            choiceElement.placeholder = ""
                        }
                    });

                    choiceElement.addEventListener("blur", () => {
                        if (choiceElement) {
                            choiceElement.placeholder = inputPlaceholder
                            choiceElement.style.minWidth = minWidth;
                        }
                    });
                });
            });
        </script>

const minWidth = window.getComputedStyle(choiceElement).getPropertyValue("min-width");
choiceElement.addEventListener("focus", () => {
if (choiceElement) {
choiceElement.placeholder = "Search..."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to add something like choiceElement.style.minWidth = "10ch"; or the search placeholder gets cut of if the original placeholder is shorter.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, 7ch looks enough for the search placeholder.

allowHTML: true,
removeItemButton: true,
});
selectElement.choicesInstance = choicesInstance;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not enough to set selectElement.choicesInstance = true;?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cleaner solution than my hack in any case.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, true should work. It will be more efficient.

@sandeepsajan0 sandeepsajan0 requested a review from frjo January 27, 2025 05:12
@frjo
Copy link
Member

frjo commented Jan 27, 2025

Latest version deployed to test now.

@wes-otf wes-otf added Status: Tested - approved for live ✅ and removed Status: Needs testing Tickets that need testing/qa Status: Needs dev testing 🧑‍💻 Tasks that should be tested by the dev team labels Jan 27, 2025
@frjo frjo merged commit 946b8b3 into main Jan 27, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Tested - approved for live ✅ Type: Enhancement This is an improvement of an existing thing (not a new thing, which would be a feature). Type: Minor Minor change, used in release drafter
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Replace select2 with choices.js
4 participants