Kirigami-addons

FormTextFieldDelegate.qml
1/*
2 * Copyright 2022 Carl Schwan <carl@carlschwan.eu>
3 * SPDX-License-Identifier: LGPL-2.0-or-later
4 */
5
6import QtQuick
7import QtQuick.Controls
8import QtQuick.Layouts
9
10import org.kde.kirigami as Kirigami
11
12/**
13 * @brief A Form delegate that corresponds to a text field.
14 *
15 * ```qml
16 *
17 * FormCard.FormHeader {
18 * title: "Information"
19 * }
20 *
21 * FormCard.FormCard {
22 * FormCard.FormTextFieldDelegate {
23 * label: "Account name"
24 * }
25 *
26 * FormCard.FormTextFieldDelegate {
27 * label: "Password"
28 * statusMessage: "Password incorrect"
29 * status: Kirigami.MessageType.Error
30 * echoMode: TextInput.Password
31 * text: "666666666"
32 * }
33 *
34 * FormCard.FormTextFieldDelegate {
35 * label: "Password"
36 * statusMessage: "Password match"
37 * text: "4242424242"
38 * status: Kirigami.MessageType.Positive
39 * echoMode: TextInput.Password
40 * }
41 * }
42 * ```
43 *
44 * @since KirigamiAddons 0.11.0
45 *
46 * @inherit AbstractFormDelegate
47 */
48AbstractFormDelegate {
49 id: root
50
51 /**
52 * @brief A label containing primary text that appears above and
53 * to the left the text field.
54 */
55 required property string label
56
57 /**
58 * @brief set the maximum length of the text inside the TextField if maxLength > 0
59 */
60 property alias maximumLength: textField.maximumLength
61
62 /**
63 * @brief This hold the activeFocus state of the internal TextField.
64 */
65 property alias fieldActiveFocus: textField.activeFocus
66
67 /**
68 * @brief This hold the `readOnly` state of the internal TextField.
69 */
70 property alias readOnly: textField.readOnly
71
72 /**
73 * @brief This property holds the `echoMode` of the internal TextField.
74 *
75 * This consists of how the text inside the text field will be
76 * displayed to the user.
77 *
78 * @see <a href="https://doc.qt.io/qt-6/qml-qtquick-textinput.html#echoMode-prop">TextInput.echoMode</a>
79 */
80 property alias echoMode: textField.echoMode
81
82 /**
83 * @brief This property holds the `inputMethodHints` of the
84 * internal TextField.
85 *
86 * This consists of hints on the expected content or behavior of
87 * the text field, be it sensitive data, in a date format, or whether
88 * the characters will be hidden, for example.
89 *
90 * @see <a href="https://doc.qt.io/qt-6/qml-qtquick-textinput.html#inputMethodHints-prop">TextInput.inputMethodHints</a>
91 */
92 property alias inputMethodHints: textField.inputMethodHints
93
94 /**
95 * @brief This property holds the `placeholderText` of the
96 * internal TextField.
97 *
98 * This consists of secondary text shown by default on the text field
99 * if no text has been written in it.
100 */
101 property alias placeholderText: textField.placeholderText
102
103 /**
104 * @brief This property holds the `validator` of the internal TextField.
105 */
106 property alias validator: textField.validator
107
108 /**
109 * @brief This property holds the `acceptableInput` of the internal TextField.
110 */
111 property alias acceptableInput: textField.acceptableInput
112
113 /**
114 * @brief This property holds the current status message type of
115 * the text field.
116 *
117 * This consists of an inline message with a colorful background
118 * and an appropriate icon.
119 *
120 * The status property will affect the color of ::statusMessage used.
121 *
122 * Accepted values:
123 * - `Kirigami.MessageType.Information` (blue color)
124 * - `Kirigami.MessageType.Positive` (green color)
125 * - `Kirigami.MessageType.Warning` (orange color)
126 * - `Kirigami.MessageType.Error` (red color)
128 * default: `Kirigami.MessageType.Information` if ::statusMessage is set,
129 * nothing otherwise.
130 *
131 * @see Kirigami.MessageType
132 */
133 property var status: Kirigami.MessageType.Information
134
135 /**
136 * @brief This property holds the current status message of
137 * the text field.
138 *
139 * If this property is not set, no ::status will be shown.
140 */
141 property string statusMessage: ""
142
143 /**
144 * @This signal is emitted when the Return or Enter key is pressed.
146 * Note that if there is a validator or inputMask set on the text input,
147 * the signal will only be emitted if the input is in an acceptable
148 * state.
149 */
150 signal accepted();
151
152 /**
153 * @brief This signal is emitted when the Return or Enter key is pressed
154 * or the text input loses focus.
155 *
156 * Note that if there is a validator or inputMask set on the text input
157 * and enter/return is pressed, this signal will only be emitted if
158 * the input follows the inputMask and the validator returns an
159 * acceptable state.
160 */
161 signal editingFinished();
163 /**
164 * @brief This signal is emitted whenever the text is edited.
165 *
166 * Unlike textChanged(), this signal is not emitted when the text
167 * is changed programmatically, for example, by changing the
168 * value of the text property or by calling ::clear().
169 */
170 signal textEdited();
171
172 /**
173 * @brief Clears the contents of the text input and resets partial
174 * text input from an input method.
175 */
176 function clear() {
177 textField.clear();
178 }
179
180 /**
181 * Inserts text into the TextInput at position.
182 */
183 function insert(position: int, text: string) {
184 textField.insert(position, text);
185 }
186
187 onActiveFocusChanged: { // propagate focus to the text field
188 if (activeFocus) {
189 textField.forceActiveFocus();
190 }
191 }
192
193 onClicked: textField.forceActiveFocus()
194 background: null
195 Accessible.role: Accessible.EditableText
196
197 contentItem: ColumnLayout {
198 spacing: Kirigami.Units.smallSpacing
199 RowLayout {
200 spacing: Kirigami.Units.largeSpacing
201 Label {
202 Layout.fillWidth: true
203 text: label
204 elide: Text.ElideRight
205 color: root.enabled ? Kirigami.Theme.textColor : Kirigami.Theme.disabledTextColor
206 wrapMode: Text.Wrap
207 maximumLineCount: 2
208 Accessible.ignored: true
209 }
210 Label {
211 TextMetrics {
212 id: metrics
213 text: label(root.maximumLength, root.maximumLength)
214 font: Kirigami.Theme.smallFont
215
216 function label(current: int, maximum: int): string {
217 return i18nc("@label %1 is current text length, %2 is maximum length of text field", "%1/%2", current, maximum)
218 }
219 }
220 // 32767 is the default value for TextField.maximumLength
221 visible: root.maximumLength < 32767
222 text: metrics.label(textField.text.length, root.maximumLength)
223 font: Kirigami.Theme.smallFont
224 color: textField.text.length === root.maximumLength
225 ? Kirigami.Theme.neutralTextColor
226 : Kirigami.Theme.textColor
227 horizontalAlignment: Text.AlignRight
228
229 Layout.margins: Kirigami.Units.smallSpacing
230 Layout.preferredWidth: metrics.advanceWidth
231 }
232 }
233 TextField {
234 id: textField
235 Accessible.name: root.label
236 Layout.fillWidth: true
237 placeholderText: root.placeholderText
238 text: root.text
239 onTextChanged: root.text = text
240 onAccepted: root.accepted()
241 onEditingFinished: root.editingFinished()
242 onTextEdited: root.textEdited()
243 activeFocusOnTab: false
244 }
245
246 Kirigami.InlineMessage {
247 id: formErrorHandler
248 visible: root.statusMessage.length > 0
249 Layout.topMargin: visible ? Kirigami.Units.smallSpacing : 0
250 Layout.fillWidth: true
251 text: root.statusMessage
252 type: root.status
253 }
254 }
255}
256
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 3 2024 11:46:57 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.