From 91fbc16ee2b736966b23c08c19fc2ff839302271 Mon Sep 17 00:00:00 2001 From: Pascal Precht Date: Fri, 13 Aug 2021 13:21:42 +0200 Subject: [PATCH] feat(StatusBaseInput): enforce `maximumLength` if it's set Prior to this commit, setting `charLimit` on `StatusInput` would only have a visual effect (rendering the char limit), however it wouldn't actually enforce this limit. This was by intended behaviour, because we wanted to leave some room for possible validators to kick in (for example a max length validator). If however the char limit is enforce, such a validator would never kick in. There seems to be consensus in the team that the limit should be enforced though. This commit enables that. --- .../src/StatusQ/Controls/StatusBaseInput.qml | 20 +++++++++++++++++-- .../src/StatusQ/Controls/StatusInput.qml | 2 ++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/ui/StatusQ/src/StatusQ/Controls/StatusBaseInput.qml b/ui/StatusQ/src/StatusQ/Controls/StatusBaseInput.qml index e35e7fe287..c53aff8167 100644 --- a/ui/StatusQ/src/StatusQ/Controls/StatusBaseInput.qml +++ b/ui/StatusQ/src/StatusQ/Controls/StatusBaseInput.qml @@ -39,6 +39,7 @@ Item { property real minimumHeight: 0 property real maximumHeight: 0 + property int maximumLength: 0 property bool valid: true @@ -128,17 +129,17 @@ Item { TextEdit { id: edit + property string previousText: text + width: flick.width selectByMouse: true selectionColor: Theme.palette.primaryColor2 selectedTextColor: color anchors.verticalCenter: parent.verticalCenter focus: true - font.pixelSize: 15 font.family: Theme.palette.baseFont.name color: Theme.palette.directColor1 - onCursorRectangleChanged: flick.ensureVisible(cursorRectangle) wrapMode: TextEdit.NoWrap @@ -158,6 +159,21 @@ Item { } } + onTextChanged: { + if (statusBaseInput.maximumLength > 0) { + if (text.length > statusBaseInput.maximumLength) { + var cursor = cursorPosition; + text = previousText; + if (cursor > text.length) { + cursorPosition = text.length; + } else { + cursorPosition = cursor-1; + } + } + previousText = text + } + } + StatusBaseText { id: placeholder visible: edit.text.length === 0 diff --git a/ui/StatusQ/src/StatusQ/Controls/StatusInput.qml b/ui/StatusQ/src/StatusQ/Controls/StatusInput.qml index 0688ee66f3..0b06ed211f 100644 --- a/ui/StatusQ/src/StatusQ/Controls/StatusInput.qml +++ b/ui/StatusQ/src/StatusQ/Controls/StatusInput.qml @@ -66,6 +66,8 @@ Item { anchors.topMargin: charLimitLabel.visible ? 11 : 8 anchors.leftMargin: 16 anchors.rightMargin: 16 + + maximumLength: root.charLimit } StatusBaseText {