index.tsx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. STATUS_CHEAT_SHEET,
  12. NCoreUIKitTheme,
  13. PageContainer,
  14. CodeViewer,
  15. SelectBox,
  16. TextInput,
  17. Avatar,
  18. Header,
  19. Switch,
  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. ActivityStatusType,
  31. IAvatarProps
  32. } from "ncore-ui-kit";
  33. const AVATAR_SIZES: Array<IAvatarProps["size"]> = [
  34. "medium",
  35. "xLarge",
  36. "xSmall",
  37. "large",
  38. "small"
  39. ];
  40. const AVATAR_SIZES_DATA = AVATAR_SIZES.map((item) => ({
  41. __title: item as string,
  42. __key: item as string
  43. }));
  44. const AVATAR_STATUS_INDICATOR_TYPES = Object.keys(STATUS_CHEAT_SHEET) as Array<ActivityStatusType>;
  45. const AVATAR_STATUS_INDICATOR_TYPES_DATA = AVATAR_STATUS_INDICATOR_TYPES.map(
  46. (item) => ({
  47. __title: item as string,
  48. __key: item as string
  49. })
  50. );
  51. const AvatarPage = () => {
  52. const {
  53. radiuses,
  54. borders,
  55. spaces,
  56. colors
  57. } = NCoreUIKitTheme.useContext();
  58. const {
  59. localize
  60. } = NCoreUIKitLocalize.useContext();
  61. const navigation = useNavigation<NativeStackNavigationProp<RootStackParamList>>();
  62. const [
  63. statusIndicatorIndex,
  64. setStatusIndicatorIndex
  65. ] = useState<number>(1);
  66. const [
  67. sizeIndex,
  68. setSizeIndex
  69. ] = useState<number>(3);
  70. const [
  71. isDisabled,
  72. setIsDisabled
  73. ] = useState<boolean>(false);
  74. const [
  75. isLoading,
  76. setIsLoading
  77. ] = useState<boolean>(false);
  78. const [
  79. imageUrl,
  80. setImageUrl
  81. ] = useState<string>(
  82. "https://ncore.nibgat.space/assets/images/ncorelogo.png",
  83. );
  84. const [
  85. title,
  86. setTitle
  87. ] = useState<string>("John Doe");
  88. useLayoutEffect(() => {
  89. navigation.setOptions({
  90. header: () => <Header
  91. title={localize("avatar-title")}
  92. isWrapSafeareaContext={false}
  93. navigation={navigation}
  94. isGoBackEnable={true}
  95. />,
  96. headerShown: true
  97. });
  98. }, []);
  99. return <PageContainer
  100. scrollViewProps={{
  101. contentContainerStyle: stylesheet.scrollContent
  102. }}
  103. isWorkWithHeaderSpace={false}
  104. scrollViewStyle={[
  105. stylesheet.container
  106. ]}
  107. isScrollable={true}
  108. >
  109. <View
  110. style={[
  111. stylesheet.contentContainer
  112. ]}
  113. >
  114. <Text
  115. style={stylesheet.descText}
  116. variant="bodyMediumSize"
  117. color="mid"
  118. >
  119. {localize("avatar-desc")}
  120. </Text>
  121. <View
  122. style={[
  123. stylesheet.previewContainer,
  124. {
  125. borderColor: colors.content.border.subtle,
  126. marginBottom: spaces.spacingMd,
  127. borderRadius: radiuses.form,
  128. borderWidth: borders.line,
  129. padding: spaces.spacingMd
  130. }
  131. ]}
  132. >
  133. <Avatar
  134. statusIndicatorType={
  135. AVATAR_STATUS_INDICATOR_TYPES[statusIndicatorIndex]
  136. }
  137. size={AVATAR_SIZES[sizeIndex]}
  138. isStatusIndicator={true}
  139. isDisabled={isDisabled}
  140. isLoading={isLoading}
  141. imageUrl={imageUrl}
  142. title={title}
  143. />
  144. </View>
  145. <View
  146. style={{
  147. marginBottom: spaces.spacingMd
  148. }}
  149. >
  150. <Text
  151. style={{
  152. marginBottom: spaces.spacingSm
  153. }}
  154. variant="headlineSmallSize"
  155. >
  156. {localize("usage")}
  157. </Text>
  158. <CodeViewer
  159. code={"import {\n Avatar\n} from \"ncore-ui-kit\";\n\n<Avatar\n title=\"John Doe\"\n imageUrl=\"https://ncore.nibgat.space/assets/images/ncorelogo.png\"\n statusIndicatorType=\"online\"\n isStatusIndicator={true}\n size=\"large\"\n/>"}
  160. language="tsx"
  161. />
  162. </View>
  163. <TextInput
  164. placeholder={localize("avatar-enterImageUrl")}
  165. title={localize("avatar-imageUrl")}
  166. style={{
  167. marginBottom: spaces.spacingMd
  168. }}
  169. onChangeText={setImageUrl}
  170. spreadBehaviour="stretch"
  171. value={imageUrl}
  172. />
  173. <TextInput
  174. placeholder={localize("avatar-enterText")}
  175. title={localize("avatar-titleLabel")}
  176. style={{
  177. marginBottom: spaces.spacingMd
  178. }}
  179. spreadBehaviour="stretch"
  180. onChangeText={setTitle}
  181. value={title}
  182. />
  183. <View
  184. style={[
  185. stylesheet.rowContainer,
  186. {
  187. gap: spaces.spacingMd
  188. }
  189. ]}
  190. >
  191. <SelectBox
  192. onChange={(selectedItems) => {
  193. if (selectedItems.length > 0) {
  194. const index = AVATAR_SIZES.indexOf(
  195. selectedItems[0]!
  196. .__key as IAvatarProps["size"],
  197. );
  198. if (index !== -1) setSizeIndex(index);
  199. }
  200. }}
  201. initialSelectedItems={
  202. AVATAR_SIZES_DATA[sizeIndex]
  203. ? [
  204. AVATAR_SIZES_DATA[sizeIndex]!
  205. ]
  206. : []
  207. }
  208. titleExtractor={(item) => item.__title}
  209. keyExtractor={(item) => item.__key}
  210. title={localize("change-size")}
  211. data={AVATAR_SIZES_DATA}
  212. spreadBehaviour="free"
  213. />
  214. <SelectBox
  215. onChange={(selectedItems) => {
  216. if (selectedItems.length > 0) {
  217. const index =
  218. AVATAR_STATUS_INDICATOR_TYPES.indexOf(
  219. selectedItems[0]!
  220. .__key as ActivityStatusType,
  221. );
  222. if (index !== -1)
  223. setStatusIndicatorIndex(index);
  224. }
  225. }}
  226. initialSelectedItems={
  227. AVATAR_STATUS_INDICATOR_TYPES_DATA[
  228. statusIndicatorIndex
  229. ]
  230. ? [
  231. AVATAR_STATUS_INDICATOR_TYPES_DATA[
  232. statusIndicatorIndex
  233. ]!
  234. ]
  235. : []
  236. }
  237. data={AVATAR_STATUS_INDICATOR_TYPES_DATA}
  238. title={localize("avatar-changeStatus")}
  239. titleExtractor={(item) => item.__title}
  240. keyExtractor={(item) => item.__key}
  241. spreadBehaviour="free"
  242. />
  243. <View
  244. style={stylesheet.switchesContainer}
  245. >
  246. <Switch
  247. title={localize("avatar-toggleLoading")}
  248. onPress={() => {
  249. setIsLoading(!isLoading);
  250. }}
  251. spreadBehaviour="free"
  252. isActive={isLoading}
  253. />
  254. <Switch
  255. title={localize("avatar-toggleDisabled")}
  256. onPress={() => {
  257. setIsDisabled(!isDisabled);
  258. }}
  259. spreadBehaviour="free"
  260. isActive={isDisabled}
  261. />
  262. </View>
  263. </View>
  264. </View>
  265. </PageContainer>;
  266. };
  267. export default AvatarPage;