@momo-kits/native-kits 0.162.36-debug → 0.162.37-debug
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/compose/build.gradle.kts +1 -1
- package/compose/compose.podspec +1 -1
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Information.kt +57 -2
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputMoney.kt +84 -34
- package/gradle.properties +1 -1
- package/ios/Information/Information.swift +5 -2
- package/ios/InputMoney/InputMoney.swift +76 -21
- package/ios/Typography/Text.swift +41 -1
- package/ios/native-kits.podspec +1 -1
- package/ios-demo/MoMoUIKitsDemo.podspec +1 -1
- package/ios-demo/Sources/MoMoUIKitsDemo/SampleDemos/InformationDemo.swift +9 -1
- package/package.json +1 -1
package/compose/build.gradle.kts
CHANGED
package/compose/compose.podspec
CHANGED
|
@@ -9,13 +9,20 @@ import androidx.compose.runtime.Composable
|
|
|
9
9
|
import androidx.compose.runtime.remember
|
|
10
10
|
import androidx.compose.ui.Modifier
|
|
11
11
|
import androidx.compose.ui.draw.clip
|
|
12
|
+
import androidx.compose.ui.text.AnnotatedString
|
|
13
|
+
import androidx.compose.ui.text.SpanStyle
|
|
14
|
+
import androidx.compose.ui.text.buildAnnotatedString
|
|
15
|
+
import androidx.compose.ui.text.font.FontSynthesis
|
|
12
16
|
import androidx.compose.ui.text.style.TextAlign
|
|
17
|
+
import androidx.compose.ui.text.withStyle
|
|
13
18
|
import androidx.compose.ui.unit.dp
|
|
14
19
|
import vn.momo.kits.application.IsShowBaseLineDebug
|
|
20
|
+
import vn.momo.kits.const.AppTheme
|
|
15
21
|
import vn.momo.kits.const.Colors
|
|
16
22
|
import vn.momo.kits.const.Radius
|
|
17
23
|
import vn.momo.kits.const.Spacing
|
|
18
24
|
import vn.momo.kits.const.Typography
|
|
25
|
+
import vn.momo.kits.const.getFontFamily
|
|
19
26
|
import vn.momo.kits.modifier.conditional
|
|
20
27
|
import vn.momo.kits.modifier.setAutomationId
|
|
21
28
|
|
|
@@ -37,6 +44,7 @@ fun Information(
|
|
|
37
44
|
showIconClose: Boolean = true,
|
|
38
45
|
onPressClose: (() -> Unit)? = null,
|
|
39
46
|
accessibilityLabel: String? = null,
|
|
47
|
+
highlightText: List<String> = emptyList(),
|
|
40
48
|
) {
|
|
41
49
|
val automationId = remember(accessibilityLabel, title) {
|
|
42
50
|
accessibilityLabel ?: "Information${if (title != null) "/$title" else ""}"
|
|
@@ -61,6 +69,22 @@ fun Information(
|
|
|
61
69
|
iconSource = "24_notifications_alert_octagon"
|
|
62
70
|
}
|
|
63
71
|
|
|
72
|
+
// Host fonts are one file per weight: swap fontFamily and disable synthesis so the SemiBold file isn't
|
|
73
|
+
// faux-bolded again; fontWeight still applies when no host font is bundled and the system font is used.
|
|
74
|
+
val highlightWeight = Typography.headerXsSemibold.fontWeight
|
|
75
|
+
val highlightFontFamily = getFontFamily(AppTheme.current.font, highlightWeight)
|
|
76
|
+
val annotatedDescription = remember(description, highlightText, highlightFontFamily) {
|
|
77
|
+
highlightDescription(
|
|
78
|
+
description,
|
|
79
|
+
highlightText,
|
|
80
|
+
SpanStyle(
|
|
81
|
+
fontFamily = highlightFontFamily,
|
|
82
|
+
fontWeight = highlightWeight,
|
|
83
|
+
fontSynthesis = FontSynthesis.None,
|
|
84
|
+
),
|
|
85
|
+
)
|
|
86
|
+
}
|
|
87
|
+
|
|
64
88
|
Column(
|
|
65
89
|
modifier = Modifier.border(
|
|
66
90
|
width = 0.5.dp,
|
|
@@ -100,7 +124,7 @@ fun Information(
|
|
|
100
124
|
)
|
|
101
125
|
}
|
|
102
126
|
Text(
|
|
103
|
-
|
|
127
|
+
annotatedDescription,
|
|
104
128
|
style = Typography.descriptionDefaultRegular,
|
|
105
129
|
accessibilityId = "$automationId|description-text"
|
|
106
130
|
)
|
|
@@ -131,4 +155,35 @@ fun Information(
|
|
|
131
155
|
}
|
|
132
156
|
}
|
|
133
157
|
|
|
134
|
-
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// Whole-word matches only; the longest keyword wins when keywords overlap (RN parity).
|
|
161
|
+
private fun highlightDescription(
|
|
162
|
+
description: String,
|
|
163
|
+
highlightText: List<String>,
|
|
164
|
+
style: SpanStyle,
|
|
165
|
+
): AnnotatedString {
|
|
166
|
+
val keywords = highlightText.filter { it.isNotEmpty() }
|
|
167
|
+
if (keywords.isEmpty()) return AnnotatedString(description)
|
|
168
|
+
val regex = Regex(keywords.sortedByDescending { it.length }.joinToString("|") { Regex.escape(it) })
|
|
169
|
+
fun isWordChar(index: Int) = description.getOrNull(index)?.let { it.isLetterOrDigit() || it == '_' } == true
|
|
170
|
+
|
|
171
|
+
return buildAnnotatedString {
|
|
172
|
+
var lastIndex = 0
|
|
173
|
+
var searchFrom = 0
|
|
174
|
+
while (true) {
|
|
175
|
+
val match = regex.find(description, searchFrom) ?: break
|
|
176
|
+
val start = match.range.first
|
|
177
|
+
val end = match.range.last + 1
|
|
178
|
+
if (isWordChar(start - 1) || isWordChar(end)) {
|
|
179
|
+
searchFrom = start + 1
|
|
180
|
+
continue
|
|
181
|
+
}
|
|
182
|
+
append(description.substring(lastIndex, start))
|
|
183
|
+
withStyle(style) { append(match.value) }
|
|
184
|
+
lastIndex = end
|
|
185
|
+
searchFrom = end
|
|
186
|
+
}
|
|
187
|
+
append(description.substring(lastIndex))
|
|
188
|
+
}
|
|
189
|
+
}
|
|
@@ -30,12 +30,10 @@ import androidx.compose.ui.graphics.Color
|
|
|
30
30
|
import androidx.compose.ui.semantics.contentDescription
|
|
31
31
|
import androidx.compose.ui.semantics.semantics
|
|
32
32
|
import androidx.compose.ui.semantics.testTag
|
|
33
|
-
import androidx.compose.ui.text.
|
|
33
|
+
import androidx.compose.ui.text.TextRange
|
|
34
34
|
import androidx.compose.ui.text.TextStyle
|
|
35
35
|
import androidx.compose.ui.text.input.KeyboardType
|
|
36
|
-
import androidx.compose.ui.text.input.
|
|
37
|
-
import androidx.compose.ui.text.input.TransformedText
|
|
38
|
-
import androidx.compose.ui.text.input.VisualTransformation
|
|
36
|
+
import androidx.compose.ui.text.input.TextFieldValue
|
|
39
37
|
import androidx.compose.ui.unit.dp
|
|
40
38
|
import androidx.compose.ui.unit.sp
|
|
41
39
|
import androidx.compose.ui.zIndex
|
|
@@ -46,34 +44,63 @@ import vn.momo.kits.const.Colors
|
|
|
46
44
|
import vn.momo.kits.const.Typography
|
|
47
45
|
import vn.momo.kits.const.getFontFamily
|
|
48
46
|
import vn.momo.kits.modifier.conditional
|
|
49
|
-
import vn.momo.kits.utils.formatNumberToMoney
|
|
50
47
|
|
|
51
|
-
|
|
52
|
-
override fun filter(text: AnnotatedString): TransformedText {
|
|
53
|
-
if (text.text.isEmpty()) {
|
|
54
|
-
return TransformedText(
|
|
55
|
-
AnnotatedString("0"),
|
|
56
|
-
object : OffsetMapping {
|
|
57
|
-
override fun originalToTransformed(offset: Int): Int = 0
|
|
58
|
-
override fun transformedToOriginal(offset: Int): Int = 0
|
|
59
|
-
}
|
|
60
|
-
)
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
if (text.text == "0") {
|
|
64
|
-
return TransformedText(AnnotatedString("0"), OffsetMapping.Identity)
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
val formattedText = formatNumberToMoney(text.text.toLong())
|
|
48
|
+
private const val MONEY_CURRENCY = "đ"
|
|
68
49
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
50
|
+
private data class MoneyEdit(val start: Int, val end: Int, val inserted: String)
|
|
51
|
+
|
|
52
|
+
private fun digitsOf(text: String) = text.filter { it in '0'..'9' }
|
|
53
|
+
|
|
54
|
+
private fun normalizeDigits(text: String) = digitsOf(text).let { digits ->
|
|
55
|
+
digits.trimStart('0').ifEmpty { digits.take(1) }
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
private fun moneyDisplay(digits: String) =
|
|
59
|
+
if (digits.isEmpty()) "" else digits.reversed().chunked(3).joinToString(".").reversed() + MONEY_CURRENCY
|
|
60
|
+
|
|
61
|
+
// Caret position right after the `digitIndex`-th digit of `display`.
|
|
62
|
+
private fun caretFromDigitIndex(display: String, digitIndex: Int): Int {
|
|
63
|
+
var count = 0
|
|
64
|
+
display.forEachIndexed { index, char ->
|
|
65
|
+
if (char in '0'..'9' && ++count == digitIndex) return index + 1
|
|
76
66
|
}
|
|
67
|
+
return 0
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Finds which range of `old` was replaced to produce `next`, given the post-edit caret.
|
|
71
|
+
private fun detectEdit(old: String, next: String, caret: Int): MoneyEdit {
|
|
72
|
+
val tail = next.length - caret
|
|
73
|
+
if (tail in 0..old.length && next.substring(caret) == old.substring(old.length - tail)) {
|
|
74
|
+
var prefix = 0
|
|
75
|
+
while (prefix < minOf(caret, old.length - tail) && old[prefix] == next[prefix]) prefix++
|
|
76
|
+
return MoneyEdit(prefix, old.length - tail, next.substring(prefix, caret))
|
|
77
|
+
}
|
|
78
|
+
var prefix = 0
|
|
79
|
+
while (prefix < minOf(old.length, next.length) && old[prefix] == next[prefix]) prefix++
|
|
80
|
+
var suffix = 0
|
|
81
|
+
while (suffix < minOf(old.length, next.length) - prefix &&
|
|
82
|
+
old[old.length - 1 - suffix] == next[next.length - 1 - suffix]
|
|
83
|
+
) suffix++
|
|
84
|
+
return MoneyEdit(prefix, old.length - suffix, next.substring(prefix, next.length - suffix))
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Applies `edit` to the digits of `display`. Separators and currency are never
|
|
88
|
+
// deleted: removing a separator deletes the digit before it instead, removing
|
|
89
|
+
// only the currency leaves the digits untouched.
|
|
90
|
+
// Returns the new digits and the caret as a digit index.
|
|
91
|
+
private fun applyEdit(display: String, edit: MoneyEdit): Pair<String, Int> {
|
|
92
|
+
val digits = digitsOf(display)
|
|
93
|
+
var left = digitsOf(display.substring(0, edit.start)).length
|
|
94
|
+
var removed = digitsOf(display.substring(edit.start, edit.end)).length
|
|
95
|
+
val inserted = digitsOf(edit.inserted)
|
|
96
|
+
if (removed == 0 && inserted.isEmpty() && left > 0 && '.' in display.substring(edit.start, edit.end)) {
|
|
97
|
+
left -= 1
|
|
98
|
+
removed = 1
|
|
99
|
+
}
|
|
100
|
+
if (removed == 0 && inserted.isEmpty()) return digits to left
|
|
101
|
+
val next = digits.substring(0, left) + inserted + digits.substring(left + removed)
|
|
102
|
+
val trimmed = next.trimStart('0').ifEmpty { next.take(1) }
|
|
103
|
+
return trimmed to maxOf(0, left + inserted.length - (next.length - trimmed.length))
|
|
77
104
|
}
|
|
78
105
|
|
|
79
106
|
@Composable
|
|
@@ -102,6 +129,11 @@ fun InputMoney(
|
|
|
102
129
|
keyboardType: KeyboardType = KeyboardType.Number,
|
|
103
130
|
) {
|
|
104
131
|
var isFocused by remember { mutableStateOf(false) }
|
|
132
|
+
var field by remember { mutableStateOf(TextFieldValue()) }
|
|
133
|
+
val digits = normalizeDigits(text.value)
|
|
134
|
+
val current = if (digitsOf(field.text) == digits) field else moneyDisplay(digits).let {
|
|
135
|
+
TextFieldValue(it, TextRange(caretFromDigitIndex(it, digits.length)))
|
|
136
|
+
}
|
|
105
137
|
var isBlurred = false
|
|
106
138
|
val (textColor, placeholderColor, iconTintColor, floatingTitleColor, floatingIconTintColor) =
|
|
107
139
|
getInputColors(disabled, iconColor, floatingValueColor, floatingIconColor)
|
|
@@ -118,7 +150,7 @@ fun InputMoney(
|
|
|
118
150
|
BasicTextField(
|
|
119
151
|
enabled = !disabled,
|
|
120
152
|
singleLine = true,
|
|
121
|
-
value =
|
|
153
|
+
value = current,
|
|
122
154
|
textStyle = TextStyle(
|
|
123
155
|
color = textColor,
|
|
124
156
|
fontSize = 20.sp,
|
|
@@ -126,7 +158,6 @@ fun InputMoney(
|
|
|
126
158
|
fontWeight = fontWeight.value,
|
|
127
159
|
fontFamily = getFontFamily(AppTheme.current.font, fontWeight.value)
|
|
128
160
|
),
|
|
129
|
-
visualTransformation = CustomConverter(),
|
|
130
161
|
keyboardOptions = KeyboardOptions.Default.copy(keyboardType = keyboardType),
|
|
131
162
|
modifier = Modifier.height(size.values.height).onFocusChanged {
|
|
132
163
|
isFocused = it.isFocused
|
|
@@ -136,9 +167,28 @@ fun InputMoney(
|
|
|
136
167
|
if (!it.isFocused && isBlurred) onBlur()
|
|
137
168
|
if (it.isFocused && !isBlurred) isBlurred = true
|
|
138
169
|
},
|
|
139
|
-
onValueChange = {
|
|
140
|
-
|
|
141
|
-
|
|
170
|
+
onValueChange = { value ->
|
|
171
|
+
if (value.text == current.text) {
|
|
172
|
+
// Keeps the caret out of the currency suffix.
|
|
173
|
+
val max = if (current.text.isEmpty()) 0 else current.text.length - MONEY_CURRENCY.length
|
|
174
|
+
field = value.copy(
|
|
175
|
+
selection = TextRange(
|
|
176
|
+
value.selection.start.coerceAtMost(max),
|
|
177
|
+
value.selection.end.coerceAtMost(max)
|
|
178
|
+
)
|
|
179
|
+
)
|
|
180
|
+
} else {
|
|
181
|
+
val (newDigits, caretDigit) = applyEdit(
|
|
182
|
+
current.text,
|
|
183
|
+
detectEdit(current.text, value.text, value.selection.min)
|
|
184
|
+
)
|
|
185
|
+
val display = moneyDisplay(newDigits)
|
|
186
|
+
field = TextFieldValue(display, TextRange(caretFromDigitIndex(display, caretDigit)))
|
|
187
|
+
if (newDigits != digits) {
|
|
188
|
+
text.value = newDigits
|
|
189
|
+
onChangeText(newDigits)
|
|
190
|
+
}
|
|
191
|
+
}
|
|
142
192
|
},
|
|
143
193
|
decorationBox = { innerTextField ->
|
|
144
194
|
// Floating Icon
|
package/gradle.properties
CHANGED
|
@@ -18,6 +18,7 @@ public struct Information: View {
|
|
|
18
18
|
private let action: String?
|
|
19
19
|
private let showIconClose: Bool
|
|
20
20
|
private let onPressClose: (() -> Void)?
|
|
21
|
+
private let highlightText: [String]
|
|
21
22
|
|
|
22
23
|
public init(
|
|
23
24
|
title: String? = nil,
|
|
@@ -28,7 +29,8 @@ public struct Information: View {
|
|
|
28
29
|
showIcon: Bool = true,
|
|
29
30
|
action: String? = nil,
|
|
30
31
|
showIconClose: Bool = true,
|
|
31
|
-
onPressClose: (() -> Void)? = nil
|
|
32
|
+
onPressClose: (() -> Void)? = nil,
|
|
33
|
+
highlightText: [String] = []
|
|
32
34
|
) {
|
|
33
35
|
self.title = title
|
|
34
36
|
self.description = description
|
|
@@ -39,6 +41,7 @@ public struct Information: View {
|
|
|
39
41
|
self.action = action
|
|
40
42
|
self.showIconClose = showIconClose
|
|
41
43
|
self.onPressClose = onPressClose
|
|
44
|
+
self.highlightText = highlightText
|
|
42
45
|
}
|
|
43
46
|
|
|
44
47
|
private var borderColor: Color {
|
|
@@ -90,7 +93,7 @@ public struct Information: View {
|
|
|
90
93
|
if let title {
|
|
91
94
|
MomoText(title, typography: .labelDefaultMedium)
|
|
92
95
|
}
|
|
93
|
-
MomoText(description, typography: .descriptionDefaultRegular)
|
|
96
|
+
MomoText(description, typography: .descriptionDefaultRegular, highlightText: highlightText)
|
|
94
97
|
}
|
|
95
98
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
96
99
|
|
|
@@ -46,6 +46,42 @@ public func formatNumberToMoney(_ digits: String, currency: String = "đ") -> St
|
|
|
46
46
|
return String(chars.reversed()) + currency
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
+
private func isMoneyDigit(_ char: Character) -> Bool { char.isASCII && char.isNumber }
|
|
50
|
+
|
|
51
|
+
/// Applies an edit of `display[start..<end]` → `inserted` to the digits of `display`.
|
|
52
|
+
/// Separators and currency are never deleted: removing a separator deletes the
|
|
53
|
+
/// digit before it instead, removing only the currency leaves the digits untouched. Returns the new digits and the caret as a digit index.
|
|
54
|
+
private func applyMoneyEdit(_ display: String, start: Int, end: Int, inserted: String) -> (digits: String, caretDigit: Int) {
|
|
55
|
+
let chars = Array(display)
|
|
56
|
+
let start = min(start, chars.count)
|
|
57
|
+
let end = min(max(end, start), chars.count)
|
|
58
|
+
let digits = chars.filter(isMoneyDigit)
|
|
59
|
+
var left = chars[..<start].filter(isMoneyDigit).count
|
|
60
|
+
var removed = chars[start..<end].filter(isMoneyDigit).count
|
|
61
|
+
let insertedDigits = inserted.filter(isMoneyDigit)
|
|
62
|
+
if removed == 0 && insertedDigits.isEmpty && left > 0 && chars[start..<end].contains(".") {
|
|
63
|
+
left -= 1
|
|
64
|
+
removed = 1
|
|
65
|
+
}
|
|
66
|
+
if removed == 0 && insertedDigits.isEmpty {
|
|
67
|
+
return (String(digits), left)
|
|
68
|
+
}
|
|
69
|
+
let next = String(digits[..<left]) + insertedDigits + String(digits[(left + removed)...])
|
|
70
|
+
var trimmed = String(next.drop(while: { $0 == "0" }))
|
|
71
|
+
if trimmed.isEmpty { trimmed = String(next.prefix(1)) }
|
|
72
|
+
return (trimmed, max(0, left + insertedDigits.count - (next.count - trimmed.count)))
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/// Caret offset right after the `digitIndex`-th digit of `display`.
|
|
76
|
+
private func moneyCaret(_ display: String, digitIndex: Int) -> Int {
|
|
77
|
+
var count = 0
|
|
78
|
+
for (index, char) in display.enumerated() where isMoneyDigit(char) {
|
|
79
|
+
count += 1
|
|
80
|
+
if count == digitIndex { return index + 1 }
|
|
81
|
+
}
|
|
82
|
+
return 0
|
|
83
|
+
}
|
|
84
|
+
|
|
49
85
|
// MARK: - InputMoney
|
|
50
86
|
|
|
51
87
|
public struct InputMoney: View {
|
|
@@ -261,8 +297,8 @@ public struct InputMoney: View {
|
|
|
261
297
|
// MARK: - MoneyTextField (UIViewRepresentable)
|
|
262
298
|
|
|
263
299
|
/// A UITextField that shows live thousands-separated currency while the bound
|
|
264
|
-
/// `digits` string holds only the raw numeric value.
|
|
265
|
-
/// the trailing currency symbol.
|
|
300
|
+
/// `digits` string holds only the raw numeric value. The caret can move freely
|
|
301
|
+
/// but never past the trailing currency symbol; edits apply at the caret.
|
|
266
302
|
private struct MoneyTextField: UIViewRepresentable {
|
|
267
303
|
@Binding var digits: String
|
|
268
304
|
var keyboardType: UIKeyboardType
|
|
@@ -286,11 +322,6 @@ private struct MoneyTextField: UIViewRepresentable {
|
|
|
286
322
|
field.setContentHuggingPriority(.defaultLow, for: .horizontal)
|
|
287
323
|
field.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
|
|
288
324
|
field.text = displayValue(digits)
|
|
289
|
-
field.addTarget(
|
|
290
|
-
context.coordinator,
|
|
291
|
-
action: #selector(Coordinator.editingChanged(_:)),
|
|
292
|
-
for: .editingChanged
|
|
293
|
-
)
|
|
294
325
|
return field
|
|
295
326
|
}
|
|
296
327
|
|
|
@@ -302,12 +333,13 @@ private struct MoneyTextField: UIViewRepresentable {
|
|
|
302
333
|
field.textColor = textColor
|
|
303
334
|
field.tintColor = cursorColor
|
|
304
335
|
let expected = displayValue(digits)
|
|
305
|
-
if
|
|
336
|
+
if field.text != expected {
|
|
306
337
|
field.text = expected
|
|
338
|
+
context.coordinator.setCaret(field, offset: moneyCaret(expected, digitIndex: expected.filter(isMoneyDigit).count))
|
|
307
339
|
}
|
|
308
340
|
}
|
|
309
341
|
|
|
310
|
-
|
|
342
|
+
fileprivate func displayValue(_ raw: String) -> String {
|
|
311
343
|
let numeric = raw.filter { $0.isNumber }
|
|
312
344
|
return numeric.isEmpty ? "" : formatNumberToMoney(numeric)
|
|
313
345
|
}
|
|
@@ -330,21 +362,44 @@ private struct MoneyTextField: UIViewRepresentable {
|
|
|
330
362
|
return true
|
|
331
363
|
}
|
|
332
364
|
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
365
|
+
func textField(
|
|
366
|
+
_ textField: UITextField,
|
|
367
|
+
shouldChangeCharactersIn range: NSRange,
|
|
368
|
+
replacementString string: String
|
|
369
|
+
) -> Bool {
|
|
370
|
+
let (numeric, caretDigit) = applyMoneyEdit(
|
|
371
|
+
textField.text ?? "",
|
|
372
|
+
start: range.location,
|
|
373
|
+
end: range.location + range.length,
|
|
374
|
+
inserted: string
|
|
375
|
+
)
|
|
376
|
+
let formatted = parent.displayValue(numeric)
|
|
377
|
+
textField.text = formatted
|
|
378
|
+
setCaret(textField, offset: moneyCaret(formatted, digitIndex: caretDigit))
|
|
344
379
|
if parent.digits != numeric {
|
|
345
380
|
parent.digits = numeric
|
|
381
|
+
parent.onChangeText?(numeric)
|
|
382
|
+
}
|
|
383
|
+
return false
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
// Keeps the caret out of the currency suffix.
|
|
387
|
+
func textFieldDidChangeSelection(_ textField: UITextField) {
|
|
388
|
+
guard let text = textField.text, !text.isEmpty,
|
|
389
|
+
let range = textField.selectedTextRange,
|
|
390
|
+
let limit = textField.position(from: textField.endOfDocument, offset: -1)
|
|
391
|
+
else { return }
|
|
392
|
+
let start = textField.compare(range.start, to: limit) == .orderedDescending ? limit : range.start
|
|
393
|
+
let end = textField.compare(range.end, to: limit) == .orderedDescending ? limit : range.end
|
|
394
|
+
if start != range.start || end != range.end {
|
|
395
|
+
textField.selectedTextRange = textField.textRange(from: start, to: end)
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
func setCaret(_ textField: UITextField, offset: Int) {
|
|
400
|
+
if let position = textField.position(from: textField.beginningOfDocument, offset: offset) {
|
|
401
|
+
textField.selectedTextRange = textField.textRange(from: position, to: position)
|
|
346
402
|
}
|
|
347
|
-
parent.onChangeText?(numeric)
|
|
348
403
|
}
|
|
349
404
|
}
|
|
350
405
|
}
|
|
@@ -139,6 +139,7 @@ public struct MomoText: View {
|
|
|
139
139
|
private let letterSpacing: CGFloat?
|
|
140
140
|
private let softWrap: Bool
|
|
141
141
|
private let accessibilityId: String?
|
|
142
|
+
private var highlightText: [String] = []
|
|
142
143
|
|
|
143
144
|
public init(
|
|
144
145
|
_ content: String,
|
|
@@ -176,13 +177,52 @@ public struct MomoText: View {
|
|
|
176
177
|
self.accessibilityId = accessibilityId
|
|
177
178
|
}
|
|
178
179
|
|
|
180
|
+
init(_ content: String, typography: TypographyStyle, highlightText: [String]) {
|
|
181
|
+
self.init(content, typography: typography)
|
|
182
|
+
self.highlightText = highlightText
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// Whole-word matches of `highlightText` render semibold; the longest keyword wins (RN parity).
|
|
186
|
+
private var baseText: SwiftUI.Text {
|
|
187
|
+
let keywords = highlightText.filter { !$0.isEmpty }.sorted { $0.count > $1.count }
|
|
188
|
+
guard !keywords.isEmpty else { return SwiftUI.Text(content) }
|
|
189
|
+
let highlightFont = momoFont(size: fontScale(typography.fontSize), weight: TypographyStyle.headerXsSemibold.fontWeight, family: theme.font)
|
|
190
|
+
func isWordChar(_ character: Character?) -> Bool {
|
|
191
|
+
guard let character else { return false }
|
|
192
|
+
return character.isLetter || character.isNumber || character == "_"
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
var result = SwiftUI.Text(verbatim: "")
|
|
196
|
+
var plainStart = content.startIndex
|
|
197
|
+
var index = content.startIndex
|
|
198
|
+
while index < content.endIndex {
|
|
199
|
+
let previous = index > content.startIndex ? content[content.index(before: index)] : nil
|
|
200
|
+
let keyword = isWordChar(previous) ? nil : keywords.first { keyword in
|
|
201
|
+
guard content[index...].hasPrefix(keyword) else { return false }
|
|
202
|
+
let end = content.index(index, offsetBy: keyword.count)
|
|
203
|
+
return !isWordChar(end < content.endIndex ? content[end] : nil)
|
|
204
|
+
}
|
|
205
|
+
guard let keyword else {
|
|
206
|
+
index = content.index(after: index)
|
|
207
|
+
continue
|
|
208
|
+
}
|
|
209
|
+
let end = content.index(index, offsetBy: keyword.count)
|
|
210
|
+
result = result
|
|
211
|
+
+ SwiftUI.Text(verbatim: String(content[plainStart..<index]))
|
|
212
|
+
+ SwiftUI.Text(verbatim: String(content[index..<end])).font(highlightFont)
|
|
213
|
+
plainStart = end
|
|
214
|
+
index = end
|
|
215
|
+
}
|
|
216
|
+
return result + SwiftUI.Text(verbatim: String(content[plainStart...]))
|
|
217
|
+
}
|
|
218
|
+
|
|
179
219
|
public var body: some View {
|
|
180
220
|
let textColor = color ?? theme.colors.text.default
|
|
181
221
|
// Strikethrough is driven either by the typography variant (mirrors Compose
|
|
182
222
|
// *Strikethrough styles) or the explicit `strikethrough` flag.
|
|
183
223
|
let isStrikethrough = strikethrough || typography.isStrikethrough
|
|
184
224
|
|
|
185
|
-
var text =
|
|
225
|
+
var text = baseText
|
|
186
226
|
.font(momoFont(size: fontScale(typography.fontSize), weight: typography.fontWeight, family: theme.font))
|
|
187
227
|
.foregroundColor(textColor)
|
|
188
228
|
.strikethrough(isStrikethrough, color: textColor)
|
package/ios/native-kits.podspec
CHANGED
|
@@ -44,6 +44,10 @@ private struct InformationPreview: View {
|
|
|
44
44
|
Information(title: "Payment failed", description: "We couldn't process your payment.",
|
|
45
45
|
state: .error, onPressAction: {}, action: "Retry")
|
|
46
46
|
}
|
|
47
|
+
BasicColumnBox("Highlight text", alignment: .leading) {
|
|
48
|
+
Information(description: "Bạn được hoàn 50.000đ, áp dụng đến hết 31/12/2026",
|
|
49
|
+
highlightText: ["50.000đ", "31/12/2026"])
|
|
50
|
+
}
|
|
47
51
|
BasicColumnBox("Without icon", alignment: .leading) {
|
|
48
52
|
Information(title: "No icon", description: "showIcon is set to false.", showIcon: false)
|
|
49
53
|
}
|
|
@@ -60,6 +64,7 @@ private struct InformationPlayground: View {
|
|
|
60
64
|
@State private var showIcon = true
|
|
61
65
|
@State private var showIconClose = false
|
|
62
66
|
@State private var action = ""
|
|
67
|
+
@State private var highlightText = "description"
|
|
63
68
|
|
|
64
69
|
var body: some View {
|
|
65
70
|
PlaygroundTab(
|
|
@@ -72,12 +77,15 @@ private struct InformationPlayground: View {
|
|
|
72
77
|
showIcon: showIcon,
|
|
73
78
|
action: action.isEmpty ? nil : action,
|
|
74
79
|
showIconClose: showIconClose,
|
|
75
|
-
onPressClose: {}
|
|
80
|
+
onPressClose: {},
|
|
81
|
+
highlightText: highlightText.components(separatedBy: ",")
|
|
82
|
+
.map { $0.trimmingCharacters(in: .whitespaces) }
|
|
76
83
|
)
|
|
77
84
|
},
|
|
78
85
|
params: [
|
|
79
86
|
.string("Title", $title),
|
|
80
87
|
.string("Description", $description),
|
|
88
|
+
.string("HighlightText: [String] (comma-separated)", $highlightText),
|
|
81
89
|
.radio("State", informationStateList, $state),
|
|
82
90
|
.checkbox("ShowIcon", $showIcon),
|
|
83
91
|
.checkbox("ShowIconClose", $showIconClose),
|