index.tsx 8.9 KB

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