index.tsx 10.0 KB

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