index.tsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. RadioButton,
  14. SelectBox,
  15. TextInput,
  16. Button,
  17. Header,
  18. Switch
  19. ,
  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. import type {
  30. IRadioButtonProps
  31. } from "ncore-ui-kit";
  32. const TYPES: Array<IRadioButtonProps["type"]> = [
  33. "primary",
  34. "danger",
  35. "warning",
  36. "success",
  37. "info"
  38. ];
  39. const SPREAD_BEHAVIOURS: Array<IRadioButtonProps["spreadBehaviour"]> = [
  40. "baseline",
  41. "stretch",
  42. "free"
  43. ];
  44. const TYPES_DATA = TYPES.map((item) => ({
  45. __title: item as string,
  46. __key: item as string
  47. }));
  48. const RadioButtonPage = () => {
  49. const {
  50. radiuses,
  51. borders,
  52. spaces,
  53. colors
  54. } = NCoreUIKitTheme.useContext();
  55. const {
  56. localize
  57. } = NCoreUIKitLocalize.useContext();
  58. const navigation = useNavigation<NativeStackNavigationProp<RootStackParamList>>();
  59. const [
  60. typeIndex,
  61. setTypeIndex
  62. ] = useState<number>(0);
  63. const [
  64. spreadIndex,
  65. setSpreadIndex
  66. ] = useState<number>(0);
  67. const [
  68. title,
  69. setTitle
  70. ] = useState<string>("");
  71. const [
  72. isDisabled,
  73. setIsDisabled
  74. ] = useState<boolean>(false);
  75. const [
  76. isLoading,
  77. setIsLoading
  78. ] = useState<boolean>(false);
  79. const [
  80. isSelected,
  81. setIsSelected
  82. ] = useState<boolean>(false);
  83. useLayoutEffect(() => {
  84. navigation.setOptions({
  85. header: () => (
  86. <Header
  87. title={localize("radioButton-title")}
  88. isWrapSafeareaContext={false}
  89. navigation={navigation}
  90. isGoBackEnable={true}
  91. />
  92. ),
  93. headerShown: true
  94. });
  95. }, []);
  96. return (
  97. <PageContainer
  98. scrollViewProps={{
  99. contentContainerStyle: stylesheet.scrollContent
  100. }}
  101. scrollViewStyle={[stylesheet.container]}
  102. isWorkWithHeaderSpace={false}
  103. isScrollable={true}
  104. >
  105. <View style={[stylesheet.contentContainer]}>
  106. <Text
  107. variant="bodyMediumSize"
  108. color="mid"
  109. style={{
  110. marginBottom: 20
  111. }}
  112. >
  113. {localize("radioButton-desc")}
  114. </Text>
  115. <View
  116. style={{
  117. borderColor: colors.content.border.subtle,
  118. marginBottom: spaces.spacingMd,
  119. borderRadius: radiuses.form,
  120. borderWidth: borders.line,
  121. padding: spaces.spacingMd,
  122. justifyContent: "center",
  123. alignItems: "center",
  124. minHeight: 120
  125. }}
  126. >
  127. <RadioButton
  128. title={
  129. title || localize("radioButton-exampleTitle")
  130. }
  131. spreadBehaviour={SPREAD_BEHAVIOURS[spreadIndex]}
  132. onPress={() => setIsSelected(!isSelected)}
  133. type={TYPES[typeIndex]}
  134. isDisabled={isDisabled}
  135. isChecked={isSelected}
  136. isLoading={isLoading}
  137. />
  138. </View>
  139. <TextInput
  140. placeholder={localize("radioButton-enterTitle")}
  141. title={localize("radioButton-titleLabel")}
  142. style={{
  143. marginBottom: spaces.spacingMd
  144. }}
  145. spreadBehaviour="stretch"
  146. onChangeText={setTitle}
  147. value={title}
  148. />
  149. <View
  150. style={{
  151. justifyContent: "space-between",
  152. gap: spaces.spacingMd,
  153. flexDirection: "row",
  154. flexWrap: "wrap",
  155. width: "100%"
  156. }}
  157. >
  158. <SelectBox
  159. onChange={(selectedItems) => {
  160. if (selectedItems.length > 0) {
  161. const index = TYPES.indexOf(
  162. selectedItems[0]!
  163. .__key as unknown as IRadioButtonProps["type"],
  164. );
  165. if (index !== -1) setTypeIndex(index);
  166. }
  167. }}
  168. initialSelectedItems={
  169. TYPES_DATA[typeIndex] ? [TYPES_DATA[typeIndex]] : []
  170. }
  171. title={localize("radioButton-changeType")}
  172. titleExtractor={(item) => item.__title}
  173. keyExtractor={(item) => item.__key}
  174. spreadBehaviour="free"
  175. data={TYPES_DATA}
  176. />
  177. <Button
  178. onPress={() => {
  179. setSpreadIndex((prev) =>
  180. prev + 1 > SPREAD_BEHAVIOURS.length - 1
  181. ? 0
  182. : prev + 1,
  183. );
  184. }}
  185. title={
  186. `${localize("radioButton-changeSpread")}: ${SPREAD_BEHAVIOURS[spreadIndex]}`
  187. }
  188. spreadBehaviour="free"
  189. />
  190. <View
  191. style={{
  192. justifyContent: "space-between",
  193. flexDirection: "row",
  194. alignItems: "center",
  195. flexWrap: "wrap",
  196. width: "100%"
  197. }}
  198. >
  199. <Switch
  200. title={localize(
  201. "radioButton-toggleDisabled",
  202. )}
  203. onPress={() => setIsDisabled(!isDisabled)}
  204. isActive={isDisabled}
  205. />
  206. <Switch
  207. title={localize("radioButton-toggleLoading")}
  208. onPress={() => setIsLoading(!isLoading)}
  209. isActive={isLoading}
  210. />
  211. <Switch
  212. title={localize(
  213. "radioButton-toggleSelected",
  214. )}
  215. onPress={() => setIsSelected(!isSelected)}
  216. isActive={isSelected}
  217. />
  218. </View>
  219. </View>
  220. </View>
  221. </PageContainer>
  222. );
  223. };
  224. export default RadioButtonPage;