Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | 1x 1x 1x 1x 1x 1x 1x 1x | import defineCustomElement from "../../../custom-element/helpers/defineCustomElement";
import findChild from "../../../custom-element/helpers/findChild";
import { CustomElementPropertyMetadata } from "../../../custom-element/interfaces";
import SelectionContainerMixin from "../../../custom-element/mixins/components/selection-container/SelectionContainerMixin";
import html from "../../../renderer/html";
import { NodePatchingData } from "../../../renderer/NodePatcher";
import Field from "../Field";
export default class ComboBox extends
SelectionContainerMixin(
Field
) {
static get properties(): Record<string, CustomElementPropertyMetadata> {
return {
/**
* The template to render the header of the combo box
*/
headerTemplate: {
attribute: 'header-template',
type: Function,
defer: true // Store the function itself instead of executing it to get its return value when initializing the property
},
/**
* The template to render the select text
*/
selectTemplate: {
attribute: 'select-template',
type: Function,
defer: true // Store the function itself instead of executing it to get its return value when initializing the property
},
/**
* The template to render the select text
*/
singleSelectionTemplate: {
attribute: 'single-selection-template',
type: Function,
defer: true // Store the function itself instead of executing it to get its return value when initializing the property
},
/**
* The template to render the select text
*/
multipleSelectionTemplate: {
attribute: 'multiple-selection-template',
type: Function,
defer: true // Store the function itself instead of executing it to get its return value when initializing the property
}
};
}
render(): NodePatchingData {
return html`<gcl-drop-down>
<span slot="header">${this.renderHeader()}</span>
<span slot="content">
<slot id="content" name="content"></slot>
</span>
</gcl-drop-down>`;
}
renderHeader(): NodePatchingData {
const {
selection
} = this;
switch (selection.length) {
case 0: return this.renderSelectTemplate();
case 1: return this.renderSingleSelectionTemplate(selection[0]);
default: return this.renderMultipleSelectionTemplate(selection);
}
}
renderSelectTemplate() : NodePatchingData {
const {
selectTemplate
} = this;
if (selectTemplate !== undefined) {
return selectTemplate();
}
else {
return html`<gcl-localized-text intl-key="please-select">Please select</gcl-localized-text>`;
}
}
renderSingleSelectionTemplate(selection) : NodePatchingData {
const {
singleSelectionTemplate,
container
} = this;
if (singleSelectionTemplate !== undefined) {
return singleSelectionTemplate(selection);
}
else {
return html`<span>${selection[container.displayField]}</span>`;
}
}
renderMultipleSelectionTemplate(selection) : NodePatchingData {
const {
multipleSelectionTemplate,
container
} = this;
if (multipleSelectionTemplate !== undefined) {
return multipleSelectionTemplate(selection);
}
else {
const {
idField,
displayField
} = container;
const data = selection.map(item => {
return {
[idField]: item[idField],
[displayField]: item[displayField]
};
});
return html`<gcl-data-list data=${data} id-field=${idField} display-field=${displayField} selectable="false"></gcl-data-list>`;
}
}
didMountCallback() {
super.didMountCallback?.();
// If the slotted content is a selection container, then attach the update header to the selectionChanged property
const content = this.document.getElementById('content');
const container = findChild(
content.assignedElements({ flatten: false }),
child => child.isSelectionContainer === true
) as any;
const selectionChangedHandler = container.selectionChanged;
if (selectionChangedHandler === undefined) {
container.selectionChanged = selection => this.selection = selection;
}
else {
container.selectionChanged = selection => {
this.selection = selection;
selectionChangedHandler(selection); // Include the original handler
}
}
this.container = container; // Needed to reference to get idField and displayField
}
}
defineCustomElement('gcl-combo-box', ComboBox); |