quick-start.mdx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import { Meta } from '@storybook/addon-docs/blocks';
  2. <Meta title="Docs/Getting Started/Quick Start" />
  3. # Getting Started
  4. ## Installing
  5. You can install NCore Context (ncore-context) with [NPM](https://www.npmjs.com/package/ncore-context) or Yarn.
  6. - NPM: `npm install ncore-context`
  7. - YARN: `yarn add ncore-context`
  8. ## Configuration
  9. You must wrap with NCoreProvider your outermost component. This is important for tools such as theme and localization to be active.
  10. NCoreProvider allows with a prop named "configs" you to make changes to themes and language packs and select initial theme and initial language.
  11. ```tsx
  12. import {
  13. type ReactNode
  14. } from "react";
  15. import {
  16. type NCoreUIKitConfig
  17. } from "../types";
  18. class CoreContext {
  19. NCoreUIKitDialog: NCoreUIKitDialog;
  20. constructor() {
  21. this.NCoreUIKitDialog = new NCoreUIKitDialog({
  22. data: []
  23. });
  24. }
  25. Provider = ({
  26. children
  27. }: {
  28. children: ReactNode
  29. }) => {
  30. const DialogContext = this.NCoreUIKitDialog;
  31. return <DialogContext.Provider>
  32. <DialogContext.Render>
  33. {children}
  34. </DialogContext.Render>
  35. </DialogContext.Provider>
  36. };
  37. };
  38. export default CoreContext;
  39. ```
  40. ```tsx
  41. import {
  42. type ReactNode,
  43. Fragment
  44. } from "react";
  45. import {
  46. type DialogContextType,
  47. type DialogDataType
  48. } from "../types/dialog";
  49. import NCoreContext, {
  50. type ConfigType
  51. } from "ncore-context";
  52. import Dialog from "../components/dialog";
  53. import {
  54. uuid
  55. } from "../utils";
  56. class NCoreUIKitDialog extends NCoreContext<DialogContextType, ConfigType<DialogContextType>> {
  57. constructor({
  58. data = []
  59. }: {
  60. data?: Array<DialogDataType>
  61. }) {
  62. super({
  63. close: () => {},
  64. open: () => "",
  65. data: data
  66. }, {
  67. key: "NCoreUIKit-DialogContext"
  68. });
  69. };
  70. open = (dialogData: DialogDataType) => {
  71. const currentData = this.state.data;
  72. const dialogID = dialogData.id ? dialogData.id : uuid();
  73. currentData.push({
  74. ...dialogData,
  75. id: dialogID
  76. });
  77. this.setState({
  78. data: currentData
  79. });
  80. return dialogID;
  81. };
  82. close = (props?: {
  83. index?: number;
  84. id?: string;
  85. }) => {
  86. const currentData = this.state.data;
  87. if (props && props.id) {
  88. const keyIndex = currentData.findIndex((dialog) => dialog.id === props.id);
  89. if (keyIndex !== -1) {
  90. currentData.splice(keyIndex, 1);
  91. this.setState({
  92. data: currentData
  93. });
  94. }
  95. return;
  96. }
  97. if (props && props.index !== undefined) {
  98. currentData.splice(props.index, 1);
  99. this.setState({
  100. data: currentData
  101. });
  102. return;
  103. }
  104. currentData.pop();
  105. this.setState({
  106. data: currentData
  107. });
  108. };
  109. Render = ({
  110. children
  111. }: {
  112. children: ReactNode;
  113. }) => {
  114. const {
  115. data
  116. } = this.useContext();
  117. return <Fragment>
  118. {children}
  119. {data.map((item: DialogDataType) => {
  120. return <Dialog
  121. key={`NCoreUIKit-Dialog-${item.id}`}
  122. id={item.id as string}
  123. onClosed={() => {
  124. if(item.onClosed) {
  125. item.onClosed({
  126. id: item.id as string
  127. });
  128. }
  129. if(item.isAutoClosed === undefined || item.isAutoClosed === true) {
  130. this.close({
  131. id: item.id
  132. });
  133. }
  134. }}
  135. {...item}
  136. />;
  137. })}
  138. </Fragment>;
  139. };
  140. }
  141. export default NCoreUIKitDialog;
  142. ```
  143. If you want to set config, please look at Configs and Schemes Pages.