index.tsx 9.2 KB

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