sapo-components-ui-rn 1.0.0

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.
Files changed (215) hide show
  1. package/README.md +69 -0
  2. package/dist/assets/error.png +0 -0
  3. package/dist/assets/icon-checkbox-active.svg +4 -0
  4. package/dist/assets/icon-checkbox.svg +3 -0
  5. package/dist/assets/image_default.png +0 -0
  6. package/dist/assets/images/error.png +0 -0
  7. package/dist/assets/images/image_default.png +0 -0
  8. package/dist/assets/images/info.png +0 -0
  9. package/dist/assets/images/success.png +0 -0
  10. package/dist/assets/images/warning.png +0 -0
  11. package/dist/assets/info.png +0 -0
  12. package/dist/assets/success.png +0 -0
  13. package/dist/assets/svg/icon-checkbox-active.svg +4 -0
  14. package/dist/assets/svg/icon-checkbox.svg +3 -0
  15. package/dist/assets/warning.png +0 -0
  16. package/dist/components/ActivityIndicator.d.ts +28 -0
  17. package/dist/components/Avatar/index.d.ts +17 -0
  18. package/dist/components/Badge/index.d.ts +13 -0
  19. package/dist/components/Button/index.d.ts +23 -0
  20. package/dist/components/ButtonIcon/index.d.ts +18 -0
  21. package/dist/components/Checkbox/index.d.ts +14 -0
  22. package/dist/components/ChipBar/index.d.ts +16 -0
  23. package/dist/components/CountingDot/index.d.ts +13 -0
  24. package/dist/components/FloatingButton/index.d.ts +17 -0
  25. package/dist/components/Icon/index.d.ts +17 -0
  26. package/dist/components/IconSvg/index.d.ts +12 -0
  27. package/dist/components/Image/index.d.ts +14 -0
  28. package/dist/components/ProgressBar.d.ts +42 -0
  29. package/dist/components/RadioButton/index.d.ts +13 -0
  30. package/dist/components/ScaleButton/index.d.ts +4 -0
  31. package/dist/components/SelectionField/index.d.ts +24 -0
  32. package/dist/components/Spacer/index.d.ts +9 -0
  33. package/dist/components/Switch/Switch.d.ts +47 -0
  34. package/dist/components/Switch/utils.d.ts +14 -0
  35. package/dist/components/Text/index.d.ts +14 -0
  36. package/dist/components/TextInput/Addons/Outline.d.ts +15 -0
  37. package/dist/components/TextInput/Addons/Underline.d.ts +19 -0
  38. package/dist/components/TextInput/Adornment/TextInputAdornment.d.ts +43 -0
  39. package/dist/components/TextInput/Adornment/TextInputAffix.d.ts +72 -0
  40. package/dist/components/TextInput/Adornment/enums.d.ts +12 -0
  41. package/dist/components/TextInput/Adornment/types.d.ts +11 -0
  42. package/dist/components/TextInput/Adornment/utils.d.ts +11 -0
  43. package/dist/components/TextInput/Label/InputLabel.d.ts +4 -0
  44. package/dist/components/TextInput/Label/LabelBackground.d.ts +4 -0
  45. package/dist/components/TextInput/TextInput.d.ts +171 -0
  46. package/dist/components/TextInput/TextInputDefault.d.ts +4 -0
  47. package/dist/components/TextInput/TextInputFlat.d.ts +4 -0
  48. package/dist/components/TextInput/constants.d.ts +31 -0
  49. package/dist/components/TextInput/helpers.d.ts +95 -0
  50. package/dist/components/TextInput/types.d.ts +154 -0
  51. package/dist/components/Toast/ToastProvider.d.ts +21 -0
  52. package/dist/components/Toast/index.d.ts +3 -0
  53. package/dist/components/Typography/AnimatedText.d.ts +34 -0
  54. package/dist/components/Typography/StyledText.d.ts +12 -0
  55. package/dist/components/Typography/Text.d.ts +35 -0
  56. package/dist/components/Typography/types.d.ts +2 -0
  57. package/dist/components/View/index.d.ts +38 -0
  58. package/dist/components/ViewVisibleAnimated/index.d.ts +25 -0
  59. package/dist/constants.d.ts +1 -0
  60. package/dist/core/theming.d.ts +43 -0
  61. package/dist/icons/IconArrowDown.d.ts +4 -0
  62. package/dist/icons/IconCheckbox.d.ts +4 -0
  63. package/dist/icons/IconCheckboxActive.d.ts +4 -0
  64. package/dist/icons/IconClearText.d.ts +4 -0
  65. package/dist/icons/IconRadio.d.ts +4 -0
  66. package/dist/icons/IconRadioActive.d.ts +4 -0
  67. package/dist/icons/IconRadioDisable.d.ts +4 -0
  68. package/dist/index.d.ts +33 -0
  69. package/dist/index.esm.js +6758 -0
  70. package/dist/index.esm.js.map +1 -0
  71. package/dist/index.js +6823 -0
  72. package/dist/index.js.map +1 -0
  73. package/dist/styles/fonts.d.ts +78 -0
  74. package/dist/styles/overlay.d.ts +3 -0
  75. package/dist/styles/shadow.d.ts +18 -0
  76. package/dist/styles/themes/DarkTheme.d.ts +2 -0
  77. package/dist/styles/themes/LightTheme.d.ts +2 -0
  78. package/dist/styles/themes/index.d.ts +2 -0
  79. package/dist/styles/themes/tokens.d.ts +830 -0
  80. package/dist/theme/blue.d.ts +14 -0
  81. package/dist/theme/colors.d.ts +99 -0
  82. package/dist/theme/container-styles.d.ts +124 -0
  83. package/dist/theme/dimensions.d.ts +36 -0
  84. package/dist/theme/green.d.ts +15 -0
  85. package/dist/theme/icon.d.ts +5 -0
  86. package/dist/theme/ink.d.ts +11 -0
  87. package/dist/theme/orange.d.ts +14 -0
  88. package/dist/theme/purple.d.ts +6 -0
  89. package/dist/theme/red.d.ts +15 -0
  90. package/dist/theme/themes.d.ts +277 -0
  91. package/dist/theme/white.d.ts +10 -0
  92. package/dist/theme/yellow.d.ts +15 -0
  93. package/dist/types/index.d.ts +65 -0
  94. package/dist/types.d.ts +220 -0
  95. package/dist/utils/BackHandler/BackHandler.d.ts +9 -0
  96. package/dist/utils/BackHandler/BackHandler.native.d.ts +2 -0
  97. package/dist/utils/addEventListener.d.ts +12 -0
  98. package/dist/utils/forwardRef.d.ts +11 -0
  99. package/dist/utils/function-utils.d.ts +12 -0
  100. package/dist/utils/getContrastingColor.d.ts +2 -0
  101. package/dist/utils/hasTouchHandler.d.ts +5 -0
  102. package/dist/utils/roundLayoutSize.d.ts +1 -0
  103. package/dist/utils/splitStyles.d.ts +19 -0
  104. package/dist/utils/toast-manager.d.ts +21 -0
  105. package/dist/utils/useAnimatedValue.d.ts +2 -0
  106. package/dist/utils/useAnimatedValueArray.d.ts +2 -0
  107. package/dist/utils/useIsKeyboardShown.d.ts +6 -0
  108. package/dist/utils/useLayout.d.ts +6 -0
  109. package/dist/utils/useLazyRef.d.ts +2 -0
  110. package/package.json +104 -0
  111. package/src/.DS_Store +0 -0
  112. package/src/assets/images/error.png +0 -0
  113. package/src/assets/images/image_default.png +0 -0
  114. package/src/assets/images/info.png +0 -0
  115. package/src/assets/images/success.png +0 -0
  116. package/src/assets/images/warning.png +0 -0
  117. package/src/assets/svg/icon-checkbox-active.svg +4 -0
  118. package/src/assets/svg/icon-checkbox.svg +3 -0
  119. package/src/babel/.eslintrc +7 -0
  120. package/src/babel/index.js +63 -0
  121. package/src/components/.DS_Store +0 -0
  122. package/src/components/ActivityIndicator.tsx +238 -0
  123. package/src/components/Avatar/index.tsx +102 -0
  124. package/src/components/Badge/index.tsx +55 -0
  125. package/src/components/Button/index.tsx +162 -0
  126. package/src/components/ButtonIcon/index.tsx +98 -0
  127. package/src/components/Checkbox/index.tsx +107 -0
  128. package/src/components/ChipBar/index.tsx +112 -0
  129. package/src/components/CountingDot/index.tsx +63 -0
  130. package/src/components/FloatingButton/index.tsx +99 -0
  131. package/src/components/Icon/index.tsx +135 -0
  132. package/src/components/IconSvg/index.tsx +42 -0
  133. package/src/components/Image/index.tsx +127 -0
  134. package/src/components/ProgressBar.tsx +272 -0
  135. package/src/components/RadioButton/index.tsx +105 -0
  136. package/src/components/ScaleButton/index.tsx +17 -0
  137. package/src/components/SelectionField/index.tsx +166 -0
  138. package/src/components/Spacer/index.tsx +19 -0
  139. package/src/components/Switch/Switch.tsx +110 -0
  140. package/src/components/Switch/utils.ts +96 -0
  141. package/src/components/Text/index.tsx +69 -0
  142. package/src/components/TextInput/.DS_Store +0 -0
  143. package/src/components/TextInput/Addons/Outline.tsx +62 -0
  144. package/src/components/TextInput/Addons/Underline.tsx +71 -0
  145. package/src/components/TextInput/Adornment/TextInputAdornment.tsx +191 -0
  146. package/src/components/TextInput/Adornment/TextInputAffix.tsx +212 -0
  147. package/src/components/TextInput/Adornment/enums.tsx +12 -0
  148. package/src/components/TextInput/Adornment/types.tsx +11 -0
  149. package/src/components/TextInput/Adornment/utils.ts +39 -0
  150. package/src/components/TextInput/Label/InputLabel.tsx +216 -0
  151. package/src/components/TextInput/Label/LabelBackground.tsx +100 -0
  152. package/src/components/TextInput/TextInput.tsx +564 -0
  153. package/src/components/TextInput/TextInputDefault.tsx +187 -0
  154. package/src/components/TextInput/TextInputFlat.tsx +452 -0
  155. package/src/components/TextInput/constants.tsx +48 -0
  156. package/src/components/TextInput/helpers.tsx +546 -0
  157. package/src/components/TextInput/types.tsx +155 -0
  158. package/src/components/Toast/ToastProvider.tsx +69 -0
  159. package/src/components/Toast/index.tsx +239 -0
  160. package/src/components/Typography/AnimatedText.tsx +84 -0
  161. package/src/components/Typography/StyledText.tsx +53 -0
  162. package/src/components/Typography/Text.tsx +189 -0
  163. package/src/components/Typography/types.tsx +5 -0
  164. package/src/components/View/index.tsx +175 -0
  165. package/src/components/ViewVisibleAnimated/index.tsx +199 -0
  166. package/src/constants.tsx +16 -0
  167. package/src/core/theming.tsx +144 -0
  168. package/src/icons/IconArrowDown.tsx +14 -0
  169. package/src/icons/IconCheckbox.tsx +20 -0
  170. package/src/icons/IconCheckboxActive.tsx +18 -0
  171. package/src/icons/IconClearText.tsx +14 -0
  172. package/src/icons/IconRadio.tsx +11 -0
  173. package/src/icons/IconRadioActive.tsx +12 -0
  174. package/src/icons/IconRadioDisable.tsx +12 -0
  175. package/src/index.ts +51 -0
  176. package/src/index.tsx +50 -0
  177. package/src/styles/fonts.tsx +89 -0
  178. package/src/styles/overlay.tsx +69 -0
  179. package/src/styles/shadow.tsx +47 -0
  180. package/src/styles/themes/DarkTheme.tsx +157 -0
  181. package/src/styles/themes/LightTheme.tsx +160 -0
  182. package/src/styles/themes/index.ts +3 -0
  183. package/src/styles/themes/tokens.tsx +481 -0
  184. package/src/theme/blue.ts +25 -0
  185. package/src/theme/colors.ts +21 -0
  186. package/src/theme/container-styles.tsx +126 -0
  187. package/src/theme/dimensions.ts +44 -0
  188. package/src/theme/green.ts +27 -0
  189. package/src/theme/icon.ts +7 -0
  190. package/src/theme/ink.ts +18 -0
  191. package/src/theme/orange.ts +24 -0
  192. package/src/theme/purple.ts +8 -0
  193. package/src/theme/red.ts +26 -0
  194. package/src/theme/themes.tsx +287 -0
  195. package/src/theme/white.ts +16 -0
  196. package/src/theme/yellow.ts +26 -0
  197. package/src/types/index.ts +71 -0
  198. package/src/types/svg.d.ts +5 -0
  199. package/src/types.ts +259 -0
  200. package/src/types.tsx +284 -0
  201. package/src/utils/BackHandler/BackHandler.native.tsx +3 -0
  202. package/src/utils/BackHandler/BackHandler.tsx +11 -0
  203. package/src/utils/addEventListener.tsx +56 -0
  204. package/src/utils/forwardRef.tsx +23 -0
  205. package/src/utils/function-utils.tsx +108 -0
  206. package/src/utils/getContrastingColor.tsx +15 -0
  207. package/src/utils/hasTouchHandler.tsx +23 -0
  208. package/src/utils/roundLayoutSize.ts +2 -0
  209. package/src/utils/splitStyles.ts +60 -0
  210. package/src/utils/toast-manager.tsx +46 -0
  211. package/src/utils/useAnimatedValue.tsx +9 -0
  212. package/src/utils/useAnimatedValueArray.tsx +13 -0
  213. package/src/utils/useIsKeyboardShown.tsx +55 -0
  214. package/src/utils/useLayout.tsx +29 -0
  215. package/src/utils/useLazyRef.tsx +11 -0
@@ -0,0 +1,96 @@
1
+ import { Platform } from "react-native";
2
+
3
+ import type { InternalTheme } from "../../types";
4
+
5
+ type BaseProps = {
6
+ theme: InternalTheme;
7
+ disabled?: boolean;
8
+ value?: boolean;
9
+ };
10
+
11
+ const getCheckedColor = ({
12
+ theme,
13
+ color,
14
+ }: {
15
+ theme: InternalTheme;
16
+ color?: string;
17
+ }) => {
18
+ if (color) {
19
+ return color;
20
+ }
21
+
22
+ return theme.colors.surfaceBrandDefault;
23
+ };
24
+
25
+ const getThumbTintColor = ({
26
+ theme,
27
+ disabled,
28
+ value,
29
+ checkedColor,
30
+ }: BaseProps & { checkedColor: string }) => {
31
+ const isIOS = Platform.OS === "ios";
32
+
33
+ if (isIOS) {
34
+ return undefined;
35
+ }
36
+
37
+ if (disabled) {
38
+ if (theme.dark) {
39
+ return theme.colors.backgroundPrimary;
40
+ }
41
+ return theme.colors.backgroundPrimary;
42
+ }
43
+
44
+ if (value) {
45
+ return checkedColor;
46
+ }
47
+
48
+ if (theme.dark) {
49
+ return theme.colors.backgroundPrimary;
50
+ }
51
+ return theme.colors.backgroundPrimary;
52
+ };
53
+
54
+ const getOnTintColor = ({
55
+ theme,
56
+ disabled,
57
+ value,
58
+ checkedColor,
59
+ }: BaseProps & { checkedColor: string }) => {
60
+ const isIOS = Platform.OS === "ios";
61
+
62
+ if (isIOS) {
63
+ return checkedColor;
64
+ }
65
+
66
+ if (disabled) {
67
+ if (theme.dark) {
68
+ return theme.colors.backgroundPrimary;
69
+ }
70
+ return theme.colors.backgroundPrimary;
71
+ }
72
+
73
+ if (value) {
74
+ return checkedColor;
75
+ }
76
+
77
+ if (theme.dark) {
78
+ return theme.colors.backgroundPrimary;
79
+ }
80
+ return theme.colors.backgroundPrimary;
81
+ };
82
+
83
+ export const getSwitchColor = ({
84
+ theme,
85
+ disabled,
86
+ value,
87
+ color,
88
+ }: BaseProps & { color?: string }) => {
89
+ const checkedColor = getCheckedColor({ theme, color });
90
+
91
+ return {
92
+ onTintColor: getOnTintColor({ theme, disabled, value, checkedColor }),
93
+ thumbTintColor: getThumbTintColor({ theme, disabled, value, checkedColor }),
94
+ checkedColor,
95
+ };
96
+ };
@@ -0,0 +1,69 @@
1
+ import React from "react";
2
+ import { Text as TextRN, TextProps, StyleSheet, Platform } from "react-native";
3
+ import { memoDeepEqual } from "../../utils/function-utils";
4
+ import colors from "../../theme/colors";
5
+ import { useInternalTheme } from "../../core/theming";
6
+ import { ThemeProp } from "../../types";
7
+
8
+ export interface IText extends TextProps {
9
+ size?: number;
10
+ color?: string;
11
+ center?: boolean;
12
+ bold?: boolean;
13
+ medium?: boolean;
14
+ children?: any;
15
+ theme?: ThemeProp;
16
+ }
17
+
18
+ const Text = (props: IText) => {
19
+ const { children, onPress, theme: themeOverrides, ...rest } = props;
20
+ const theme = useInternalTheme();
21
+
22
+ delete rest.style;
23
+ const _getStyle = () => {
24
+ const style: any = {
25
+ fontWeight: "normal",
26
+ color: colors.ink.INK100,
27
+ fontFamily: "Inter-Regular",
28
+ };
29
+ const { size = 14, center, color, bold, medium } = props;
30
+ style.color = theme.colors.textDefault || "black";
31
+ if (size) {
32
+ style.fontSize = size;
33
+ }
34
+ if (color) {
35
+ style.color = color;
36
+ }
37
+ if (center) {
38
+ style.textAlign = "center";
39
+ }
40
+ if (bold) {
41
+ style.fontFamily = "Inter-Bold";
42
+ if (Platform.OS === "ios") {
43
+ style.fontWeight = "bold";
44
+ }
45
+ }
46
+ if (medium) {
47
+ style.fontFamily = "Inter-SemiBold";
48
+ if (Platform.OS === "ios") {
49
+ style.fontWeight = "600";
50
+ }
51
+ }
52
+ return StyleSheet.create({ style }).style;
53
+ };
54
+ const defaultStyle = _getStyle();
55
+ const Component = TextRN;
56
+
57
+ return (
58
+ <Component
59
+ onPress={onPress}
60
+ style={[defaultStyle, props.style]}
61
+ allowFontScaling={false}
62
+ {...rest}
63
+ >
64
+ {children}
65
+ </Component>
66
+ );
67
+ };
68
+
69
+ export default memoDeepEqual(Text);
@@ -0,0 +1,62 @@
1
+ import * as React from "react";
2
+ import {
3
+ StyleSheet,
4
+ ColorValue,
5
+ StyleProp,
6
+ View,
7
+ ViewStyle,
8
+ } from "react-native";
9
+
10
+ import { TextInputLabelProp } from "../types";
11
+
12
+ type OutlineProps = {
13
+ activeColor: string;
14
+ backgroundColor: ColorValue | any;
15
+ hasActiveOutline?: boolean;
16
+ focused?: boolean;
17
+ outlineColor?: string;
18
+ roundness?: number;
19
+ label?: TextInputLabelProp;
20
+ style?: StyleProp<ViewStyle>;
21
+ };
22
+
23
+ export const Outline = ({
24
+ label,
25
+ activeColor,
26
+ backgroundColor,
27
+ hasActiveOutline,
28
+ focused,
29
+ outlineColor = "#EAEBED",
30
+ roundness = 8,
31
+ style,
32
+ }: OutlineProps) => (
33
+ <View
34
+ testID="text-input-outline"
35
+ pointerEvents="none"
36
+ style={[
37
+ styles.outline,
38
+ !label && styles.noLabelOutline,
39
+ // eslint-disable-next-line react-native/no-inline-styles
40
+ {
41
+ // backgroundColor,
42
+ // borderRadius: roundness,
43
+ // borderWidth: 1,
44
+ // borderColor: hasActiveOutline ? activeColor : outlineColor,
45
+ },
46
+ style,
47
+ ]}
48
+ />
49
+ );
50
+
51
+ const styles = StyleSheet.create({
52
+ outline: {
53
+ position: "absolute",
54
+ left: 0,
55
+ right: 0,
56
+ top: 6,
57
+ bottom: 0,
58
+ },
59
+ noLabelOutline: {
60
+ top: 0,
61
+ },
62
+ });
@@ -0,0 +1,71 @@
1
+ import * as React from "react";
2
+ import { Animated, StyleSheet, StyleProp, ViewStyle } from "react-native";
3
+
4
+ import { ThemeProp } from "../../../types";
5
+
6
+ type UnderlineProps = {
7
+ parentState: {
8
+ focused: boolean;
9
+ };
10
+ error?: boolean;
11
+ colors?: {
12
+ error?: string;
13
+ };
14
+ activeColor: string;
15
+ underlineColorCustom?: string;
16
+ hasActiveOutline?: boolean;
17
+ style?: StyleProp<ViewStyle>;
18
+ theme?: ThemeProp;
19
+ };
20
+
21
+ export const Underline = ({
22
+ parentState,
23
+ error,
24
+ colors,
25
+ activeColor,
26
+ underlineColorCustom,
27
+ hasActiveOutline,
28
+ style,
29
+ }: UnderlineProps) => {
30
+ let backgroundColor = parentState.focused
31
+ ? activeColor
32
+ : underlineColorCustom;
33
+
34
+ if (error) backgroundColor = colors?.error;
35
+
36
+ const activeScale = 2;
37
+
38
+ return (
39
+ <Animated.View
40
+ testID="text-input-underline"
41
+ style={[
42
+ styles.underline,
43
+ styles.md3Underline,
44
+ {
45
+ backgroundColor,
46
+ // Underlines is thinner when input is not focused
47
+ transform: [
48
+ {
49
+ scaleY: hasActiveOutline ? activeScale : 0.5,
50
+ },
51
+ ],
52
+ },
53
+ style,
54
+ ]}
55
+ />
56
+ );
57
+ };
58
+
59
+ const styles = StyleSheet.create({
60
+ underline: {
61
+ position: "absolute",
62
+ left: 0,
63
+ right: 0,
64
+ bottom: 0,
65
+ height: 2,
66
+ zIndex: 1,
67
+ },
68
+ md3Underline: {
69
+ height: 1,
70
+ },
71
+ });
@@ -0,0 +1,191 @@
1
+ import React from "react";
2
+ import type {
3
+ LayoutChangeEvent,
4
+ TextStyle,
5
+ StyleProp,
6
+ Animated,
7
+ DimensionValue,
8
+ } from "react-native";
9
+
10
+ import { AdornmentSide, AdornmentType, InputMode } from "./enums";
11
+ import TextInputAffix, { AffixAdornment } from "./TextInputAffix";
12
+ import type {
13
+ AdornmentConfig,
14
+ AdornmentStyleAdjustmentForNativeInput,
15
+ } from "./types";
16
+ import { getConstants } from "../helpers";
17
+ import { ThemeProp } from "../../../types";
18
+
19
+ export function getAdornmentConfig({
20
+ left,
21
+ right,
22
+ }: {
23
+ left?: React.ReactNode;
24
+ right?: React.ReactNode;
25
+ }): Array<AdornmentConfig> {
26
+ let adornmentConfig: any[] = [];
27
+ if (left || right) {
28
+ [
29
+ { side: AdornmentSide.Left, adornment: left },
30
+ { side: AdornmentSide.Right, adornment: right },
31
+ ].forEach(({ side, adornment }) => {
32
+ if (adornment && React.isValidElement(adornment)) {
33
+ let type;
34
+ if (adornment.type === TextInputAffix) {
35
+ type = AdornmentType.Affix;
36
+ }
37
+ adornmentConfig.push({
38
+ side,
39
+ type,
40
+ });
41
+ }
42
+ });
43
+ }
44
+
45
+ return adornmentConfig;
46
+ }
47
+
48
+ export function getAdornmentStyleAdjustmentForNativeInput({
49
+ adornmentConfig,
50
+ leftAffixWidth,
51
+ rightAffixWidth,
52
+ paddingHorizontal,
53
+ inputOffset = 0,
54
+ mode,
55
+ }: {
56
+ inputOffset?: number;
57
+ adornmentConfig: AdornmentConfig[];
58
+ leftAffixWidth: number;
59
+ rightAffixWidth: number;
60
+ mode?: "outlined" | "flat";
61
+ paddingHorizontal?: DimensionValue;
62
+ }): AdornmentStyleAdjustmentForNativeInput | {} {
63
+ const { OUTLINED_INPUT_OFFSET, ADORNMENT_OFFSET } = getConstants();
64
+
65
+ if (adornmentConfig.length) {
66
+ const adornmentStyleAdjustmentForNativeInput = adornmentConfig.map(
67
+ ({ type, side }: AdornmentConfig) => {
68
+ const isLeftSide = side === AdornmentSide.Left;
69
+ const inputModeAdornemntOffset =
70
+ mode === InputMode.Outlined
71
+ ? ADORNMENT_OFFSET + OUTLINED_INPUT_OFFSET
72
+ : ADORNMENT_OFFSET;
73
+ const paddingKey = `padding${captalize(side)}`;
74
+ const affixWidth = isLeftSide ? leftAffixWidth : rightAffixWidth;
75
+ const padding =
76
+ typeof paddingHorizontal === "number"
77
+ ? paddingHorizontal
78
+ : inputModeAdornemntOffset;
79
+ const offset = affixWidth + padding;
80
+
81
+ const isAffix = type === AdornmentType.Affix;
82
+ const marginKey = `margin${captalize(side)}`;
83
+
84
+ return {
85
+ [marginKey]: isAffix ? 0 : offset,
86
+ [paddingKey]: isAffix ? offset : inputOffset,
87
+ };
88
+ }
89
+ );
90
+ const allStyleAdjustmentsMerged =
91
+ adornmentStyleAdjustmentForNativeInput.reduce(
92
+ (mergedStyles, currentStyle) => {
93
+ return {
94
+ ...mergedStyles,
95
+ ...currentStyle,
96
+ };
97
+ },
98
+ {}
99
+ );
100
+ return allStyleAdjustmentsMerged;
101
+ } else {
102
+ return [{}];
103
+ }
104
+ }
105
+
106
+ const captalize = (text: string) =>
107
+ text.charAt(0).toUpperCase() + text.slice(1);
108
+
109
+ export interface TextInputAdornmentProps {
110
+ forceFocus: () => void;
111
+ adornmentConfig: AdornmentConfig[];
112
+ topPosition: {
113
+ [AdornmentType.Affix]: {
114
+ [AdornmentSide.Left]: number | null;
115
+ [AdornmentSide.Right]: number | null;
116
+ };
117
+ [AdornmentType.Icon]: number;
118
+ };
119
+ onAffixChange: {
120
+ [AdornmentSide.Left]: (event: LayoutChangeEvent) => void;
121
+ [AdornmentSide.Right]: (event: LayoutChangeEvent) => void;
122
+ };
123
+ left?: React.ReactNode;
124
+ right?: React.ReactNode;
125
+ textStyle?: StyleProp<TextStyle>;
126
+ visible?: Animated.Value;
127
+ isTextInputFocused: boolean;
128
+ paddingHorizontal?: DimensionValue;
129
+ maxFontSizeMultiplier?: number | undefined | null;
130
+ theme?: ThemeProp;
131
+ disabled?: boolean;
132
+ }
133
+
134
+ const TextInputAdornment: React.FunctionComponent<TextInputAdornmentProps> = ({
135
+ adornmentConfig,
136
+ left,
137
+ right,
138
+ onAffixChange,
139
+ textStyle,
140
+ visible,
141
+ topPosition,
142
+ isTextInputFocused,
143
+ forceFocus,
144
+ paddingHorizontal,
145
+ maxFontSizeMultiplier,
146
+ theme,
147
+ disabled,
148
+ }) => {
149
+ if (adornmentConfig.length) {
150
+ return (
151
+ <>
152
+ {adornmentConfig.map(({ type, side }: AdornmentConfig) => {
153
+ let inputAdornmentComponent;
154
+ if (side === AdornmentSide.Left) {
155
+ inputAdornmentComponent = left;
156
+ } else if (side === AdornmentSide.Right) {
157
+ inputAdornmentComponent = right;
158
+ }
159
+
160
+ const commonProps = {
161
+ side: side,
162
+ testID: `${side}-${type}-adornment`,
163
+ isTextInputFocused,
164
+ paddingHorizontal,
165
+ disabled,
166
+ };
167
+ if (type === AdornmentType.Affix) {
168
+ return (
169
+ <AffixAdornment
170
+ {...commonProps}
171
+ key={side}
172
+ topPosition={topPosition[AdornmentType.Affix][side]}
173
+ affix={inputAdornmentComponent}
174
+ textStyle={textStyle}
175
+ onLayout={onAffixChange[side]}
176
+ visible={visible}
177
+ maxFontSizeMultiplier={maxFontSizeMultiplier}
178
+ />
179
+ );
180
+ } else {
181
+ return null;
182
+ }
183
+ })}
184
+ </>
185
+ );
186
+ } else {
187
+ return null;
188
+ }
189
+ };
190
+
191
+ export default TextInputAdornment;
@@ -0,0 +1,212 @@
1
+ import React from "react";
2
+ import {
3
+ Animated,
4
+ DimensionValue,
5
+ GestureResponderEvent,
6
+ LayoutChangeEvent,
7
+ Pressable,
8
+ StyleProp,
9
+ StyleSheet,
10
+ Text,
11
+ TextStyle,
12
+ ViewStyle,
13
+ } from "react-native";
14
+
15
+ import { AdornmentSide } from "./enums";
16
+ import { getTextColor } from "./utils";
17
+ import { useInternalTheme } from "../../../core/theming";
18
+ import type { ThemeProp } from "../../../types";
19
+ import { getConstants } from "../helpers";
20
+
21
+ export type Props = {
22
+ /**
23
+ * Text to show.
24
+ */
25
+ text: string;
26
+ onLayout?: (event: LayoutChangeEvent) => void;
27
+ /**
28
+ * Function to execute on press.
29
+ */
30
+ onPress?: (e: GestureResponderEvent) => void;
31
+ /**
32
+ * Accessibility label for the affix. This is read by the screen reader when the user taps the affix.
33
+ */
34
+ accessibilityLabel?: string;
35
+ /**
36
+ * Style that is passed to the Text element.
37
+ */
38
+ textStyle?: StyleProp<TextStyle>;
39
+ /**
40
+ * @optional
41
+ */
42
+ theme?: ThemeProp;
43
+ };
44
+
45
+ type ContextState = {
46
+ topPosition: number | null;
47
+ onLayout?: (event: LayoutChangeEvent) => void;
48
+ visible?: Animated.Value;
49
+ textStyle?: StyleProp<TextStyle>;
50
+ side: AdornmentSide;
51
+ paddingHorizontal?: DimensionValue;
52
+ maxFontSizeMultiplier?: number | undefined | null;
53
+ testID?: string;
54
+ disabled?: boolean;
55
+ };
56
+
57
+ const AffixContext = React.createContext<ContextState>({
58
+ textStyle: { fontFamily: "", color: "" },
59
+ topPosition: null,
60
+ side: AdornmentSide.Left,
61
+ });
62
+
63
+ const AffixAdornment: React.FunctionComponent<
64
+ {
65
+ affix: React.ReactNode;
66
+ testID: string;
67
+ } & ContextState
68
+ > = ({
69
+ affix,
70
+ side,
71
+ textStyle,
72
+ topPosition,
73
+ onLayout,
74
+ visible,
75
+ paddingHorizontal,
76
+ maxFontSizeMultiplier,
77
+ testID,
78
+ disabled,
79
+ }) => {
80
+ return (
81
+ <AffixContext.Provider
82
+ value={{
83
+ side,
84
+ textStyle,
85
+ topPosition,
86
+ onLayout,
87
+ visible,
88
+ paddingHorizontal,
89
+ maxFontSizeMultiplier,
90
+ testID,
91
+ disabled,
92
+ }}
93
+ >
94
+ {affix}
95
+ </AffixContext.Provider>
96
+ );
97
+ };
98
+
99
+ /**
100
+ * A component to render a leading / trailing text in the TextInput
101
+ *
102
+ * ## Usage
103
+ * ```js
104
+ * import * as React from 'react';
105
+ * import { TextInput } from 'react-native-paper';
106
+ *
107
+ * const MyComponent = () => {
108
+ * const [text, setText] = React.useState('');
109
+ *
110
+ * return (
111
+ * <TextInput
112
+ * mode="outlined"
113
+ * label="Outlined input"
114
+ * placeholder="Type something"
115
+ * right={<TextInput.Affix text="/100" />}
116
+ * />
117
+ * );
118
+ * };
119
+ *
120
+ * export default MyComponent;
121
+ * ```
122
+ */
123
+
124
+ const TextInputAffix = ({
125
+ text,
126
+ textStyle: labelStyle,
127
+ theme: themeOverrides,
128
+ onLayout: onTextLayout,
129
+ onPress,
130
+ accessibilityLabel = text,
131
+ }: Props) => {
132
+ const theme = useInternalTheme();
133
+ const { AFFIX_OFFSET } = getConstants();
134
+
135
+ const {
136
+ textStyle,
137
+ onLayout,
138
+ topPosition,
139
+ side,
140
+ visible,
141
+ paddingHorizontal,
142
+ maxFontSizeMultiplier,
143
+ testID,
144
+ disabled,
145
+ } = React.useContext(AffixContext);
146
+
147
+ const offset =
148
+ typeof paddingHorizontal === "number" ? paddingHorizontal : AFFIX_OFFSET;
149
+
150
+ const style = {
151
+ top: topPosition,
152
+ [side]: offset,
153
+ } as ViewStyle;
154
+
155
+ const textColor = getTextColor({ theme, disabled });
156
+
157
+ const content = (
158
+ <Text
159
+ maxFontSizeMultiplier={maxFontSizeMultiplier}
160
+ style={[{ color: textColor }, textStyle, labelStyle]}
161
+ onLayout={onTextLayout}
162
+ testID={`${testID}-text`}
163
+ >
164
+ {text}
165
+ </Text>
166
+ );
167
+
168
+ return (
169
+ <Animated.View
170
+ style={[
171
+ styles.container,
172
+ style,
173
+ {
174
+ opacity:
175
+ visible?.interpolate({
176
+ inputRange: [0, 1],
177
+ outputRange: [1, 0],
178
+ }) || 1,
179
+ },
180
+ ]}
181
+ onLayout={onLayout}
182
+ testID={testID}
183
+ >
184
+ {onPress ? (
185
+ <Pressable
186
+ onPress={onPress}
187
+ accessibilityRole="button"
188
+ accessibilityLabel={accessibilityLabel}
189
+ >
190
+ {content}
191
+ </Pressable>
192
+ ) : (
193
+ content
194
+ )}
195
+ </Animated.View>
196
+ );
197
+ };
198
+
199
+ TextInputAffix.displayName = "TextInput.Affix";
200
+
201
+ const styles = StyleSheet.create({
202
+ container: {
203
+ position: "absolute",
204
+ justifyContent: "center",
205
+ alignItems: "center",
206
+ },
207
+ });
208
+
209
+ export default TextInputAffix;
210
+
211
+ // @component-docs ignore-next-line
212
+ export { TextInputAffix, AffixAdornment };
@@ -0,0 +1,12 @@
1
+ export enum AdornmentType {
2
+ Icon = 'icon',
3
+ Affix = 'affix',
4
+ }
5
+ export enum AdornmentSide {
6
+ Right = 'right',
7
+ Left = 'left',
8
+ }
9
+ export enum InputMode {
10
+ Outlined = 'outlined',
11
+ Flat = 'flat',
12
+ }