import QtQuick // A labelled input with an inline, filtered suggestion list. The user can type // to narrow the list, click an entry, or use the arrow keys: Up/Down move a // visible highlight, Enter selects it, Escape closes the list. A custom value // is still allowed. Used for timezone, locale, keymap, provider and model so // the user picks from what the system actually offers instead of typing from // memory. `options` may be strings or `{value,label}` pairs; the value is the // identifier the backend validates, the label is what the user reads. Column { id: combo property string label: "" property string placeholder: "" property string error: "" property var options: [] property int maxVisible: 8 property string hint: "Type to filter · ↑ ↓ to choose · Enter to select" property alias text: field.text property alias inputItem: field.inputItem property var suggestions: [] property int highlightIndex: -1 property bool dismissed: false readonly property bool open: field.inputItem.activeFocus && !combo.dismissed && combo.suggestions.length > 0 signal edited(string value) signal selected(string value) width: parent ? parent.width : implicitWidth spacing: 4 function optionValue(option) { return (option !== null && typeof option === "object") ? ("" + option.value) : ("" + option); } function optionLabel(option) { return (option !== null && typeof option === "object" && option.label !== undefined) ? ("" + option.label) : ("" + option); } function refresh() { const query = (field.text || "").toLowerCase(); const all = combo.options || []; const out = []; for (let i = 0; i < all.length && out.length < combo.maxVisible; i++) { const value = combo.optionValue(all[i]); const label = combo.optionLabel(all[i]); if (query.length === 0 || value.toLowerCase().indexOf(query) !== -1 || label.toLowerCase().indexOf(query) !== -1) out.push({ value: value, label: label }); } combo.suggestions = out; if (combo.highlightIndex >= out.length) combo.highlightIndex = out.length - 1; } function choose(index) { if (index < 0 || index >= combo.suggestions.length) return; const chosen = combo.suggestions[index]; field.inputItem.text = chosen.value; combo.suggestions = []; combo.highlightIndex = -1; combo.dismissed = false; combo.selected(chosen.value); } function commit() { if (combo.open && combo.highlightIndex >= 0) combo.choose(combo.highlightIndex); else { combo.suggestions = []; combo.dismissed = true; } } function forceFocus() { field.inputItem.forceActiveFocus(); } onOptionsChanged: combo.refresh() Component.onCompleted: combo.refresh() PhoebusField { id: field width: parent.width label: combo.label placeholder: combo.placeholder error: combo.error escapeConsumed: combo.open returnConsumed: combo.open onEdited: value => { combo.dismissed = false; combo.refresh(); combo.edited(value); } onKeyUp: { if (combo.suggestions.length === 0) return; combo.dismissed = false; combo.highlightIndex = combo.highlightIndex < 0 ? combo.suggestions.length - 1 : (combo.highlightIndex <= 0 ? combo.suggestions.length - 1 : combo.highlightIndex - 1); } onKeyDown: { if (combo.suggestions.length === 0) return; combo.dismissed = false; combo.highlightIndex = combo.highlightIndex < 0 ? 0 : (combo.highlightIndex >= combo.suggestions.length - 1 ? 0 : combo.highlightIndex + 1); } onKeyEscape: combo.dismissed = true onAccepted: combo.commit() } // Show suggestions as soon as the field takes focus, and clear the // highlight when it loses focus. Connections { target: field.inputItem function onActiveFocusChanged() { if (field.inputItem.activeFocus) { combo.dismissed = false; combo.refresh(); } else { combo.dismissed = false; combo.highlightIndex = -1; } } } Column { width: parent.width spacing: 0 visible: combo.open Repeater { model: combo.open ? combo.suggestions : [] delegate: Rectangle { id: row required property var modelData required property int index width: parent.width height: 30 color: index === combo.highlightIndex ? Theme.amberDeep : (hover.containsMouse ? Theme.surfaceRaised : Theme.surface) border.width: 1 border.color: index === combo.highlightIndex ? Theme.amber : Theme.border Accessible.role: Accessible.ListItem Accessible.name: row.modelData.label Accessible.selected: index === combo.highlightIndex Text { anchors.verticalCenter: parent.verticalCenter anchors.left: parent.left anchors.right: parent.right anchors.leftMargin: 12 anchors.rightMargin: 12 text: row.modelData.label elide: Text.ElideRight color: index === combo.highlightIndex ? Theme.charcoal : Theme.warmWhite font.family: Theme.family font.pixelSize: Theme.fontSmall } MouseArea { id: hover anchors.fill: parent hoverEnabled: true cursorShape: Qt.PointingHandCursor onClicked: combo.choose(index) } } } } Text { width: parent.width visible: field.inputItem.activeFocus && combo.options.length > 0 text: combo.hint color: Theme.muted font.family: Theme.family font.pixelSize: Theme.fontSmall } }