index.tsx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. import {
  2. useLayoutEffect,
  3. useState
  4. } from "react";
  5. import {
  6. View
  7. } from "react-native";
  8. import stylesheet from "./stylesheet";
  9. import {
  10. NCoreUIKitLocalize,
  11. NCoreUIKitTheme,
  12. PageContainer,
  13. SelectBox,
  14. TextInput,
  15. Button,
  16. Header,
  17. Switch
  18. ,
  19. Text
  20. } from "ncore-ui-kit";
  21. import {
  22. useNavigation
  23. } from "@react-navigation/native";
  24. import type RootStackParamList from "../../navigation/type";
  25. import type {
  26. NativeStackNavigationProp
  27. } from "@react-navigation/native-stack";
  28. import type {
  29. ITextInputProps
  30. } from "ncore-ui-kit";
  31. import {
  32. Home as HomeIcon
  33. } from "lucide-react-native";
  34. const TYPES: Array<ITextInputProps["type"]> = [
  35. "default",
  36. "danger",
  37. "warning",
  38. "question",
  39. "success",
  40. "info"
  41. ];
  42. const VARIANTS: Array<ITextInputProps["variant"]> = [
  43. "text",
  44. "hidden"
  45. ];
  46. const SPREAD_BEHAVIOURS: Array<ITextInputProps["spreadBehaviour"]> = [
  47. "baseline",
  48. "stretch",
  49. "free"
  50. ];
  51. const TYPES_DATA = TYPES.map((item) => ({
  52. __title: item as string,
  53. __key: item as string
  54. }));
  55. const VARIANTS_DATA = VARIANTS.map((item) => ({
  56. __title: item as string,
  57. __key: item as string
  58. }));
  59. const TextInputPage = () => {
  60. const {
  61. radiuses,
  62. borders,
  63. spaces,
  64. colors
  65. } = NCoreUIKitTheme.useContext();
  66. const {
  67. localize
  68. } = NCoreUIKitLocalize.useContext();
  69. const navigation = useNavigation<NativeStackNavigationProp<RootStackParamList>>();
  70. const [
  71. typeIndex,
  72. setTypeIndex
  73. ] = useState<number>(0);
  74. const [
  75. variantIndex,
  76. setVariantIndex
  77. ] = useState<number>(0);
  78. const [
  79. spreadIndex,
  80. setSpreadIndex
  81. ] = useState<number>(0);
  82. const [
  83. title,
  84. setTitle
  85. ] = useState<string>("");
  86. const [
  87. isDisabled,
  88. setIsDisabled
  89. ] = useState<boolean>(false);
  90. const [
  91. showIcon,
  92. setShowIcon
  93. ] = useState<boolean>(false);
  94. const [
  95. isCleanEnabled,
  96. setIsCleanEnabled
  97. ] = useState<boolean>(true);
  98. const [
  99. isRequired,
  100. setIsRequired
  101. ] = useState<boolean>(false);
  102. const [
  103. isOptional,
  104. setIsOptional
  105. ] = useState<boolean>(false);
  106. const [
  107. val,
  108. setVal
  109. ] = useState<string>("");
  110. useLayoutEffect(() => {
  111. navigation.setOptions({
  112. header: () => (
  113. <Header
  114. title={localize("textInput-title")}
  115. isWrapSafeareaContext={false}
  116. navigation={navigation}
  117. isGoBackEnable={true}
  118. />
  119. ),
  120. headerShown: true
  121. });
  122. }, []);
  123. return (
  124. <PageContainer
  125. scrollViewProps={{
  126. contentContainerStyle: stylesheet.scrollContent
  127. }}
  128. scrollViewStyle={[stylesheet.container]}
  129. isWorkWithHeaderSpace={false}
  130. isScrollable={true}
  131. >
  132. <View style={[stylesheet.contentContainer]}>
  133. <Text
  134. variant="bodyMediumSize"
  135. color="mid"
  136. style={{
  137. marginBottom: 20
  138. }}
  139. >
  140. {localize("textInput-desc")}
  141. </Text>
  142. <View
  143. style={{
  144. borderColor: colors.content.border.subtle,
  145. marginBottom: spaces.spacingMd,
  146. borderRadius: radiuses.form,
  147. borderWidth: borders.line,
  148. padding: spaces.spacingMd,
  149. justifyContent: "center",
  150. alignItems: "center",
  151. minHeight: 120
  152. }}
  153. >
  154. <TextInput
  155. icon={
  156. showIcon
  157. ? ({
  158. color,
  159. size
  160. }) => (
  161. <HomeIcon
  162. color={colors.content.icon[color]}
  163. size={size}
  164. />
  165. )
  166. : undefined
  167. }
  168. title={
  169. title || localize("textInput-exampleTitle")
  170. }
  171. placeholder={localize("textInput-placeholder")}
  172. spreadBehaviour={SPREAD_BEHAVIOURS[spreadIndex]}
  173. variant={VARIANTS[variantIndex]}
  174. isCleanEnabled={isCleanEnabled}
  175. type={TYPES[typeIndex]}
  176. isDisabled={isDisabled}
  177. isRequired={isRequired}
  178. isOptional={isOptional}
  179. onChangeText={setVal}
  180. value={val}
  181. />
  182. </View>
  183. <TextInput
  184. placeholder={localize("textInput-enterTitle")}
  185. title={localize("textInput-titleLabel")}
  186. style={{
  187. marginBottom: spaces.spacingMd
  188. }}
  189. spreadBehaviour="stretch"
  190. onChangeText={setTitle}
  191. value={title}
  192. />
  193. <View
  194. style={{
  195. justifyContent: "space-between",
  196. gap: spaces.spacingMd,
  197. flexDirection: "row",
  198. flexWrap: "wrap",
  199. width: "100%"
  200. }}
  201. >
  202. <SelectBox
  203. initialSelectedItems={TYPES_DATA[typeIndex] ? [TYPES_DATA[typeIndex]] : []}
  204. onChange={(selectedItems) => {
  205. if (selectedItems.length > 0) {
  206. const index = TYPES.indexOf(selectedItems[0]!.__key as unknown as ITextInputProps["type"]);
  207. if (index !== -1) setTypeIndex(index);
  208. }
  209. }}
  210. title={localize("textInput-changeType")}
  211. titleExtractor={(item) => item.__title}
  212. keyExtractor={(item) => item.__key}
  213. spreadBehaviour="free"
  214. data={TYPES_DATA}
  215. />
  216. <SelectBox
  217. onChange={(selectedItems) => {
  218. if (selectedItems.length > 0) {
  219. const index = VARIANTS.indexOf(selectedItems[0]!.__key as unknown as ITextInputProps["variant"]);
  220. if (index !== -1) setVariantIndex(index);
  221. }
  222. }}
  223. initialSelectedItems={VARIANTS_DATA[variantIndex] ? [VARIANTS_DATA[variantIndex]] : []}
  224. title={localize("textInput-changeVariant")}
  225. titleExtractor={(item) => item.__title}
  226. keyExtractor={(item) => item.__key}
  227. spreadBehaviour="free"
  228. data={VARIANTS_DATA}
  229. />
  230. <Button
  231. onPress={() => {
  232. setSpreadIndex((prev) => prev + 1 > SPREAD_BEHAVIOURS.length - 1 ? 0 : prev + 1);
  233. }}
  234. title={`${localize("textInput-changeSpread")}: ${SPREAD_BEHAVIOURS[spreadIndex]}`}
  235. spreadBehaviour="free"
  236. />
  237. <View
  238. style={{
  239. justifyContent: "space-between",
  240. flexDirection: "row",
  241. alignItems: "center",
  242. flexWrap: "wrap",
  243. width: "100%"
  244. }}
  245. >
  246. <Switch
  247. title={localize("textInput-toggleIcon")}
  248. onPress={() => setShowIcon(!showIcon)}
  249. isActive={showIcon}
  250. />
  251. <Switch
  252. title={localize("textInput-toggleDisabled")}
  253. onPress={() => setIsDisabled(!isDisabled)}
  254. isActive={isDisabled}
  255. />
  256. <Switch
  257. onPress={() => setIsCleanEnabled(!isCleanEnabled)}
  258. title={localize("textInput-toggleClean")}
  259. isActive={isCleanEnabled}
  260. />
  261. <Switch
  262. title={localize("textInput-toggleRequired")}
  263. onPress={() => setIsRequired(!isRequired)}
  264. isActive={isRequired}
  265. />
  266. <Switch
  267. title={localize("textInput-toggleOptional")}
  268. onPress={() => setIsOptional(!isOptional)}
  269. isActive={isOptional}
  270. />
  271. </View>
  272. </View>
  273. </View>
  274. </PageContainer>
  275. );
  276. };
  277. export default TextInputPage;