index.tsx 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. CodeViewer,
  14. SelectBox,
  15. TextInput,
  16. Button,
  17. Header,
  18. Switch,
  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: () => <Header
  113. title={localize("textInput-title")}
  114. isWrapSafeareaContext={false}
  115. navigation={navigation}
  116. isGoBackEnable={true}
  117. />,
  118. headerShown: true
  119. });
  120. }, []);
  121. return <PageContainer
  122. scrollViewProps={{
  123. contentContainerStyle: stylesheet.scrollContent
  124. }}
  125. isWorkWithHeaderSpace={false}
  126. scrollViewStyle={[
  127. stylesheet.container
  128. ]}
  129. isScrollable={true}
  130. >
  131. <View
  132. style={[
  133. stylesheet.contentContainer
  134. ]}
  135. >
  136. <Text
  137. style={stylesheet.descText}
  138. variant="bodyMediumSize"
  139. color="mid"
  140. >
  141. {localize("textInput-desc")}
  142. </Text>
  143. <View
  144. style={[
  145. stylesheet.previewContainer,
  146. {
  147. borderColor: colors.content.border.subtle,
  148. marginBottom: spaces.spacingMd,
  149. borderRadius: radiuses.form,
  150. borderWidth: borders.line,
  151. padding: spaces.spacingMd
  152. }
  153. ]}
  154. >
  155. <TextInput
  156. title={title || localize("textInput-exampleTitle")}
  157. spreadBehaviour={SPREAD_BEHAVIOURS[spreadIndex]}
  158. placeholder={localize("textInput-placeholder")}
  159. icon={
  160. showIcon
  161. ? ({
  162. color,
  163. size
  164. }) => <HomeIcon
  165. color={colors.content.icon[color]}
  166. size={size}
  167. />
  168. : undefined
  169. }
  170. variant={VARIANTS[variantIndex]}
  171. isCleanEnabled={isCleanEnabled}
  172. type={TYPES[typeIndex]}
  173. isDisabled={isDisabled}
  174. isRequired={isRequired}
  175. isOptional={isOptional}
  176. onChangeText={setVal}
  177. value={val}
  178. />
  179. </View>
  180. <View
  181. style={{
  182. marginBottom: spaces.spacingMd
  183. }}
  184. >
  185. <Text
  186. style={{
  187. marginBottom: spaces.spacingSm
  188. }}
  189. variant="headlineSmallSize"
  190. >
  191. {localize("usage")}
  192. </Text>
  193. <CodeViewer
  194. code={"import {\n TextInput\n} from \"ncore-ui-kit\";\n\n<TextInput\n title=\"Title\"\n placeholder=\"Placeholder\"\n value={value}\n onChangeText={setValue}\n/>"}
  195. language="tsx"
  196. />
  197. </View>
  198. <TextInput
  199. placeholder={localize("textInput-enterTitle")}
  200. title={localize("textInput-titleLabel")}
  201. style={{
  202. marginBottom: spaces.spacingMd
  203. }}
  204. spreadBehaviour="stretch"
  205. onChangeText={setTitle}
  206. value={title}
  207. />
  208. <View
  209. style={[
  210. stylesheet.rowContainer,
  211. {
  212. gap: spaces.spacingMd
  213. }
  214. ]}
  215. >
  216. <SelectBox
  217. onChange={(selectedItems) => {
  218. if (selectedItems.length > 0) {
  219. const index = TYPES.indexOf(selectedItems[0]!.__key as unknown as ITextInputProps["type"]);
  220. if (index !== -1) setTypeIndex(index);
  221. }
  222. }}
  223. title={localize("textInput-changeType")}
  224. titleExtractor={(item) => item.__title}
  225. keyExtractor={(item) => item.__key}
  226. initialSelectedItems={
  227. TYPES_DATA[typeIndex]
  228. ? [
  229. TYPES_DATA[typeIndex]!
  230. ]
  231. : []
  232. }
  233. spreadBehaviour="free"
  234. data={TYPES_DATA}
  235. />
  236. <SelectBox
  237. onChange={(selectedItems) => {
  238. if (selectedItems.length > 0) {
  239. const index = VARIANTS.indexOf(selectedItems[0]!.__key as unknown as ITextInputProps["variant"]);
  240. if (index !== -1) setVariantIndex(index);
  241. }
  242. }}
  243. title={localize("textInput-changeVariant")}
  244. initialSelectedItems={
  245. VARIANTS_DATA[variantIndex]
  246. ? [
  247. VARIANTS_DATA[variantIndex]!
  248. ]
  249. : []
  250. }
  251. titleExtractor={(item) => item.__title}
  252. keyExtractor={(item) => item.__key}
  253. spreadBehaviour="free"
  254. data={VARIANTS_DATA}
  255. />
  256. <Button
  257. onPress={() => {
  258. setSpreadIndex((prev) => prev + 1 > SPREAD_BEHAVIOURS.length - 1 ? 0 : prev + 1);
  259. }}
  260. title={`${localize("textInput-changeSpread")}: ${SPREAD_BEHAVIOURS[spreadIndex]}`}
  261. spreadBehaviour="free"
  262. />
  263. <View
  264. style={stylesheet.switchesContainer}
  265. >
  266. <Switch
  267. title={localize("textInput-toggleIcon")}
  268. onPress={() => setShowIcon(!showIcon)}
  269. spreadBehaviour="free"
  270. isActive={showIcon}
  271. />
  272. <Switch
  273. title={localize("textInput-toggleDisabled")}
  274. onPress={() => setIsDisabled(!isDisabled)}
  275. spreadBehaviour="free"
  276. isActive={isDisabled}
  277. />
  278. <Switch
  279. onPress={() => setIsCleanEnabled(!isCleanEnabled)}
  280. title={localize("textInput-toggleClean")}
  281. isActive={isCleanEnabled}
  282. spreadBehaviour="free"
  283. />
  284. <Switch
  285. title={localize("textInput-toggleRequired")}
  286. onPress={() => setIsRequired(!isRequired)}
  287. spreadBehaviour="free"
  288. isActive={isRequired}
  289. />
  290. <Switch
  291. title={localize("textInput-toggleOptional")}
  292. onPress={() => setIsOptional(!isOptional)}
  293. spreadBehaviour="free"
  294. isActive={isOptional}
  295. />
  296. </View>
  297. </View>
  298. </View>
  299. </PageContainer>;
  300. };
  301. export default TextInputPage;