Fluid 1.3.0
Material Design for QtQuick and Qml
Loading...
Searching...
No Matches
TextField.qml
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Pier Luigi Fiorini <pierluigi.fiorini@gmail.com>
2// SPDX-License-Identifier: MPL-2.0
3
4pragma ComponentBehavior: Bound
5
6import Fluid as MD
7import Fluid.Private as P
8import "../internal/MotionAnimation.js" as MotionAnimation
9import QtQuick
10import QtQuick.Templates as T
11
40T.TextField {
41 id: control
42 objectName: "textField"
43
45 enum FieldStyle {
47 Filled = 0,
49 Outlined = 1
50 }
51
53 property int fieldStyle: MD.TextField.Filled
54
61 readonly property bool mirrored: LayoutMirroring.enabled
62 || Qt.locale().textDirection === Qt.RightToLeft
63
65 property string label: ""
66
68 property string supportingText: ""
69
71 property string errorText: ""
72
74 property bool error: false
75
77 property string prefixText: ""
78
80 property string suffixText: ""
81
89 property Component leading: null
90
98 property Component trailing: null
99
101 readonly property color leadingContentColor: !enabled
102 ? control.MD.Style.onSurfaceColor
103 : control.MD.Style.onSurfaceVariantColor
104
106 readonly property color trailingContentColor: !enabled
107 ? control.MD.Style.onSurfaceColor
108 : error && hovered
109 ? control.MD.Style.onErrorContainerColor
110 : error
111 ? control.MD.Style.errorColor
112 : control.MD.Style.onSurfaceVariantColor
113
115 readonly property bool _filledField: fieldStyle === MD.TextField.Filled
117 readonly property bool _outlinedField: fieldStyle === MD.TextField.Outlined
119 readonly property bool _fieldActive: enabled && activeFocus
121 readonly property real _supportingAreaHeight: decoration.supportingAreaHeight
123 readonly property real _leadingBaseReservation: leading !== null
124 ? Math.max(
125 MD.Tokens.textField.iconTargetSize,
126 decoration.leadingImplicitWidth)
127 : MD.Tokens.textField.horizontalPadding
129 readonly property real _trailingBaseReservation: trailing !== null
130 ? Math.max(
131 MD.Tokens.textField.iconTargetSize,
132 decoration.trailingImplicitWidth)
133 : MD.Tokens.textField.horizontalPadding
135 readonly property real _leadingReservation: _leadingBaseReservation
136 + (prefixText.length > 0
137 ? decoration.prefixImplicitWidth
138 + MD.Tokens.textField.prefixSuffixTextSpace
139 : 0)
141 readonly property real _trailingReservation: _trailingBaseReservation
142 + (suffixText.length > 0
143 ? decoration.suffixImplicitWidth
144 + MD.Tokens.textField.prefixSuffixTextSpace
145 : 0)
147 readonly property real _inputCenterY: MD.Tokens.textField.containerHeight / 2
148 + (_filledField
149 ? MD.Tokens.typescale.bodySmall.lineHeight / 2
150 * decoration.labelProgress
151 : 0)
153 readonly property color _labelColor: !enabled
154 ? control.MD.Style.onSurfaceColor
155 : error && hovered
156 ? control.MD.Style.onErrorContainerColor
157 : error
158 ? control.MD.Style.errorColor
159 : _fieldActive
160 ? control.MD.Style.primaryColor
161 : hovered && _outlinedField
162 ? control.MD.Style.onSurfaceColor
163 : control.MD.Style.onSurfaceVariantColor
165 readonly property color _indicatorColor: !enabled
166 ? control.MD.Style.onSurfaceColor
167 : error && hovered
168 ? control.MD.Style.onErrorContainerColor
169 : error
170 ? control.MD.Style.errorColor
171 : _fieldActive
172 ? control.MD.Style.primaryColor
173 : hovered
174 ? control.MD.Style.onSurfaceColor
175 : _outlinedField
176 ? control.MD.Style.outlineColor
177 : control.MD.Style.onSurfaceVariantColor
179 readonly property color _supportingColor: !enabled
180 ? control.MD.Style.onSurfaceColor
181 : error
182 ? control.MD.Style.errorColor
183 : control.MD.Style.onSurfaceVariantColor
184
185 implicitWidth: MD.Tokens.textField.minimumWidth
186 implicitHeight: MD.Tokens.textField.containerHeight + _supportingAreaHeight
187 leftPadding: mirrored ? _trailingReservation : _leadingReservation
188 rightPadding: mirrored ? _leadingReservation : _trailingReservation
189 topPadding: _filledField
190 ? (MD.Tokens.typescale.bodySmall.lineHeight
191 + MD.Tokens.textField.contentVerticalPadding)
192 * decoration.labelProgress
193 : 0
194 bottomPadding: _supportingAreaHeight
195 hoverEnabled: true
196 focusPolicy: Qt.StrongFocus
197 selectByMouse: true
198 horizontalAlignment: mirrored ? Text.AlignRight : Text.AlignLeft
199 verticalAlignment: TextInput.AlignVCenter
200
201 color: enabled
202 ? control.MD.Style.onSurfaceColor
203 : MD.Color.transparent(control.MD.Style.onSurfaceColor,
204 MD.Tokens.textField.disabledContentOpacity)
205 selectionColor: control.MD.Style.primaryColor
206 selectedTextColor: control.MD.Style.onPrimaryColor
207 placeholderTextColor: "transparent"
208 font.family: control.MD.Style.plainFontFamily
209 font.pixelSize: MD.Tokens.typescale.bodyLarge.fontSize
210 font.weight: MD.Tokens.typescale.bodyLarge.fontWeight
211 font.letterSpacing: MD.Tokens.typescale.bodyLarge.tracking
212
213 Accessible.name: label.length > 0 ? label : placeholderText
214 Accessible.description: error && errorText.length > 0 ? errorText : supportingText
215 Accessible.role: Accessible.EditableText
216 Accessible.editable: enabled && !readOnly
217 Accessible.readOnly: readOnly
218 Accessible.passwordEdit: echoMode !== TextInput.Normal
219
220 background: Loader {
221 sourceComponent: decoration.backgroundComponent
222 }
223
224 P.FieldDecoration {
225 id: decoration
226 objectName: "textFieldDecoration"
227 anchors.fill: parent
228
229 containerObjectName: "textFieldContainer"
230 activeIndicatorObjectName: "textFieldActiveIndicator"
231 labelNotchObjectName: "textFieldLabelNotch"
232 labelObjectName: "textFieldLabel"
233 placeholderObjectName: "textFieldPlaceholder"
234 prefixObjectName: "textFieldPrefix"
235 suffixObjectName: "textFieldSuffix"
236 leadingSlotObjectName: "textFieldLeadingSlot"
237 trailingSlotObjectName: "textFieldTrailingSlot"
238 supportingObjectName: "textFieldSupportingText"
239 errorObjectName: "textFieldErrorText"
240 mirrored: control.mirrored
241 outlined: control._outlinedField
242 fieldFocused: control.activeFocus
243 fieldEnabled: control.enabled
244 error: control.error
245 containerTransitionsEnabled: true
246 labelTransitionsEnabled: true
247 indicatorTransitionsEnabled: control.enabled
248 applySlotDisabledOpacity: true
249 backgroundImplicitWidth: MD.Tokens.textField.minimumWidth
250 containerHeight: MD.Tokens.textField.containerHeight
251 horizontalPadding: MD.Tokens.textField.horizontalPadding
252 contentVerticalPadding: MD.Tokens.textField.contentVerticalPadding
253 supportingTextTopSpace: MD.Tokens.textField.supportingTextTopSpace
254 supportingTextMinimumHeight: MD.Tokens.textField.supportingTextMinimumHeight
255 outlinedLabelHorizontalPadding: MD.Tokens.textField.outlinedLabelHorizontalPadding
256 disabledContentOpacity: MD.Tokens.textField.disabledContentOpacity
257 leadingSlotMinimumSize: MD.Tokens.textField.iconTargetSize
258 trailingSlotMinimumSize: MD.Tokens.textField.iconTargetSize
259 inputCenterY: control._inputCenterY
260 inputBaselineY: control.baselineOffset
261 alignInputDecorationsToBaseline: true
262 leadingReservation: control._leadingBaseReservation
263 trailingReservation: control._trailingBaseReservation
264 inputLeftPadding: control.leftPadding
265 inputRightPadding: control.rightPadding
266 labelLeadingPosition: control._leadingBaseReservation
267 labelTrailingPosition: control._trailingBaseReservation
268 fullShapeValue: MD.Tokens.shape.cornerValueFull
269 labelText: control.label
270 placeholderText: control.placeholderText
271 inputText: control.text
272 prefixText: control.prefixText
273 suffixText: control.suffixText
274 supportingText: control.supportingText
275 errorText: control.errorText
276 containerColor: control._filledField
277 ? control.enabled
278 ? control.MD.Style.surfaceContainerHighestColor
279 : MD.Color.transparent(
280 control.MD.Style.onSurfaceColor,
281 MD.Tokens.textField.filledDisabledContainerOpacity)
282 : "transparent"
283 outlineColor: control.enabled
284 ? control._indicatorColor
285 : MD.Color.transparent(
286 control.MD.Style.onSurfaceColor,
287 MD.Tokens.textField.outlinedDisabledOutlineOpacity)
288 indicatorColor: control._indicatorColor
289 labelColor: control._labelColor
290 inputDecorationColor: control.enabled
291 ? control.MD.Style.onSurfaceVariantColor
292 : control.MD.Style.onSurfaceColor
293 supportingColor: control._supportingColor
294 notchColor: control.MD.Style.surfaceColor
295 containerShape: control._filledField
296 ? MD.Tokens.textField.filledContainerShape
297 : MD.Tokens.textField.outlinedContainerShape
298 outlineWidth: control._outlinedField
299 ? !control.enabled
300 ? MD.Tokens.textField.outlinedDisabledOutlineWidth
301 : control._fieldActive
302 ? MD.Tokens.textField.outlinedFocusOutlineWidth
303 : control.hovered
304 ? MD.Tokens.textField.outlinedHoverOutlineWidth
305 : MD.Tokens.textField.outlinedOutlineWidth
306 : 0
307 activeIndicatorVisible: control._filledField
308 activeIndicatorHeight: !control.enabled
309 ? MD.Tokens.textField.filledDisabledActiveIndicatorHeight
310 : control._fieldActive
311 ? MD.Tokens.textField.filledFocusActiveIndicatorHeight
312 : control.hovered
313 ? MD.Tokens.textField.filledHoverActiveIndicatorHeight
314 : MD.Tokens.textField.filledActiveIndicatorHeight
315 activeIndicatorOpacity: control.enabled
316 ? 1
317 : MD.Tokens.textField.filledDisabledActiveIndicatorOpacity
318 fontFamily: control.MD.Style.plainFontFamily
319 bodyLargeTypeScale: MD.Tokens.typescale.bodyLarge
320 bodySmallTypeScale: MD.Tokens.typescale.bodySmall
321 supportingWrapMode: Text.Wrap
322 supportingElide: Text.ElideNone
323 effectsDuration: MotionAnimation.expressiveFastEffectsDuration
324 slowEffectsDuration: MotionAnimation.expressiveSlowEffectsDuration
325 spatialDuration: MotionAnimation.expressiveFastSpatialDuration
326 effectsCurve: MotionAnimation.expressiveFastEffectsCurve
327 slowEffectsCurve: MotionAnimation.expressiveSlowEffectsCurve
328 spatialCurve: MotionAnimation.expressiveFastSpatialCurve
329 leading: control.leading
330 trailing: control.trailing
331 }
332}
Utility functions for colors.
Definition color.h:16
Material Design 3 design tokens.
Definition tokens.h:68