static.ts 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import { TomOption } from 'tom-select/src/types';
  2. import TomSelect from 'tom-select';
  3. import { escape_html } from 'tom-select/src/utils';
  4. import { config } from './config';
  5. import { getElements } from '../util';
  6. // Initialize <select> elements with statically-defined options
  7. export function initStaticSelects(): void {
  8. for (const select of getElements<HTMLSelectElement>(
  9. 'select:not(.api-select):not(.color-select)',
  10. )) {
  11. new TomSelect(select, {
  12. ...config,
  13. });
  14. }
  15. }
  16. // Initialize color selection fields
  17. export function initColorSelects(): void {
  18. function renderColor(item: TomOption, escape: typeof escape_html) {
  19. return `<div><span class="dropdown-item-indicator color-label" style="background-color: #${escape(
  20. item.value,
  21. )}"></span> ${escape(item.text)}</div>`;
  22. }
  23. for (const select of getElements<HTMLSelectElement>('select.color-select')) {
  24. new TomSelect(select, {
  25. ...config,
  26. render: {
  27. option: renderColor,
  28. item: renderColor,
  29. },
  30. });
  31. }
  32. }