index.tsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. TextInput,
  14. Button,
  15. Header,
  16. Switch
  17. ,
  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. ISwitchProps
  29. } from "ncore-ui-kit";
  30. const SPREAD_BEHAVIOURS: Array<ISwitchProps["spreadBehaviour"]> = [
  31. "baseline",
  32. "stretch",
  33. "free"
  34. ];
  35. const SwitchPage = () => {
  36. const {
  37. radiuses,
  38. borders,
  39. spaces,
  40. colors
  41. } = NCoreUIKitTheme.useContext();
  42. const {
  43. localize
  44. } = NCoreUIKitLocalize.useContext();
  45. const navigation = useNavigation<NativeStackNavigationProp<RootStackParamList>>();
  46. const [
  47. spreadIndex,
  48. setSpreadIndex
  49. ] = useState<number>(0);
  50. const [
  51. title,
  52. setTitle
  53. ] = useState<string>("");
  54. const [
  55. subTitle,
  56. setSubTitle
  57. ] = useState<string>("");
  58. const [
  59. isDisabled,
  60. setIsDisabled
  61. ] = useState<boolean>(false);
  62. const [
  63. isActive,
  64. setIsActive
  65. ] = useState<boolean>(false);
  66. const [
  67. isLoading,
  68. setIsLoading
  69. ] = useState<boolean>(false);
  70. useLayoutEffect(() => {
  71. navigation.setOptions({
  72. header: () => (
  73. <Header
  74. title={localize("switch-title")}
  75. isWrapSafeareaContext={false}
  76. navigation={navigation}
  77. isGoBackEnable={true}
  78. />
  79. ),
  80. headerShown: true
  81. });
  82. }, []);
  83. return (
  84. <PageContainer
  85. scrollViewProps={{
  86. contentContainerStyle: stylesheet.scrollContent
  87. }}
  88. scrollViewStyle={[stylesheet.container]}
  89. isWorkWithHeaderSpace={false}
  90. isScrollable={true}
  91. >
  92. <View style={[stylesheet.contentContainer]}>
  93. <Text
  94. variant="bodyMediumSize"
  95. color="mid"
  96. style={{
  97. marginBottom: 20
  98. }}
  99. >
  100. {localize("switch-desc")}
  101. </Text>
  102. <View
  103. style={{
  104. borderColor: colors.content.border.subtle,
  105. marginBottom: spaces.spacingMd,
  106. borderRadius: radiuses.form,
  107. borderWidth: borders.line,
  108. padding: spaces.spacingMd,
  109. justifyContent: "center",
  110. alignItems: "center",
  111. minHeight: 120
  112. }}
  113. >
  114. <Switch
  115. title={title || localize("switch-exampleTitle")}
  116. spreadBehaviour={SPREAD_BEHAVIOURS[spreadIndex]}
  117. onPress={() => setIsActive(!isActive)}
  118. isDisabled={isDisabled}
  119. isLoading={isLoading}
  120. subTitle={subTitle}
  121. isActive={isActive}
  122. />
  123. </View>
  124. <TextInput
  125. placeholder={localize("switch-enterTitle")}
  126. title={localize("switch-titleLabel")}
  127. style={{
  128. marginBottom: spaces.spacingMd
  129. }}
  130. spreadBehaviour="stretch"
  131. onChangeText={setTitle}
  132. value={title}
  133. />
  134. <TextInput
  135. placeholder={localize("switch-enterSubTitle")}
  136. title={localize("switch-subTitleLabel")}
  137. style={{
  138. marginBottom: spaces.spacingMd
  139. }}
  140. onChangeText={setSubTitle}
  141. spreadBehaviour="stretch"
  142. value={subTitle}
  143. />
  144. <View
  145. style={{
  146. justifyContent: "space-between",
  147. gap: spaces.spacingMd,
  148. flexDirection: "row",
  149. flexWrap: "wrap",
  150. width: "100%"
  151. }}
  152. >
  153. <Button
  154. onPress={() => {
  155. setSpreadIndex((prev) =>
  156. prev + 1 > SPREAD_BEHAVIOURS.length - 1
  157. ? 0
  158. : prev + 1,
  159. );
  160. }}
  161. title={
  162. `${localize("switch-changeSpread")}: ${SPREAD_BEHAVIOURS[spreadIndex]}`
  163. }
  164. spreadBehaviour="free"
  165. />
  166. <View
  167. style={{
  168. justifyContent: "space-between",
  169. flexDirection: "row",
  170. alignItems: "center",
  171. flexWrap: "wrap",
  172. width: "100%"
  173. }}
  174. >
  175. <Switch
  176. title={localize("switch-toggleDisabled")}
  177. onPress={() => setIsDisabled(!isDisabled)}
  178. isActive={isDisabled}
  179. />
  180. <Switch
  181. title={localize("switch-toggleLoading")}
  182. onPress={() => setIsLoading(!isLoading)}
  183. isActive={isLoading}
  184. />
  185. <Switch
  186. title={localize("switch-toggleActive")}
  187. onPress={() => setIsActive(!isActive)}
  188. isActive={isActive}
  189. />
  190. </View>
  191. </View>
  192. </View>
  193. </PageContainer>
  194. );
  195. };
  196. export default SwitchPage;