From 64ab6e4247fca39a104853e720a1d16dbf6f3ebf Mon Sep 17 00:00:00 2001 From: Vut Pov Date: Tue, 14 Jul 2020 16:04:58 +0700 Subject: [PATCH] init lib --- .idea/.gitignore | 8 +++ .idea/base-context-provider.iml | 9 ++++ .idea/codeStyles/Project.xml | 40 ++++++++++++++ .idea/codeStyles/codeStyleConfig.xml | 5 ++ .idea/inspectionProfiles/Project_Default.xml | 6 +++ .idea/misc.xml | 9 ++++ .idea/modules.xml | 8 +++ .idea/vcs.xml | 6 +++ .prettierrc => .prettierrc.json | 0 README.md | 21 -------- example/package.json | 4 +- example/src/App.tsx | 11 ++-- example/src/Button.tsx | 15 ++++++ example/src/TestContext.ts | 33 ++++++++++++ example/src/index.tsx | 1 - example/tsconfig.json | 24 +++++++-- example/yarn.lock | 23 +++++--- package.json | 5 +- src/index.test.tsx | 7 --- src/index.tsx | 57 +++++++++++++++++--- src/styles.module.css | 9 ---- src/typings.d.ts | 17 ------ yarn.lock | 31 +---------- 23 files changed, 239 insertions(+), 110 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/base-context-provider.iml create mode 100644 .idea/codeStyles/Project.xml create mode 100644 .idea/codeStyles/codeStyleConfig.xml create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml rename .prettierrc => .prettierrc.json (100%) create mode 100644 example/src/Button.tsx create mode 100644 example/src/TestContext.ts delete mode 100644 src/index.test.tsx delete mode 100644 src/styles.module.css delete mode 100644 src/typings.d.ts diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..73f69e0 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/base-context-provider.iml b/.idea/base-context-provider.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/base-context-provider.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..8ce5817 --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,40 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..79ee123 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..03d9549 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..7f2b2ff --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,9 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..cf3862f --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.prettierrc b/.prettierrc.json similarity index 100% rename from .prettierrc rename to .prettierrc.json diff --git a/README.md b/README.md index 95533a8..49638b6 100644 --- a/README.md +++ b/README.md @@ -4,27 +4,6 @@ [![NPM](https://img.shields.io/npm/v/base-context-provider.svg)](https://www.npmjs.com/package/base-context-provider) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com) -## Install - -```bash -npm install --save base-context-provider -``` - -## Usage - -```tsx -import React, { Component } from 'react' - -import MyComponent from 'base-context-provider' -import 'base-context-provider/dist/index.css' - -class Example extends Component { - render() { - return - } -} -``` - ## License MIT © [vuthpov](https://github.com/vuthpov) diff --git a/example/package.json b/example/package.json index 1107f07..d11e773 100644 --- a/example/package.json +++ b/example/package.json @@ -17,8 +17,8 @@ "@types/node": "link:../node_modules/@types/node", "@types/react": "link:../node_modules/@types/react", "@types/react-dom": "link:../node_modules/@types/react-dom", - "react": "link:../node_modules/react", - "react-dom": "link:../node_modules/react-dom", + "react": "^16.13.1", + "react-dom": "^16.13.1", "react-scripts": "link:../node_modules/react-scripts", "typescript": "link:../node_modules/typescript", "base-context-provider": "link:.." diff --git a/example/src/App.tsx b/example/src/App.tsx index 5046006..1a404b2 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -1,10 +1,11 @@ import React from 'react' - -import { ExampleComponent } from 'base-context-provider' -import 'base-context-provider/dist/index.css' +import {wrapTestProvider} from './TestContext' +import Button from './Button' const App = () => { - return + return
hello +
} -export default App +export default wrapTestProvider(App) diff --git a/example/src/Button.tsx b/example/src/Button.tsx new file mode 100644 index 0000000..2a10969 --- /dev/null +++ b/example/src/Button.tsx @@ -0,0 +1,15 @@ +import React from 'react' +import { useTestContext, withTestContext } from './TestContext' + +const Button = (props: any) => { + const {punishmentLevel} = props.testContextProps + const { heat }: any = useTestContext() + return ( +
+ heat: {heat}
+ levels: {punishmentLevel} +
+ ) +} + +export default withTestContext(Button) diff --git a/example/src/TestContext.ts b/example/src/TestContext.ts new file mode 100644 index 0000000..a2c54ee --- /dev/null +++ b/example/src/TestContext.ts @@ -0,0 +1,33 @@ +import React from 'react' +import BaseContextProvider, { + baseContextWrap, + baseUseContext, + baseWithContext +} from 'base-context-provider' + +const Context = React.createContext({}) + +class TestContextProvider extends BaseContextProvider { + state = { + heat: 5, + punishmentLevel: 10 + } + + getContextReturnValue() { + return { + ...this.state + } + } + + getContext() { + return Context + } +} + +export const useTestContext = baseUseContext(Context) + +export const withTestContext = baseWithContext(Context, 'testContextProps') + +export const wrapTestProvider = baseContextWrap(TestContextProvider) + +export default TestContextProvider diff --git a/example/src/index.tsx b/example/src/index.tsx index 90b6253..4d2d4b0 100644 --- a/example/src/index.tsx +++ b/example/src/index.tsx @@ -1,5 +1,4 @@ import './index.css' - import React from 'react' import ReactDOM from 'react-dom' import App from './App' diff --git a/example/tsconfig.json b/example/tsconfig.json index ec13f6f..c4063c7 100644 --- a/example/tsconfig.json +++ b/example/tsconfig.json @@ -2,7 +2,10 @@ "compilerOptions": { "outDir": "dist", "module": "esnext", - "lib": ["dom", "esnext"], + "lib": [ + "dom", + "esnext" + ], "moduleResolution": "node", "jsx": "react", "sourceMap": true, @@ -15,8 +18,21 @@ "suppressImplicitAnyIndexErrors": true, "noUnusedLocals": true, "noUnusedParameters": true, - "allowSyntheticDefaultImports": true + "allowSyntheticDefaultImports": true, + "target": "es5", + "allowJs": true, + "skipLibCheck": true, + "strict": true, + "forceConsistentCasingInFileNames": true, + "resolveJsonModule": true, + "isolatedModules": true, + "noEmit": true }, - "include": ["src"], - "exclude": ["node_modules", "build"] + "include": [ + "src" + ], + "exclude": [ + "node_modules", + "build" + ] } diff --git a/example/yarn.lock b/example/yarn.lock index cc8f62c..70c150f 100644 --- a/example/yarn.lock +++ b/example/yarn.lock @@ -8687,9 +8687,15 @@ react-dev-utils@^10.2.1: strip-ansi "6.0.0" text-table "0.2.0" -"react-dom@link:../node_modules/react-dom": - version "0.0.0" - uid "" +react-dom@^16.13.1: + version "16.13.1" + resolved "https://npm-registry.cubetiqs.com/react-dom/-/react-dom-16.13.1.tgz#c1bd37331a0486c078ee54c4740720993b2e0e7f" + integrity sha512-81PIMmVLnCNLO/fFOQxdQkvEq/+Hfpv24XNJfpyZhTRfO0QcmQIF/PgCa1zCOj2w1hrn12MFLyaJ/G0+Mxtfag== + dependencies: + loose-envify "^1.1.0" + object-assign "^4.1.1" + prop-types "^15.6.2" + scheduler "^0.19.1" react-error-overlay@^6.0.7: version "6.0.7" @@ -8705,9 +8711,14 @@ react-is@^16.12.0, react-is@^16.8.1, react-is@^16.8.4: version "0.0.0" uid "" -"react@link:../node_modules/react": - version "0.0.0" - uid "" +react@^16.13.1: + version "16.13.1" + resolved "https://npm-registry.cubetiqs.com/react/-/react-16.13.1.tgz#2e818822f1a9743122c063d6410d85c1e3afe48e" + integrity sha512-YMZQQq32xHLX0bz5Mnibv1/LHb3Sqzngu7xstSM+vrkE5Kzr9xE0yMByK5kMoTK30YVJE61WfbxIFFvfeDKT1w== + dependencies: + loose-envify "^1.1.0" + object-assign "^4.1.1" + prop-types "^15.6.2" read-pkg-up@^2.0.0: version "2.0.0" diff --git a/package.json b/package.json index 4207be9..3da75a1 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,8 @@ "deploy": "gh-pages -d example/build" }, "peerDependencies": { - "react": "^16.0.0" + "react": "^16.0.0", + "react-dom": "^16.0.0" }, "devDependencies": { "@testing-library/jest-dom": "^4.2.4", @@ -52,8 +53,6 @@ "gh-pages": "^2.2.0", "npm-run-all": "^4.1.5", "prettier": "^2.0.4", - "react": "^16.13.1", - "react-dom": "^16.13.1", "react-scripts": "^3.4.1", "typescript": "^3.7.5" }, diff --git a/src/index.test.tsx b/src/index.test.tsx deleted file mode 100644 index a0f0449..0000000 --- a/src/index.test.tsx +++ /dev/null @@ -1,7 +0,0 @@ -import { ExampleComponent } from '.' - -describe('ExampleComponent', () => { - it('is truthy', () => { - expect(ExampleComponent).toBeTruthy() - }) -}) diff --git a/src/index.tsx b/src/index.tsx index c06ff11..7412043 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,10 +1,55 @@ -import * as React from 'react' -import styles from './styles.module.css' +import React from 'react' -interface Props { - text: string +const BaseContext = React.createContext({}) + +/** + * Component : BaseContextProvider + * + * @author sl + * @time 13/2/20 + * @param {{}} + * @constructor + * + */ +class BaseContextProvider extends React.Component { + getContextReturnValue() { + return {} + } + + getContext() { + return BaseContext + } + + render() { + const contextReturn = this.getContextReturnValue() + const Context = this.getContext() + const { children } = this.props + + return {children} + } } -export const ExampleComponent = ({ text }: Props) => { - return
Example Component: {text}
+export const baseContextWrap = (Provider: any) => { + return (Component: any) => (props: any) => ( + } /> + ) } + +export function baseUseContext(context: any) { + return function () { + return React.useContext(context) + } +} + +export function baseWithContext(context: any, contextPassPropKey: any) { + const Consumer = context.Consumer + return (Component: any) => (props: any) => ( + + {(contextValue: any) => ( + + )} + + ) +} + +export default BaseContextProvider diff --git a/src/styles.module.css b/src/styles.module.css deleted file mode 100644 index 41006b5..0000000 --- a/src/styles.module.css +++ /dev/null @@ -1,9 +0,0 @@ -/* add css module styles here (optional) */ - -.test { - margin: 2em; - padding: 0.5em; - border: 2px solid #000; - font-size: 2em; - text-align: center; -} diff --git a/src/typings.d.ts b/src/typings.d.ts deleted file mode 100644 index cd16102..0000000 --- a/src/typings.d.ts +++ /dev/null @@ -1,17 +0,0 @@ -/** - * Default CSS definition for typescript, - * will be overridden with file-specific definitions by rollup - */ -declare module '*.css' { - const content: { [className: string]: string }; - export default content; -} - -interface SvgrComponent extends React.StatelessComponent> {} - -declare module '*.svg' { - const svgUrl: string; - const svgComponent: SvgrComponent; - export default svgUrl; - export { svgComponent as ReactComponent } -} diff --git a/yarn.lock b/yarn.lock index 8278e4b..194f0b9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7200,7 +7200,7 @@ loglevel@^1.6.6: resolved "https://npm-registry.cubetiqs.com/loglevel/-/loglevel-1.6.8.tgz#8a25fb75d092230ecd4457270d80b54e28011171" integrity sha512-bsU7+gc9AJ2SqpzxwU3+1fedl8zAntbtC5XYlt3s2j1hJcn2PsXSmgN8TaLG/J1/2mod4+cE/3vNL70/c1RNCA== -loose-envify@^1.0.0, loose-envify@^1.1.0, loose-envify@^1.4.0: +loose-envify@^1.0.0, loose-envify@^1.4.0: version "1.4.0" resolved "https://npm-registry.cubetiqs.com/loose-envify/-/loose-envify-1.4.0.tgz#71ee51fa7be4caec1a63839f7e682d8132d30caf" integrity sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q== @@ -9265,7 +9265,7 @@ prompts@^2.0.1: kleur "^3.0.3" sisteransi "^1.0.4" -prop-types@^15.6.2, prop-types@^15.7.2: +prop-types@^15.7.2: version "15.7.2" resolved "https://npm-registry.cubetiqs.com/prop-types/-/prop-types-15.7.2.tgz#52c41e75b8c87e72b9d9360e0206b99dcbffa6c5" integrity sha512-8QQikdH7//R2vurIJSutZ1smHYTcLpRWEOlHnzcWHmBYrOGUysKwSsrC89BCiFj3CbrfJ/nXFdJepOVrY1GCHQ== @@ -9461,16 +9461,6 @@ react-dev-utils@^10.2.1: strip-ansi "6.0.0" text-table "0.2.0" -react-dom@^16.13.1: - version "16.13.1" - resolved "https://npm-registry.cubetiqs.com/react-dom/-/react-dom-16.13.1.tgz#c1bd37331a0486c078ee54c4740720993b2e0e7f" - integrity sha512-81PIMmVLnCNLO/fFOQxdQkvEq/+Hfpv24XNJfpyZhTRfO0QcmQIF/PgCa1zCOj2w1hrn12MFLyaJ/G0+Mxtfag== - dependencies: - loose-envify "^1.1.0" - object-assign "^4.1.1" - prop-types "^15.6.2" - scheduler "^0.19.1" - react-error-overlay@^6.0.7: version "6.0.7" resolved "https://npm-registry.cubetiqs.com/react-error-overlay/-/react-error-overlay-6.0.7.tgz#1dcfb459ab671d53f660a991513cb2f0a0553108" @@ -9541,15 +9531,6 @@ react-scripts@^3.4.1: optionalDependencies: fsevents "2.1.2" -react@^16.13.1: - version "16.13.1" - resolved "https://npm-registry.cubetiqs.com/react/-/react-16.13.1.tgz#2e818822f1a9743122c063d6410d85c1e3afe48e" - integrity sha512-YMZQQq32xHLX0bz5Mnibv1/LHb3Sqzngu7xstSM+vrkE5Kzr9xE0yMByK5kMoTK30YVJE61WfbxIFFvfeDKT1w== - dependencies: - loose-envify "^1.1.0" - object-assign "^4.1.1" - prop-types "^15.6.2" - read-pkg-up@^2.0.0: version "2.0.0" resolved "https://npm-registry.cubetiqs.com/read-pkg-up/-/read-pkg-up-2.0.0.tgz#6b72a8048984e0c41e79510fd5e9fa99b3b549be" @@ -10153,14 +10134,6 @@ saxes@^3.1.9: dependencies: xmlchars "^2.1.1" -scheduler@^0.19.1: - version "0.19.1" - resolved "https://npm-registry.cubetiqs.com/scheduler/-/scheduler-0.19.1.tgz#4f3e2ed2c1a7d65681f4c854fa8c5a1ccb40f196" - integrity sha512-n/zwRWRYSUj0/3g/otKDRPMh6qv2SYMWNq85IEa8iZyAv8od9zDYpGSnpBEjNgcMNq6Scbu5KfIPxNF72R/2EA== - dependencies: - loose-envify "^1.1.0" - object-assign "^4.1.1" - schema-utils@^1.0.0: version "1.0.0" resolved "https://npm-registry.cubetiqs.com/schema-utils/-/schema-utils-1.0.0.tgz#0b79a93204d7b600d4b2850d1f66c2a34951c770"