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.
This commit is contained in:
Pascal Precht 2021-08-13 13:21:42 +02:00 committed by Pascal Precht
parent 58a3071626
commit f635bad63c
2 changed files with 20 additions and 2 deletions

View File

@ -39,6 +39,7 @@ Item {
property real minimumHeight: 0 property real minimumHeight: 0
property real maximumHeight: 0 property real maximumHeight: 0
property int maximumLength: 0
property bool valid: true property bool valid: true
@ -128,17 +129,17 @@ Item {
TextEdit { TextEdit {
id: edit id: edit
property string previousText: text
width: flick.width width: flick.width
selectByMouse: true selectByMouse: true
selectionColor: Theme.palette.primaryColor2 selectionColor: Theme.palette.primaryColor2
selectedTextColor: color selectedTextColor: color
anchors.verticalCenter: parent.verticalCenter anchors.verticalCenter: parent.verticalCenter
focus: true focus: true
font.pixelSize: 15 font.pixelSize: 15
font.family: Theme.palette.baseFont.name font.family: Theme.palette.baseFont.name
color: Theme.palette.directColor1 color: Theme.palette.directColor1
onCursorRectangleChanged: flick.ensureVisible(cursorRectangle) onCursorRectangleChanged: flick.ensureVisible(cursorRectangle)
wrapMode: TextEdit.NoWrap 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 { StatusBaseText {
id: placeholder id: placeholder
visible: edit.text.length === 0 visible: edit.text.length === 0

View File

@ -66,6 +66,8 @@ Item {
anchors.topMargin: charLimitLabel.visible ? 11 : 8 anchors.topMargin: charLimitLabel.visible ? 11 : 8
anchors.leftMargin: 16 anchors.leftMargin: 16
anchors.rightMargin: 16 anchors.rightMargin: 16
maximumLength: root.charLimit
} }
StatusBaseText { StatusBaseText {