index.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. Dialog,
  18. Header,
  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. IDialogProps
  30. } from "ncore-ui-kit";
  31. const DIALOG_VARIANTS: Array<IDialogProps["variant"]> = [
  32. "yes-no",
  33. "ok",
  34. "info"
  35. ];
  36. const DIALOG_VARIANTS_DATA = DIALOG_VARIANTS.map((item) => ({
  37. __title: item as string,
  38. __key: item as string
  39. }));
  40. const DialogPage = () => {
  41. const {
  42. radiuses,
  43. borders,
  44. spaces,
  45. colors
  46. } = NCoreUIKitTheme.useContext();
  47. const {
  48. localize
  49. } = NCoreUIKitLocalize.useContext();
  50. const navigation = useNavigation<NativeStackNavigationProp<RootStackParamList>>();
  51. const [
  52. variantIndex,
  53. setVariantIndex
  54. ] = useState<number>(0);
  55. const [
  56. isVisible,
  57. setIsVisible
  58. ] = useState<boolean>(false);
  59. const [
  60. content,
  61. setContent
  62. ] = useState<string>("This is a dialog content.");
  63. const [
  64. title,
  65. setTitle
  66. ] = useState<string>("Dialog Title");
  67. useLayoutEffect(() => {
  68. navigation.setOptions({
  69. header: () => <Header
  70. title={localize("dialog-title")}
  71. isWrapSafeareaContext={false}
  72. navigation={navigation}
  73. isGoBackEnable={true}
  74. />,
  75. headerShown: true
  76. });
  77. }, []);
  78. return <PageContainer
  79. scrollViewProps={{
  80. contentContainerStyle: stylesheet.scrollContent
  81. }}
  82. isWorkWithHeaderSpace={false}
  83. scrollViewStyle={[
  84. stylesheet.container
  85. ]}
  86. isScrollable={true}
  87. >
  88. <View
  89. style={[
  90. stylesheet.contentContainer
  91. ]}
  92. >
  93. <Text
  94. style={stylesheet.descText}
  95. variant="bodyMediumSize"
  96. color="mid"
  97. >
  98. {localize("dialog-desc")}
  99. </Text>
  100. <View
  101. style={[
  102. stylesheet.previewContainer,
  103. {
  104. borderColor: colors.content.border.subtle,
  105. marginBottom: spaces.spacingMd,
  106. borderRadius: radiuses.form,
  107. borderWidth: borders.line,
  108. padding: spaces.spacingMd
  109. }
  110. ]}
  111. >
  112. <Button
  113. title={localize("dialog-openDialog")}
  114. onPress={() => setIsVisible(true)}
  115. />
  116. </View>
  117. <View
  118. style={{
  119. marginBottom: spaces.spacingMd
  120. }}
  121. >
  122. <Text
  123. style={{
  124. marginBottom: spaces.spacingSm
  125. }}
  126. variant="headlineSmallSize"
  127. >
  128. {localize("usage")}
  129. </Text>
  130. <CodeViewer
  131. code={"import {\n Dialog\n} from \"ncore-ui-kit\";\n\n<Dialog\n title=\"Dialog Title\"\n content=\"This is a dialog content.\"\n isVisible={isVisible}\n variant=\"yes-no\"\n/>"}
  132. language="tsx"
  133. />
  134. </View>
  135. <TextInput
  136. placeholder={localize("dialog-enterText")}
  137. title={localize("dialog-titleLabel")}
  138. style={{
  139. marginBottom: spaces.spacingMd
  140. }}
  141. spreadBehaviour="stretch"
  142. onChangeText={setTitle}
  143. value={title}
  144. />
  145. <TextInput
  146. placeholder={localize("dialog-enterContentText")}
  147. title={localize("dialog-contentLabel")}
  148. style={{
  149. marginBottom: spaces.spacingMd
  150. }}
  151. spreadBehaviour="stretch"
  152. onChangeText={setContent}
  153. value={content}
  154. />
  155. <View
  156. style={[
  157. stylesheet.rowContainer,
  158. {
  159. gap: spaces.spacingMd
  160. }
  161. ]}
  162. >
  163. <SelectBox
  164. onChange={(selectedItems) => {
  165. if (selectedItems.length > 0) {
  166. const index = DIALOG_VARIANTS.indexOf(
  167. selectedItems[0]!
  168. .__key as IDialogProps["variant"],
  169. );
  170. if (index !== -1) setVariantIndex(index);
  171. }
  172. }}
  173. initialSelectedItems={
  174. DIALOG_VARIANTS_DATA[variantIndex]
  175. ? [
  176. DIALOG_VARIANTS_DATA[variantIndex]!
  177. ]
  178. : []
  179. }
  180. title={localize("dialog-changeVariant")}
  181. titleExtractor={(item) => item.__title}
  182. keyExtractor={(item) => item.__key}
  183. data={DIALOG_VARIANTS_DATA}
  184. spreadBehaviour="free"
  185. />
  186. </View>
  187. </View>
  188. <Dialog
  189. variant={DIALOG_VARIANTS[variantIndex]}
  190. secondaryButtonProps={{
  191. onPress: ({
  192. closeAnimation
  193. }) => {
  194. closeAnimation();
  195. }
  196. }}
  197. primaryButtonProps={{
  198. onPress: ({
  199. closeAnimation
  200. }) => {
  201. closeAnimation();
  202. }
  203. }}
  204. onClosed={() => {
  205. setIsVisible(false);
  206. }}
  207. onOverlayPress={({
  208. closeAnimation
  209. }) => {
  210. closeAnimation();
  211. }}
  212. isVisible={isVisible}
  213. content={content}
  214. title={title}
  215. />
  216. </PageContainer>;
  217. };
  218. export default DialogPage;