index.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. type ICheckBoxProps,
  11. NCoreUIKitLocalize,
  12. NCoreUIKitTheme,
  13. PageContainer,
  14. CodeViewer,
  15. SelectBox,
  16. TextInput,
  17. CheckBox,
  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. const CHECK_BOX_TYPES: Array<ICheckBoxProps["type"]> = [
  30. "primary",
  31. "danger",
  32. "warning",
  33. "success",
  34. "info"
  35. ];
  36. const CHECK_BOX_SPREAD_BEHAVIOURS: Array<ICheckBoxProps["spreadBehaviour"]> = [
  37. "baseline",
  38. "stretch",
  39. "free"
  40. ];
  41. const CHECK_BOX_TYPES_DATA = CHECK_BOX_TYPES.map((item) => ({
  42. __title: item as string,
  43. __key: item as string
  44. }));
  45. const CHECK_BOX_SPREAD_BEHAVIOURS_DATA = CHECK_BOX_SPREAD_BEHAVIOURS.map(
  46. (item) => ({
  47. __title: item as string,
  48. __key: item as string
  49. }),
  50. );
  51. type IsCheckedType = "checked" | "partially" | null;
  52. const IS_CHECKED_TYPES: Array<IsCheckedType> = [
  53. "partially",
  54. "checked",
  55. null
  56. ];
  57. const IS_CHECKED_TYPES_DATA = IS_CHECKED_TYPES.map((item) => ({
  58. __title: String(item),
  59. __key: String(item)
  60. }));
  61. const CheckBoxPage = () => {
  62. const {
  63. radiuses,
  64. borders,
  65. spaces,
  66. colors
  67. } = NCoreUIKitTheme.useContext();
  68. const {
  69. localize
  70. } = NCoreUIKitLocalize.useContext();
  71. const navigation = useNavigation<NativeStackNavigationProp<RootStackParamList>>();
  72. const [
  73. spreadIndex,
  74. setSpreadIndex
  75. ] = useState<number>(0);
  76. const [
  77. typeIndex,
  78. setTypeIndex
  79. ] = useState<number>(0);
  80. const [
  81. isCheckedIndex,
  82. setIsCheckedIndex
  83. ] = useState<number>(0);
  84. const [
  85. isDisabled,
  86. setIsDisabled
  87. ] = useState<boolean>(false);
  88. const [
  89. isLoading,
  90. setIsLoading
  91. ] = useState<boolean>(false);
  92. const [
  93. subTitle,
  94. setSubTitle
  95. ] = useState<string>("Subtitle");
  96. const [
  97. isFlip,
  98. setIsFlip
  99. ] = useState<boolean>(false);
  100. const [
  101. title,
  102. setTitle
  103. ] = useState<string>("CheckBox Title");
  104. useLayoutEffect(() => {
  105. navigation.setOptions({
  106. header: () => <Header
  107. title={localize("checkBox-title")}
  108. isWrapSafeareaContext={false}
  109. navigation={navigation}
  110. isGoBackEnable={true}
  111. />,
  112. headerShown: true
  113. });
  114. }, []);
  115. return <PageContainer
  116. scrollViewProps={{
  117. contentContainerStyle: stylesheet.scrollContent
  118. }}
  119. isWorkWithHeaderSpace={false}
  120. scrollViewStyle={[
  121. stylesheet.container
  122. ]}
  123. isScrollable={true}
  124. >
  125. <View
  126. style={[
  127. stylesheet.contentContainer
  128. ]}
  129. >
  130. <Text
  131. style={stylesheet.descText}
  132. variant="bodyMediumSize"
  133. color="mid"
  134. >
  135. {localize("checkBox-desc")}
  136. </Text>
  137. <View
  138. style={[
  139. stylesheet.previewContainer,
  140. {
  141. borderColor: colors.content.border.subtle,
  142. marginBottom: spaces.spacingMd,
  143. borderRadius: radiuses.form,
  144. borderWidth: borders.line,
  145. padding: spaces.spacingMd
  146. }
  147. ]}
  148. >
  149. <CheckBox
  150. onPress={() => {
  151. setIsCheckedIndex((prev) => (prev === 0 ? 2 : 0));
  152. }}
  153. spreadBehaviour={
  154. CHECK_BOX_SPREAD_BEHAVIOURS[spreadIndex]
  155. }
  156. isChecked={IS_CHECKED_TYPES[isCheckedIndex]}
  157. type={CHECK_BOX_TYPES[typeIndex]}
  158. isDisabled={isDisabled}
  159. isLoading={isLoading}
  160. subTitle={subTitle}
  161. isFlip={isFlip}
  162. title={title}
  163. />
  164. </View>
  165. <View
  166. style={{
  167. marginBottom: spaces.spacingMd
  168. }}
  169. >
  170. <Text
  171. style={{
  172. marginBottom: spaces.spacingSm
  173. }}
  174. variant="headlineSmallSize"
  175. >
  176. {localize("usage")}
  177. </Text>
  178. <CodeViewer
  179. code={"import {\n CheckBox\n} from \"ncore-ui-kit\";\n\n<CheckBox\n title=\"CheckBox Title\"\n subTitle=\"Subtitle\"\n type=\"primary\"\n/>"}
  180. language="tsx"
  181. />
  182. </View>
  183. <TextInput
  184. placeholder={localize("checkBox-enterText")}
  185. title={localize("checkBox-titleLabel")}
  186. style={{
  187. marginBottom: spaces.spacingMd
  188. }}
  189. spreadBehaviour="stretch"
  190. onChangeText={setTitle}
  191. value={title}
  192. />
  193. <TextInput
  194. placeholder={localize("checkBox-enterSubText")}
  195. title={localize("checkBox-subTitleLabel")}
  196. style={{
  197. marginBottom: spaces.spacingMd
  198. }}
  199. onChangeText={setSubTitle}
  200. spreadBehaviour="stretch"
  201. value={subTitle}
  202. />
  203. <View
  204. style={[
  205. stylesheet.rowContainer,
  206. {
  207. gap: spaces.spacingMd
  208. }
  209. ]}
  210. >
  211. <SelectBox
  212. onChange={(selectedItems) => {
  213. if (selectedItems.length > 0) {
  214. const index = CHECK_BOX_TYPES.indexOf(
  215. selectedItems[0]!
  216. .__key as ICheckBoxProps["type"],
  217. );
  218. if (index !== -1) setTypeIndex(index);
  219. }
  220. }}
  221. initialSelectedItems={
  222. CHECK_BOX_TYPES_DATA[typeIndex]
  223. ? [
  224. CHECK_BOX_TYPES_DATA[typeIndex]!
  225. ]
  226. : []
  227. }
  228. title={localize("checkBox-changeType")}
  229. titleExtractor={(item) => item.__title}
  230. keyExtractor={(item) => item.__key}
  231. data={CHECK_BOX_TYPES_DATA}
  232. spreadBehaviour="free"
  233. />
  234. <SelectBox
  235. onChange={(selectedItems) => {
  236. if (selectedItems.length > 0) {
  237. const index =
  238. CHECK_BOX_SPREAD_BEHAVIOURS.indexOf(
  239. selectedItems[0]!
  240. .__key as ICheckBoxProps["spreadBehaviour"],
  241. );
  242. if (index !== -1) setSpreadIndex(index);
  243. }
  244. }}
  245. initialSelectedItems={
  246. CHECK_BOX_SPREAD_BEHAVIOURS_DATA[spreadIndex]
  247. ? [
  248. CHECK_BOX_SPREAD_BEHAVIOURS_DATA[
  249. spreadIndex
  250. ]!
  251. ]
  252. : []
  253. }
  254. title={localize("checkBox-changeSpread")}
  255. data={CHECK_BOX_SPREAD_BEHAVIOURS_DATA}
  256. titleExtractor={(item) => item.__title}
  257. keyExtractor={(item) => item.__key}
  258. spreadBehaviour="free"
  259. />
  260. <SelectBox
  261. onChange={(selectedItems) => {
  262. if (selectedItems.length > 0) {
  263. const index = IS_CHECKED_TYPES_DATA.findIndex(
  264. (item) =>
  265. item.__key === selectedItems[0]!.__key,
  266. );
  267. if (index !== -1) setIsCheckedIndex(index);
  268. }
  269. }}
  270. initialSelectedItems={
  271. IS_CHECKED_TYPES_DATA[isCheckedIndex]
  272. ? [
  273. IS_CHECKED_TYPES_DATA[isCheckedIndex]!
  274. ]
  275. : []
  276. }
  277. title={localize("checkBox-changeIsChecked")}
  278. titleExtractor={(item) => item.__title}
  279. keyExtractor={(item) => item.__key}
  280. data={IS_CHECKED_TYPES_DATA}
  281. spreadBehaviour="free"
  282. />
  283. <View
  284. style={stylesheet.switchesContainer}
  285. >
  286. <Switch
  287. title={localize("checkBox-toggleLoading")}
  288. onPress={() => {
  289. setIsLoading(!isLoading);
  290. }}
  291. spreadBehaviour="free"
  292. isActive={isLoading}
  293. />
  294. <Switch
  295. title={localize("checkBox-toggleDisabled")}
  296. onPress={() => {
  297. setIsDisabled(!isDisabled);
  298. }}
  299. spreadBehaviour="free"
  300. isActive={isDisabled}
  301. />
  302. <Switch
  303. title={localize("checkBox-toggleFlip")}
  304. onPress={() => {
  305. setIsFlip(!isFlip);
  306. }}
  307. spreadBehaviour="free"
  308. isActive={isFlip}
  309. />
  310. </View>
  311. </View>
  312. </View>
  313. </PageContainer>;
  314. };
  315. export default CheckBoxPage;