index.tsx 8.6 KB

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