index.tsx 8.1 KB

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