Thursday, June 04, 2026 3:49:25 AM
> role_selector_controller.js
import ApplicationController from "./application_controller";
import Choices from "choices.js";
import "choices.js/public/assets/styles/choices.css";

// Connects to data-controller="role-selector"
export default class extends ApplicationController {
  static values = { id: String };

  connect() {
    const element = this.element;
    if (element.classList.contains("choices__input")) return;

    const rolesJSON = element.dataset.roles;
    if (rolesJSON == null || rolesJSON.length === 0) return;

    const roles = JSON.parse(rolesJSON);
    this.choices = new Choices(element, {
      choices: roles,
      allowHTML: true,
      removeItemButton: true,
      placeholderValue: "  Search for roles to add...",
    });
  }

  // Controller->Controller event
  handleEnableChange({ detail: { enabled, targetId } }) {
    if (this.choices == null || this.idValue != targetId) return;

    if (enabled) {
      this.choices.enable();
    } else {
      this.choices.disable();
    }
  }
}
All opinions represented herein are my own
- © 2024 - 2026 itsthedevman
- build 4294fb2