cg-select/src/components/create-element.js

60 lines
1.7 KiB
JavaScript
Raw Normal View History

import { customStylesFormat } from './utils';
2022-10-17 19:49:06 +03:00
// Метод который создает и отвечает за поведение chips
2022-10-12 10:36:17 +03:00
export function createBreadcrumb(data, title, index, id) {
const { element, option, indexes, selectedItems } = data;
const { placeholder, styles } = option;
2022-10-10 20:05:02 +03:00
const selected = element.querySelector('.selected');
const liChip = document.createElement('li');
const textNode = document.createTextNode(title);
const svgIcon = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
2022-10-10 20:05:02 +03:00
const path1 = document.createElementNS('http://www.w3.org/2000/svg', 'path');
const path2 = document.createElementNS('http://www.w3.org/2000/svg', 'path');
svgIcon.setAttribute('viewBox', '0 0 10 10');
2022-10-10 20:05:02 +03:00
path1.setAttribute('d', 'M3,7 L7,3');
path2.setAttribute('d', 'M3,3 L7,7');
2022-10-18 14:13:14 +03:00
liChip.setAttribute('id', `tag-${index}-${id}`);
2022-10-10 20:05:02 +03:00
svgIcon.classList.add('svg-icon');
2022-10-10 20:05:02 +03:00
svgIcon.appendChild(path1);
svgIcon.appendChild(path2);
liChip.appendChild(textNode);
liChip.appendChild(svgIcon);
2022-10-10 20:05:02 +03:00
2022-10-18 14:13:14 +03:00
if (styles) {
const { chips } = styles;
customStylesFormat(chips, liChip);
}
svgIcon.addEventListener('click', (event) => {
2022-10-10 20:05:02 +03:00
event.stopPropagation();
const deleteIcon = indexes.indexOf(index);
indexes.splice(deleteIcon, 1);
selectedItems.splice(deleteIcon, 1);
let checkBox = '';
2022-10-10 20:05:02 +03:00
if (id) {
checkBox = document.getElementById(`chbox-${id}`);
} else {
checkBox = document.getElementById(`chbox-${index}`);
}
checkBox.checked = false;
checkBox.parentElement.classList.remove('active');
if (!selectedItems.length) {
selected.innerText = placeholder;
}
liChip.parentElement.removeChild(liChip);
2022-10-10 20:05:02 +03:00
});
return liChip;
2022-10-10 20:05:02 +03:00
}