index.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. Button,
  17. Header,
  18. Switch,
  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. IButtonProps
  30. } from "ncore-ui-kit";
  31. import {
  32. Home as HomeIcon
  33. } from "lucide-react-native";
  34. const BUTTON_TYPES: Array<IButtonProps["type"]> = [
  35. "primary",
  36. "danger",
  37. "warning",
  38. "success",
  39. "info"
  40. ];
  41. const BUTTON_VARIANTS: Array<IButtonProps["variant"]> = [
  42. "filled",
  43. "outline",
  44. "ghost"
  45. ];
  46. const BUTTON_SIZES: Array<IButtonProps["size"]> = [
  47. "small",
  48. "medium",
  49. "large"
  50. ];
  51. const BUTTON_TYPES_DATA = BUTTON_TYPES.map(item => ({
  52. __title: item as string,
  53. __key: item as string
  54. }));
  55. const BUTTON_SPREAD_BEHAVIOURS: Array<IButtonProps["spreadBehaviour"]> = [
  56. "baseline",
  57. "stretch",
  58. "free"
  59. ];
  60. const BUTTON_VARIANTS_DATA = BUTTON_VARIANTS.map(item => ({
  61. __title: item as string,
  62. __key: item as string
  63. }));
  64. const BUTTON_SIZES_DATA = BUTTON_SIZES.map(item => ({
  65. __title: item as string,
  66. __key: item as string
  67. }));
  68. const ButtonPage = () => {
  69. const {
  70. radiuses,
  71. borders,
  72. spaces,
  73. colors
  74. } = NCoreUIKitTheme.useContext();
  75. const {
  76. localize
  77. } = NCoreUIKitLocalize.useContext();
  78. const navigation = useNavigation<NativeStackNavigationProp<RootStackParamList>>();
  79. const [
  80. typeIndex,
  81. setTypeIndex
  82. ] = useState<number>(0);
  83. const [
  84. spreadIndex,
  85. setSpreadIndex
  86. ] = useState<number>(0);
  87. const [
  88. variantIndex,
  89. setVariantIndex
  90. ] = useState<number>(0);
  91. const [
  92. sizeIndex,
  93. setSizeIndex
  94. ] = useState<number>(1);
  95. const [
  96. title,
  97. setTitle
  98. ] = useState<string>("");
  99. const [
  100. isLoading,
  101. setIsLoading
  102. ] = useState<boolean>(false);
  103. const [
  104. isDisabled,
  105. setIsDisabled
  106. ] = useState<boolean>(false);
  107. const [
  108. isCustomPadding,
  109. setIsCustomPadding
  110. ] = useState<boolean>(false);
  111. const [
  112. showIcon,
  113. setShowIcon
  114. ] = useState<boolean>(false);
  115. const [
  116. iconDirection,
  117. setIconDirection
  118. ] = useState<"left" | "right">("left");
  119. useLayoutEffect(() => {
  120. navigation.setOptions({
  121. header: () => <Header
  122. isWrapSafeareaContext={false}
  123. title={localize("button")}
  124. navigation={navigation}
  125. isGoBackEnable={true}
  126. />,
  127. headerShown: true
  128. });
  129. }, []);
  130. return <PageContainer
  131. scrollViewProps={{
  132. contentContainerStyle: stylesheet.scrollContent
  133. }}
  134. isWorkWithHeaderSpace={false}
  135. scrollViewStyle={[
  136. stylesheet.container
  137. ]}
  138. isScrollable={true}
  139. >
  140. <View
  141. style={[
  142. stylesheet.contentContainer
  143. ]}
  144. >
  145. <Text
  146. style={stylesheet.descText}
  147. variant="bodyMediumSize"
  148. color="mid"
  149. >
  150. {localize("button-desc")}
  151. </Text>
  152. <View
  153. style={[
  154. stylesheet.previewContainer,
  155. {
  156. borderColor: colors.content.border.subtle,
  157. marginBottom: spaces.spacingMd,
  158. borderRadius: radiuses.form,
  159. borderWidth: borders.line,
  160. padding: spaces.spacingMd
  161. }
  162. ]}
  163. >
  164. <Button
  165. spreadBehaviour={BUTTON_SPREAD_BEHAVIOURS[spreadIndex]}
  166. title={title || localize("example-button")}
  167. variant={BUTTON_VARIANTS[variantIndex]}
  168. icon={showIcon ? ({
  169. color,
  170. size
  171. }) => <HomeIcon
  172. color={colors.content.icon[color]}
  173. size={size}
  174. /> : undefined}
  175. style={{
  176. marginBottom: spaces.spacingLg
  177. }}
  178. isCustomPadding={isCustomPadding}
  179. type={BUTTON_TYPES[typeIndex]}
  180. size={BUTTON_SIZES[sizeIndex]}
  181. iconDirection={iconDirection}
  182. isDisabled={isDisabled}
  183. isLoading={isLoading}
  184. onPress={() => {
  185. }}
  186. />
  187. </View>
  188. <View
  189. style={{
  190. marginBottom: spaces.spacingMd
  191. }}
  192. >
  193. <Text
  194. style={{
  195. marginBottom: spaces.spacingSm
  196. }}
  197. variant="headlineSmallSize"
  198. >
  199. {localize("usage")}
  200. </Text>
  201. <CodeViewer
  202. code={"import {\n Button\n} from \"ncore-ui-kit\";\n\n<Button\n title=\"Click Me!\"\n onPress={() => console.log(\"Pressed\")}\n spreadBehaviour=\"stretch\"\n iconDirection=\"left\"\n variant=\"filled\"\n type=\"primary\"\n size=\"medium\"\n/>"}
  203. language="tsx"
  204. />
  205. </View>
  206. <TextInput
  207. placeholder={localize("enter-text")}
  208. style={{
  209. marginBottom: spaces.spacingMd
  210. }}
  211. title={localize("button-title")}
  212. spreadBehaviour="stretch"
  213. onChangeText={setTitle}
  214. value={title}
  215. />
  216. <View
  217. style={[
  218. stylesheet.rowContainer,
  219. {
  220. gap: spaces.spacingMd
  221. }
  222. ]}
  223. >
  224. <SelectBox
  225. onChange={(selectedItems) => {
  226. if (selectedItems.length > 0) {
  227. const index = BUTTON_TYPES.indexOf(selectedItems[0]!.__key as IButtonProps["type"]);
  228. if (index !== -1) setTypeIndex(index);
  229. }
  230. }}
  231. initialSelectedItems={
  232. BUTTON_TYPES_DATA[typeIndex]
  233. ? [
  234. BUTTON_TYPES_DATA[typeIndex]!
  235. ]
  236. : []
  237. }
  238. titleExtractor={(item) => item.__title}
  239. keyExtractor={(item) => item.__key}
  240. title={localize("change-type")}
  241. data={BUTTON_TYPES_DATA}
  242. spreadBehaviour="free"
  243. />
  244. <SelectBox
  245. onChange={(selectedItems) => {
  246. if (selectedItems.length > 0) {
  247. const index = BUTTON_VARIANTS.indexOf(selectedItems[0]!.__key as IButtonProps["variant"]);
  248. if (index !== -1) setVariantIndex(index);
  249. }
  250. }}
  251. initialSelectedItems={
  252. BUTTON_VARIANTS_DATA[variantIndex]
  253. ? [
  254. BUTTON_VARIANTS_DATA[variantIndex]!
  255. ]
  256. : []
  257. }
  258. titleExtractor={(item) => item.__title}
  259. keyExtractor={(item) => item.__key}
  260. title={localize("change-variant")}
  261. data={BUTTON_VARIANTS_DATA}
  262. spreadBehaviour="free"
  263. />
  264. <Button
  265. onPress={() => {
  266. setSpreadIndex(prev => prev + 1 > BUTTON_SPREAD_BEHAVIOURS.length - 1 ? 0 : prev + 1);
  267. }}
  268. title={`${localize("change-spread")}: ${BUTTON_SPREAD_BEHAVIOURS[spreadIndex]}`}
  269. verticalLocationForStretchFix="center"
  270. isFixVerticalStretch={true}
  271. spreadBehaviour="free"
  272. />
  273. <SelectBox
  274. onChange={(selectedItems) => {
  275. if (selectedItems.length > 0) {
  276. const index = BUTTON_SIZES.indexOf(selectedItems[0]!.__key as IButtonProps["size"]);
  277. if (index !== -1) setSizeIndex(index);
  278. }
  279. }}
  280. initialSelectedItems={
  281. BUTTON_SIZES_DATA[sizeIndex]
  282. ? [
  283. BUTTON_SIZES_DATA[sizeIndex]!
  284. ]
  285. : []
  286. }
  287. titleExtractor={(item) => item.__title}
  288. keyExtractor={(item) => item.__key}
  289. title={localize("change-size")}
  290. data={BUTTON_SIZES_DATA}
  291. spreadBehaviour="free"
  292. />
  293. <View
  294. style={stylesheet.switchesContainer}
  295. >
  296. <Switch
  297. onPress={() => {
  298. setIconDirection(iconDirection === "left" ? "right" : "left");
  299. }}
  300. title={localize("toggle-icon-direction")}
  301. isActive={iconDirection === "right"}
  302. subTitle={localize(iconDirection)}
  303. spreadBehaviour="free"
  304. />
  305. <Switch
  306. title={localize("toggle-icon")}
  307. onPress={() => {
  308. setShowIcon(!showIcon);
  309. }}
  310. spreadBehaviour="free"
  311. isActive={showIcon}
  312. />
  313. <Switch
  314. title={localize("toggle-loading")}
  315. onPress={() => {
  316. setIsLoading(!isLoading);
  317. }}
  318. spreadBehaviour="free"
  319. isActive={isLoading}
  320. />
  321. <Switch
  322. title={localize("toggle-disabled")}
  323. onPress={() => {
  324. setIsDisabled(!isDisabled);
  325. }}
  326. spreadBehaviour="free"
  327. isActive={isDisabled}
  328. />
  329. <Switch
  330. onPress={() => {
  331. setIsCustomPadding(!isCustomPadding);
  332. }}
  333. title={localize("toggle-custom-padding")}
  334. isActive={isCustomPadding}
  335. spreadBehaviour="free"
  336. />
  337. </View>
  338. </View>
  339. </View>
  340. </PageContainer>;
  341. };
  342. export default ButtonPage;