index.tsx 8.2 KB

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