index.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. SelectBox,
  14. TextInput,
  15. Button,
  16. Header,
  17. Switch
  18. ,
  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. __key: item as string,
  53. __title: 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. __key: item as string,
  62. __title: item as string
  63. }));
  64. const BUTTON_SIZES_DATA = BUTTON_SIZES.map(item => ({
  65. __key: item as string,
  66. __title: 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. headerShown: true,
  122. header: () => <Header
  123. isWrapSafeareaContext={false}
  124. title={localize("button")}
  125. navigation={navigation}
  126. isGoBackEnable={true}
  127. />
  128. });
  129. }, []);
  130. return <PageContainer
  131. isWorkWithHeaderSpace={false}
  132. isScrollable={true}
  133. scrollViewProps={{
  134. contentContainerStyle: stylesheet.scrollContent
  135. }}
  136. scrollViewStyle={[
  137. stylesheet.container
  138. ]}
  139. >
  140. <View
  141. style={[
  142. stylesheet.contentContainer
  143. ]}
  144. >
  145. <Text
  146. variant="bodyMediumSize"
  147. color="mid"
  148. style={{
  149. marginBottom: 20
  150. }}
  151. >
  152. {localize("button-desc")}
  153. </Text>
  154. <View
  155. style={{
  156. borderColor: colors.content.border.subtle,
  157. marginBottom: spaces.spacingMd,
  158. borderRadius: radiuses.form,
  159. borderWidth: borders.line,
  160. padding: spaces.spacingMd,
  161. justifyContent: "center",
  162. alignItems: "center",
  163. minHeight: 120
  164. }}
  165. >
  166. <Button
  167. spreadBehaviour={BUTTON_SPREAD_BEHAVIOURS[spreadIndex]}
  168. title={title || localize("example-button")}
  169. variant={BUTTON_VARIANTS[variantIndex]}
  170. isCustomPadding={isCustomPadding}
  171. type={BUTTON_TYPES[typeIndex]}
  172. size={BUTTON_SIZES[sizeIndex]}
  173. iconDirection={iconDirection}
  174. isDisabled={isDisabled}
  175. isLoading={isLoading}
  176. icon={showIcon ? ({
  177. color,
  178. size
  179. }) => <HomeIcon
  180. color={colors.content.icon[color]}
  181. size={size}
  182. /> : undefined}
  183. style={{
  184. marginBottom: spaces.spacingLg
  185. }}
  186. onPress={() => {
  187. }}
  188. />
  189. </View>
  190. <TextInput
  191. placeholder={localize("enter-text")}
  192. title={localize("button-title")}
  193. spreadBehaviour="stretch"
  194. onChangeText={setTitle}
  195. value={title}
  196. style={{
  197. marginBottom: spaces.spacingMd
  198. }}
  199. />
  200. <View
  201. style={{
  202. justifyContent: "space-between",
  203. gap: spaces.spacingMd,
  204. flexDirection: "row",
  205. flexWrap: "wrap",
  206. width: "100%"
  207. }}
  208. >
  209. <SelectBox
  210. initialSelectedItems={BUTTON_TYPES_DATA[typeIndex] ? [
  211. BUTTON_TYPES_DATA[typeIndex]
  212. ] : []}
  213. titleExtractor={(item) => item.__title}
  214. keyExtractor={(item) => item.__key}
  215. title={localize("change-type")}
  216. spreadBehaviour="free"
  217. data={BUTTON_TYPES_DATA}
  218. onChange={(selectedItems) => {
  219. if (selectedItems.length > 0) {
  220. const index = BUTTON_TYPES.indexOf(selectedItems[0]!.__key as IButtonProps["type"]);
  221. if (index !== -1) setTypeIndex(index);
  222. }
  223. }}
  224. />
  225. <SelectBox
  226. initialSelectedItems={BUTTON_VARIANTS_DATA[variantIndex] ? [
  227. BUTTON_VARIANTS_DATA[variantIndex]
  228. ] : []}
  229. titleExtractor={(item) => item.__title}
  230. keyExtractor={(item) => item.__key}
  231. title={localize("change-variant")}
  232. data={BUTTON_VARIANTS_DATA}
  233. spreadBehaviour="free"
  234. onChange={(selectedItems) => {
  235. if (selectedItems.length > 0) {
  236. const index = BUTTON_VARIANTS.indexOf(selectedItems[0]!.__key as IButtonProps["variant"]);
  237. if (index !== -1) setVariantIndex(index);
  238. }
  239. }}
  240. />
  241. <Button
  242. title={`${localize("change-spread")}: ${BUTTON_SPREAD_BEHAVIOURS[spreadIndex]}`}
  243. verticalLocationForStretchFix="center"
  244. isFixVerticalStretch={true}
  245. spreadBehaviour="free"
  246. onPress={() => {
  247. setSpreadIndex(prev => prev + 1 > BUTTON_SPREAD_BEHAVIOURS.length - 1 ? 0 : prev + 1);
  248. }}
  249. />
  250. <SelectBox
  251. initialSelectedItems={BUTTON_SIZES_DATA[sizeIndex] ? [
  252. BUTTON_SIZES_DATA[sizeIndex]
  253. ] : []}
  254. titleExtractor={(item) => item.__title}
  255. keyExtractor={(item) => item.__key}
  256. title={localize("change-size")}
  257. spreadBehaviour="free"
  258. data={BUTTON_SIZES_DATA}
  259. onChange={(selectedItems) => {
  260. if (selectedItems.length > 0) {
  261. const index = BUTTON_SIZES.indexOf(selectedItems[0]!.__key as IButtonProps["size"]);
  262. if (index !== -1) setSizeIndex(index);
  263. }
  264. }}
  265. />
  266. <View
  267. style={{
  268. justifyContent: "space-between",
  269. flexDirection: "row",
  270. alignItems: "center",
  271. flexWrap: "wrap",
  272. width: "100%"
  273. }}
  274. >
  275. <Switch
  276. title={localize("toggle-icon-direction")}
  277. isActive={iconDirection === "right"}
  278. subTitle={localize(iconDirection)}
  279. onPress={() => {
  280. setIconDirection(iconDirection === "left" ? "right" : "left");
  281. }}
  282. />
  283. <Switch
  284. title={localize("toggle-icon")}
  285. isActive={showIcon}
  286. onPress={() => {
  287. setShowIcon(!showIcon);
  288. }}
  289. />
  290. <Switch
  291. title={localize("toggle-loading")}
  292. isActive={isLoading}
  293. onPress={() => {
  294. setIsLoading(!isLoading);
  295. }}
  296. />
  297. <Switch
  298. title={localize("toggle-disabled")}
  299. isActive={isDisabled}
  300. onPress={() => {
  301. setIsDisabled(!isDisabled);
  302. }}
  303. />
  304. <Switch
  305. title={localize("toggle-custom-padding")}
  306. isActive={isCustomPadding}
  307. onPress={() => {
  308. setIsCustomPadding(!isCustomPadding);
  309. }}
  310. />
  311. </View>
  312. </View>
  313. </View>
  314. </PageContainer>;
  315. };
  316. export default ButtonPage;