From e136957d3607a4c76a4dc3e717cee081c3dcba63 Mon Sep 17 00:00:00 2001 From: Vaibhav2002 Date: Sat, 18 Nov 2023 21:23:28 +0530 Subject: [PATCH] feat: Add MaxLength validation --- .../mohamedrejeb/richeditor/model/RichTextConfig.kt | 1 + .../com/mohamedrejeb/richeditor/model/RichTextState.kt | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/model/RichTextConfig.kt b/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/model/RichTextConfig.kt index 2c6dc38c..5f70f053 100644 --- a/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/model/RichTextConfig.kt +++ b/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/model/RichTextConfig.kt @@ -4,6 +4,7 @@ import androidx.compose.ui.graphics.Color import androidx.compose.ui.text.style.TextDecoration internal data class RichTextConfig( + val maxLength: Int? = null, val linkColor: Color = Color.Blue, val linkTextDecoration: TextDecoration = TextDecoration.Underline, val codeColor: Color = Color.Unspecified, diff --git a/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/model/RichTextState.kt b/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/model/RichTextState.kt index e7460943..8cc94be9 100644 --- a/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/model/RichTextState.kt +++ b/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/model/RichTextState.kt @@ -139,6 +139,7 @@ class RichTextState internal constructor( @ExperimentalRichTextApi fun setConfig( + maxLength: Int? = null, linkColor: Color = Color.Unspecified, linkTextDecoration: TextDecoration? = null, codeColor: Color = Color.Unspecified, @@ -146,6 +147,7 @@ class RichTextState internal constructor( codeStrokeColor: Color = Color.Unspecified, ) { richTextConfig = RichTextConfig( + maxLength = maxLength, linkColor = if (linkColor.isSpecified) linkColor else richTextConfig.linkColor, linkTextDecoration = if (linkTextDecoration != null) linkTextDecoration else richTextConfig.linkTextDecoration, codeColor = if (codeColor.isSpecified) codeColor else richTextConfig.codeColor, @@ -519,6 +521,8 @@ class RichTextState internal constructor( internal fun onTextFieldValueChange(newTextFieldValue: TextFieldValue) { if (readOnly) return + if(isExceedingMaxLength(newTextFieldValue)) return + tempTextFieldValue = newTextFieldValue if (tempTextFieldValue.text.length > textFieldValue.text.length) @@ -548,6 +552,8 @@ class RichTextState internal constructor( private fun updateTextFieldValue(newTextFieldValue: TextFieldValue = tempTextFieldValue) { tempTextFieldValue = newTextFieldValue + if(isExceedingMaxLength(newTextFieldValue)) return + if (!singleParagraphMode) { // Check for paragraphs checkForParagraphs() @@ -2172,6 +2178,10 @@ class RichTextState internal constructor( updateTextFieldValue(TextFieldValue()) } + fun isExceedingMaxLength(value: TextFieldValue) = richTextConfig.maxLength?.let { + value.text.length > it + } ?: false + companion object { val Saver: Saver = listSaver( save = {