index.tsx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. TextAreaInput,
  15. SelectBox,
  16. TextInput,
  17. Button,
  18. Header,
  19. Switch,
  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: () => <Header
  95. title={localize("textAreaInput-title")}
  96. isWrapSafeareaContext={false}
  97. navigation={navigation}
  98. isGoBackEnable={true}
  99. />,
  100. headerShown: true
  101. });
  102. }, []);
  103. return <PageContainer
  104. scrollViewProps={{
  105. contentContainerStyle: stylesheet.scrollContent
  106. }}
  107. scrollViewStyle={[stylesheet.container]}
  108. isWorkWithHeaderSpace={false}
  109. isScrollable={true}
  110. >
  111. <View
  112. style={[stylesheet.contentContainer]}
  113. >
  114. <Text
  115. variant="bodyMediumSize"
  116. color="mid"
  117. style={stylesheet.descText}
  118. >
  119. {localize("textAreaInput-desc")}
  120. </Text>
  121. <View
  122. style={[
  123. stylesheet.previewContainer,
  124. {
  125. borderColor: colors.content.border.subtle,
  126. marginBottom: spaces.spacingMd,
  127. borderRadius: radiuses.form,
  128. borderWidth: borders.line,
  129. padding: spaces.spacingMd
  130. }
  131. ]}
  132. >
  133. <TextAreaInput
  134. title={
  135. title ||
  136. localize("textAreaInput-exampleTitle")
  137. }
  138. placeholder={localize(
  139. "textAreaInput-placeholder",
  140. )}
  141. spreadBehaviour={SPREAD_BEHAVIOURS[spreadIndex]}
  142. isCleanEnabled={isCleanEnabled}
  143. type={TYPES[typeIndex]}
  144. isDisabled={isDisabled}
  145. isRequired={isRequired}
  146. isOptional={isOptional}
  147. onChangeText={setVal}
  148. value={val}
  149. isUpdateOnRealtime={true}
  150. />
  151. </View>
  152. <View
  153. style={{
  154. marginBottom: spaces.spacingMd
  155. }}
  156. >
  157. <Text
  158. variant="headlineSmallSize"
  159. style={{
  160. marginBottom: spaces.spacingSm
  161. }}
  162. >
  163. {localize("usage")}
  164. </Text>
  165. <CodeViewer
  166. code={"import {\n TextAreaInput\n} from \"ncore-ui-kit\";\n\n<TextAreaInput\n title=\"Title\"\n placeholder=\"Placeholder\"\n value={value}\n onChangeText={setValue}\n/>"}
  167. language="tsx"
  168. />
  169. </View>
  170. <TextInput
  171. placeholder={localize("textAreaInput-enterTitle")}
  172. title={localize("textAreaInput-titleLabel")}
  173. style={{
  174. marginBottom: spaces.spacingMd
  175. }}
  176. spreadBehaviour="stretch"
  177. onChangeText={setTitle}
  178. value={title}
  179. />
  180. <View
  181. style={[
  182. stylesheet.rowContainer,
  183. {
  184. gap: spaces.spacingMd
  185. }
  186. ]}
  187. >
  188. <SelectBox
  189. onChange={(selectedItems) => {
  190. if (selectedItems.length > 0) {
  191. const index = TYPES.indexOf(
  192. selectedItems[0]!
  193. .__key as unknown as ITextAreaInputProps["type"],
  194. );
  195. if (index !== -1) setTypeIndex(index);
  196. }
  197. }}
  198. initialSelectedItems={
  199. TYPES_DATA[typeIndex] ? [TYPES_DATA[typeIndex]!] : []
  200. }
  201. title={localize("textAreaInput-changeType")}
  202. titleExtractor={(item) => item.__title}
  203. keyExtractor={(item) => item.__key}
  204. spreadBehaviour="free"
  205. data={TYPES_DATA}
  206. />
  207. <Button
  208. onPress={() => {
  209. setSpreadIndex((prev) =>
  210. prev + 1 > SPREAD_BEHAVIOURS.length - 1
  211. ? 0
  212. : prev + 1,
  213. );
  214. }}
  215. title={
  216. `${localize("textAreaInput-changeSpread")}: ${SPREAD_BEHAVIOURS[spreadIndex]}`
  217. }
  218. spreadBehaviour="free"
  219. />
  220. <View
  221. style={stylesheet.switchesContainer}
  222. >
  223. <Switch
  224. title={localize(
  225. "textAreaInput-toggleDisabled",
  226. )}
  227. onPress={() => setIsDisabled(!isDisabled)}
  228. spreadBehaviour="free"
  229. isActive={isDisabled}
  230. />
  231. <Switch
  232. onPress={() => setIsCleanEnabled(!isCleanEnabled)}
  233. title={localize("textAreaInput-toggleClean")}
  234. isActive={isCleanEnabled}
  235. spreadBehaviour="free"
  236. />
  237. <Switch
  238. title={localize(
  239. "textAreaInput-toggleRequired",
  240. )}
  241. onPress={() => setIsRequired(!isRequired)}
  242. spreadBehaviour="free"
  243. isActive={isRequired}
  244. />
  245. <Switch
  246. title={localize(
  247. "textAreaInput-toggleOptional",
  248. )}
  249. onPress={() => setIsOptional(!isOptional)}
  250. spreadBehaviour="free"
  251. isActive={isOptional}
  252. />
  253. </View>
  254. </View>
  255. </View>
  256. </PageContainer>;
  257. };
  258. export default TextAreaInputPage;