diff --git a/.commitlintrc.js b/.commitlintrc.js deleted file mode 100644 index dd5c79b..0000000 --- a/.commitlintrc.js +++ /dev/null @@ -1,23 +0,0 @@ -module.exports = { - extends: ['@commitlint/config-conventional'], - rules: { - 'subject-case': [0, 'never'], - 'type-enum': [ - 2, - 'always', - [ - 'build', // 构建 - 'ci', // ci - 'chore', // Other changes that don't modify src or test files. 改变构建流程、或者增加依赖库、工具等 - 'docs', // Adds or alters documentation. 仅仅修改了文档,比如README, CHANGELOG, CONTRIBUTE等等 - 'feat', // Adds a new feature. 新增feature - 'fix', // Solves a bug. 修复bug - 'perf', // Improves performance. 优化相关,比如提升性能、体验 - 'refactor', // Rewrites code without feature, performance or bug changes. 代码重构,没有加新功能或者修复bug - 'revert', // Reverts a previous commit. 回滚到上一个版本 - 'style', // Improves formatting, white-space. 仅仅修改了空格、格式缩进、逗号等等,不改变代码逻辑 - 'test', // Adds or modifies tests. 测试用例,包括单元测试、集成测试等 - ], - ], - }, -}; diff --git a/.github/workflows/doc.yml b/.github/workflows/doc.yml deleted file mode 100644 index e59217e..0000000 --- a/.github/workflows/doc.yml +++ /dev/null @@ -1,29 +0,0 @@ -name: Deploy Doc - -on: - push: - branches: [master] - -jobs: - deploy: - runs-on: ubuntu-latest - - strategy: - matrix: - node-version: [12.x] - - steps: - - uses: actions/checkout@v2 - - name: Build - uses: actions/setup-node@v1 - with: - node-version: ${{ matrix.node-version }} - - run: yarn - - run: yarn doc:build - env: - CI: true - - name: Deploy - uses: peaceiris/actions-gh-pages@v3 - with: - github_token: ${{ secrets.ACCESS_TOKEN }} - publish_dir: ./dist diff --git a/.gitignore b/.gitignore deleted file mode 100644 index cc40be8..0000000 --- a/.gitignore +++ /dev/null @@ -1,69 +0,0 @@ -# Logs -logs -*.log -npm-debug.log* -yarn-debug.log* -yarn-error.log* - -# Runtime data -pids -*.pid -*.seed -*.pid.lock - -# Directory for instrumented libs generated by jscoverage/JSCover -lib-cov - -# Coverage directory used by tools like istanbul -coverage - -# nyc test coverage -.nyc_output - -# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) -.grunt - -# Bower dependency directory (https://bower.io/) -bower_components - -# node-waf configuration -.lock-wscript - -# Compiled binary addons (https://nodejs.org/api/addons.html) -build/Release - -# Dependency directories -node_modules/ -jspm_packages/ - -# TypeScript v1 declaration files -typings/ - -# Optional npm cache directory -.npm - -# Optional eslint cache -.eslintcache - -# Optional REPL history -.node_repl_history - -# Output of 'npm pack' -*.tgz - -# Yarn Integrity file -.yarn-integrity - -# dotenv environment variables file -.env - -# next.js build output -.next - -# build assets -packages/doc/.docz -.DS_Store -/.umi -/.umi-production -dist/ -packages/*/dist diff --git a/.husky/.gitignore b/.husky/.gitignore deleted file mode 100644 index 31354ec..0000000 --- a/.husky/.gitignore +++ /dev/null @@ -1 +0,0 @@ -_ diff --git a/.husky/commit-msg b/.husky/commit-msg deleted file mode 100755 index 392029e..0000000 --- a/.husky/commit-msg +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh -. "$(dirname "$0")/_/husky.sh" - -npx --no-install commitlint --edit && yarn test:react-hooks diff --git a/.husky/pre-commit b/.husky/pre-commit deleted file mode 100755 index 81d8d34..0000000 --- a/.husky/pre-commit +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh -. "$(dirname "$0")/_/husky.sh" - -lerna run --concurrency 1 --stream lint-staged --since HEAD --exclude-dependents diff --git a/packages/react-designer/mock/.gitkeep b/.nojekyll similarity index 100% rename from packages/react-designer/mock/.gitkeep rename to .nojekyll diff --git a/.umirc.ts b/.umirc.ts deleted file mode 100644 index 0ee6345..0000000 --- a/.umirc.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { defineConfig } from 'dumi'; - -export default defineConfig({ - title: 'Qiaoyuwen Core', - mode: 'site', - history: { - type: "hash", - }, - publicPath: process.env.CI ? "/qiaoyuwen-core/" : "/", - // more config: https://d.umijs.org/config -}); diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 4e2def7..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "eslint.workingDirectories": [ - "packages/docs", - "packages/react-hooks", - ], - "eslint.packageManager": "yarn", -} \ No newline at end of file diff --git a/.yarnrc b/.yarnrc deleted file mode 100644 index f4074f5..0000000 --- a/.yarnrc +++ /dev/null @@ -1 +0,0 @@ -registry "https://registry.npm.taobao.org" \ No newline at end of file diff --git a/404.html b/404.html new file mode 100644 index 0000000..3e65765 --- /dev/null +++ b/404.html @@ -0,0 +1,33 @@ + + + + + + + + + + + +
+ + + + diff --git a/README.md b/README.md deleted file mode 100644 index d71864a..0000000 --- a/README.md +++ /dev/null @@ -1,8 +0,0 @@ -# Qiaoyuwen Core - -## React Hooks -一些常用的hooks的实现 - -## 文档地址 - -https://qiaoyuwen.github.io/qiaoyuwen-core/ diff --git a/docs/index.md b/docs/index.md deleted file mode 100644 index 23f5f0f..0000000 --- a/docs/index.md +++ /dev/null @@ -1,6 +0,0 @@ ---- -hero: - title: Qiaoyuwen Core - desc: 常用核心库 -footer: Open-source MIT Licensed | Copyright © 2020
Powered by [dumi](https://d.umijs.org) ---- diff --git a/index.html b/index.html new file mode 100644 index 0000000..3e65765 --- /dev/null +++ b/index.html @@ -0,0 +1,33 @@ + + + + + + + + + + + +
+ + + + diff --git a/lerna.json b/lerna.json deleted file mode 100644 index 82f5d3e..0000000 --- a/lerna.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "packages": [ - "packages/*" - ], - "version": "independent", - "npmClient": "yarn", - "useWorkspaces": true -} diff --git a/package.json b/package.json deleted file mode 100644 index 76af774..0000000 --- a/package.json +++ /dev/null @@ -1,36 +0,0 @@ -{ - "name": "@qiaoyuwen/core", - "private": true, - "workspaces": [ - "packages/*" - ], - "scripts": { - "doc:start": "dumi dev", - "doc:build": "dumi build", - "lint:react-hooks": "yarn workspace @qiaoyuwen/react-hooks run lint-staged", - "test:react-hooks": "yarn workspace @qiaoyuwen/react-hooks run test", - "build:react-hooks": "yarn workspace @qiaoyuwen/react-hooks run build", - "start:react-designer": "yarn workspace @qiaoyuwen/react-designer run start", - "lint:react-designer": "yarn workspace @qiaoyuwen/react-designer run lint-staged", - "build:react-designer": "yarn workspace @qiaoyuwen/react-designer run build", - "husky-init": "husky install", - "lerna:publish": "yarn build:react-hooks && yarn registry:npm && npm adduser && lerna publish --no-commit-hooks", - "registry:taobao": "npm config set registry https://registry.npm.taobao.org/", - "registry:npm": "npm config set registry https://registry.npmjs.org/" - }, - "devDependencies": { - "lerna": "^4.0.0", - "husky": "^6.0.0", - "cz-conventional-changelog": "3.3.0", - "@commitlint/cli": "^12.1.4", - "@commitlint/config-conventional": "^12.1.4", - "dumi": "^1.1.24", - "yorkie": "^2.0.0" - }, - "config": { - "commitizen": { - "path": "./node_modules/cz-conventional-changelog" - } - }, - "license": "MIT" -} diff --git a/packages/react-designer/.editorconfig b/packages/react-designer/.editorconfig deleted file mode 100755 index 7e3649a..0000000 --- a/packages/react-designer/.editorconfig +++ /dev/null @@ -1,16 +0,0 @@ -# http://editorconfig.org -root = true - -[*] -indent_style = space -indent_size = 2 -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -[*.md] -trim_trailing_whitespace = false - -[Makefile] -indent_style = tab diff --git a/packages/react-designer/.eslintrc.js b/packages/react-designer/.eslintrc.js deleted file mode 100644 index 01e3dc3..0000000 --- a/packages/react-designer/.eslintrc.js +++ /dev/null @@ -1,255 +0,0 @@ -module.exports = { - extends: ['eslint-config-airbnb-base', 'prettier', 'plugin:@typescript-eslint/recommended'], - parser: '@typescript-eslint/parser', - plugins: ['eslint-comments', 'react', 'jest', 'unicorn', 'react-hooks'], - env: { - browser: true, - node: true, - es6: true, - mocha: true, - jest: true, - jasmine: true, - }, - rules: { - 'react/display-name': 0, - 'react/jsx-props-no-spreading': 0, - 'react/state-in-constructor': 0, - 'react/static-property-placement': 0, - // Too restrictive: https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/destructuring-assignment.md - 'react/destructuring-assignment': 'off', - 'react/jsx-filename-extension': 'off', - 'react/no-array-index-key': 'warn', - 'react-hooks/rules-of-hooks': 'error', // Checks rules of Hooks - 'react-hooks/exhaustive-deps': 'warn', // Checks deps of Hooks - 'react/require-default-props': 0, - 'react/jsx-fragments': 0, - 'react/jsx-wrap-multilines': 0, - 'react/prop-types': 0, - 'react/forbid-prop-types': 0, - 'react/sort-comp': 0, - 'react/react-in-jsx-scope': 0, - 'react/jsx-one-expression-per-line': 0, - 'generator-star-spacing': 0, - 'function-paren-newline': 0, - 'import/no-unresolved': 0, - 'import/order': 0, - 'import/no-named-as-default': 0, - 'import/no-cycle': 0, - 'import/prefer-default-export': 0, - 'import/no-default-export': 0, - 'import/no-extraneous-dependencies': 0, - 'import/named': 0, - 'import/no-named-as-default-member': 0, - 'import/no-duplicates': 0, - 'import/no-self-import': 0, - 'import/extensions': 0, - 'import/no-useless-path-segments': 0, - 'jsx-a11y/no-noninteractive-element-interactions': 0, - 'jsx-a11y/click-events-have-key-events': 0, - 'jsx-a11y/no-static-element-interactions': 0, - 'jsx-a11y/anchor-is-valid': 0, - 'sort-imports': 0, - 'class-methods-use-this': 0, - 'no-confusing-arrow': 0, - 'linebreak-style': 0, - // Too restrictive, writing ugly code to defend against a very unlikely scenario: https://eslint.org/docs/rules/no-prototype-builtins - 'no-prototype-builtins': 'off', - 'unicorn/prevent-abbreviations': 'off', - // Conflict with prettier - 'arrow-body-style': 0, - 'arrow-parens': 0, - 'object-curly-newline': 0, - 'implicit-arrow-linebreak': 0, - 'operator-linebreak': 0, - 'eslint-comments/no-unlimited-disable': 0, - 'no-param-reassign': 2, - 'space-before-function-paren': 0, - // tsEslintConfig - 'no-undef': 0, - '@typescript-eslint/adjacent-overload-signatures': 0, - '@typescript-eslint/array-type': 'error', - '@typescript-eslint/await-thenable': 0, - '@typescript-eslint/ban-ts-comment': 0, - '@typescript-eslint/ban-tslint-comment': 0, - 'brace-style': 'off', - '@typescript-eslint/brace-style': 0, - '@typescript-eslint/class-literal-property-style': 0, - 'comma-dangle': 'off', - '@typescript-eslint/comma-dangle': 0, - 'comma-spacing': 'off', - '@typescript-eslint/comma-spacing': 0, - '@typescript-eslint/consistent-indexed-object-style': 1, - '@typescript-eslint/consistent-type-assertions': 0, - '@typescript-eslint/consistent-type-definitions': 0, - '@typescript-eslint/consistent-type-imports': 1, - 'default-param-last': 'off', - '@typescript-eslint/default-param-last': 0, - 'dot-notation': 'off', - '@typescript-eslint/dot-notation': 1, - '@typescript-eslint/explicit-function-return-type': 0, - 'func-call-spacing': 'off', - '@typescript-eslint/func-call-spacing': 0, - indent: 'off', - 'init-declarations': 'off', - '@typescript-eslint/init-declarations': 0, - 'keyword-spacing': 'off', - '@typescript-eslint/keyword-spacing': 0, - 'lines-between-class-members': 'off', - '@typescript-eslint/lines-between-class-members': 0, - '@typescript-eslint/member-delimiter-style': 0, - '@typescript-eslint/member-ordering': 0, - '@typescript-eslint/method-signature-style': 'error', - 'no-array-constructor': 'off', - '@typescript-eslint/no-array-constructor': 0, - '@typescript-eslint/no-base-to-string': 0, - '@typescript-eslint/no-confusing-non-null-assertion': 'error', - '@typescript-eslint/no-confusing-void-expression': 0, - 'no-dupe-class-members': 'off', - '@typescript-eslint/no-dupe-class-members': 'error', - 'no-duplicate-imports': 'off', - '@typescript-eslint/no-duplicate-imports': 0, - '@typescript-eslint/no-dynamic-delete': 0, - 'no-empty-function': 'off', - '@typescript-eslint/no-empty-function': 0, - '@typescript-eslint/no-empty-interface': 1, - '@typescript-eslint/no-extra-non-null-assertion': 0, - 'no-extra-parens': 'off', - '@typescript-eslint/no-extra-parens': 0, - 'no-extra-semi': 'off', - '@typescript-eslint/no-extra-semi': 0, - '@typescript-eslint/no-extraneous-class': 0, - '@typescript-eslint/no-floating-promises': 0, - '@typescript-eslint/no-for-in-array': 'error', - '@typescript-eslint/no-implicit-any-catch': 0, - 'no-implied-eval': 'off', - '@typescript-eslint/no-implied-eval': 0, - '@typescript-eslint/no-inferrable-types': 0, - 'no-invalid-this': 'off', - '@typescript-eslint/no-invalid-this': 'error', - '@typescript-eslint/no-invalid-void-type': 0, - 'no-loop-func': 'off', - '@typescript-eslint/no-loop-func': 'error', - 'no-loss-of-precision': 'off', - '@typescript-eslint/no-loss-of-precision': 0, - 'no-magic-numbers': 'off', - '@typescript-eslint/no-magic-numbers': 0, - '@typescript-eslint/no-misused-new': 'error', - '@typescript-eslint/no-misused-promises': 0, - '@typescript-eslint/no-namespace': 1, - '@typescript-eslint/no-non-null-asserted-optional-chain': 'error', - '@typescript-eslint/no-parameter-properties': 'error', - 'no-redeclare': 'off', - '@typescript-eslint/no-redeclare': 'error', - '@typescript-eslint/no-require-imports': 0, - 'no-shadow': 'off', - '@typescript-eslint/no-shadow': 'error', - '@typescript-eslint/no-this-alias': 'error', - 'no-throw-literal': 'off', - '@typescript-eslint/no-throw-literal': 'error', - '@typescript-eslint/no-type-alias': 0, - '@typescript-eslint/no-unnecessary-boolean-literal-compare': 0, - '@typescript-eslint/no-unnecessary-condition': 0, - '@typescript-eslint/no-unnecessary-qualifier': 0, - '@typescript-eslint/no-unnecessary-type-arguments': 0, - '@typescript-eslint/no-unnecessary-type-assertion': 0, - '@typescript-eslint/no-unnecessary-type-constraint': 0, - '@typescript-eslint/no-unsafe-assignment': 0, - '@typescript-eslint/no-unsafe-call': 0, - '@typescript-eslint/no-unsafe-member-access': 0, - '@typescript-eslint/no-unsafe-return': 0, - 'no-unused-expressions': 'off', - '@typescript-eslint/no-unused-expressions': 'error', - 'no-unused-vars': 'off', - 'no-use-before-define': 'off', - 'no-useless-constructor': 'off', - '@typescript-eslint/no-useless-constructor': 'error', - '@typescript-eslint/non-nullable-type-assertion-style': 0, - '@typescript-eslint/prefer-as-const': 0, - '@typescript-eslint/prefer-enum-initializers': 0, - '@typescript-eslint/prefer-for-of': 0, - '@typescript-eslint/prefer-function-type': 0, - '@typescript-eslint/prefer-includes': 0, - '@typescript-eslint/prefer-literal-enum-member': 0, - '@typescript-eslint/prefer-namespace-keyword': 0, - '@typescript-eslint/prefer-nullish-coalescing': 0, - '@typescript-eslint/prefer-optional-chain': 0, - '@typescript-eslint/prefer-readonly': 0, - '@typescript-eslint/prefer-readonly-parameter-types': 0, - '@typescript-eslint/prefer-reduce-type-parameter': 0, - '@typescript-eslint/prefer-regexp-exec': 0, - '@typescript-eslint/prefer-string-starts-ends-with': 0, - '@typescript-eslint/prefer-ts-expect-error': 0, - '@typescript-eslint/promise-function-async': 0, - quotes: 'off', - '@typescript-eslint/quotes': 0, - '@typescript-eslint/require-array-sort-compare': 0, - 'require-await': 'off', - '@typescript-eslint/require-await': 0, - '@typescript-eslint/restrict-plus-operands': 0, - '@typescript-eslint/restrict-template-expressions': 0, - 'no-return-await': 'off', - '@typescript-eslint/return-await': 0, - semi: 'off', - '@typescript-eslint/semi': 0, - '@typescript-eslint/space-before-function-paren': 0, - 'space-infix-ops': 'off', - '@typescript-eslint/space-infix-ops': 0, - '@typescript-eslint/strict-boolean-expressions': 0, - '@typescript-eslint/switch-exhaustiveness-check': 'error', - '@typescript-eslint/triple-slash-reference': 'error', - '@typescript-eslint/type-annotation-spacing': 'error', - '@typescript-eslint/typedef': 'error', - '@typescript-eslint/unbound-method': 0, - '@typescript-eslint/unified-signatures': 'error', - '@typescript-eslint/indent': 0, - // Makes no sense to allow type inferrence for expression parameters, but require typing the response - '@typescript-eslint/no-use-before-define': [ - 'error', - { functions: false, classes: true, variables: true, typedefs: true }, - ], - camelcase: 0, - '@typescript-eslint/camelcase': 0, - '@typescript-eslint/no-var-requires': 0, - // Common abbreviations are known and readable - '@typescript-eslint/explicit-member-accessibility': 0, - '@typescript-eslint/interface-name-prefix': 0, - '@typescript-eslint/no-non-null-assertion': 0, - '@typescript-eslint/no-explicit-any': 0, - '@typescript-eslint/ban-types': 1, - '@typescript-eslint/explicit-module-boundary-types': 0, - '@typescript-eslint/naming-convention': 0, - '@typescript-eslint/no-unused-vars': [ - 'error', - { vars: 'all', args: 'after-used', ignoreRestSiblings: true }, - ], - }, - settings: { - // support import modules from TypeScript files in JavaScript files - 'import/resolver': { - node: { - extensions: ['.js', '.jsx', '.ts', '.tsx', '.d.ts'], - }, - }, - 'import/parsers': { - '@typescript-eslint/parser': ['.ts', '.tsx', '.d.ts'], - }, - 'import/extensions': ['.js', '.mjs', '.jsx', '.ts', '.tsx', '.d.ts'], - 'import/external-module-folders': ['node_modules', 'node_modules/@types'], - polyfills: ['fetch', 'Promise', 'URL', 'object-assign'], - }, - parserOptions: { - ecmaFeatures: { - jsx: true, - }, - babelOptions: { - presets: ['@babel/preset-env', '@babel/preset-react', '@babel/preset-typescript'], - plugins: [ - ['@babel/plugin-proposal-decorators', { legacy: true }], - ['@babel/plugin-proposal-class-properties', { loose: true }], - ], - }, - requireConfigFile: false, - tsconfigRootDir: __dirname, - project: './tsconfig.json', - }, -}; diff --git a/packages/react-designer/.gitignore b/packages/react-designer/.gitignore deleted file mode 100644 index bee1cf6..0000000 --- a/packages/react-designer/.gitignore +++ /dev/null @@ -1,20 +0,0 @@ -# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. - -# dependencies -/node_modules -/npm-debug.log* -/yarn-error.log -/yarn.lock -/package-lock.json - -# production -/dist - -# misc -.DS_Store - -# umi -/src/.umi -/src/.umi-production -/src/.umi-test -/.env.local diff --git a/packages/react-designer/.prettierignore b/packages/react-designer/.prettierignore deleted file mode 100644 index 0d4222f..0000000 --- a/packages/react-designer/.prettierignore +++ /dev/null @@ -1,8 +0,0 @@ -**/*.md -**/*.svg -**/*.ejs -**/*.html -package.json -.umi -.umi-production -.umi-test diff --git a/packages/react-designer/.prettierrc b/packages/react-designer/.prettierrc deleted file mode 100644 index 94beb14..0000000 --- a/packages/react-designer/.prettierrc +++ /dev/null @@ -1,11 +0,0 @@ -{ - "singleQuote": true, - "trailingComma": "all", - "printWidth": 80, - "overrides": [ - { - "files": ".prettierrc", - "options": { "parser": "json" } - } - ] -} diff --git a/packages/react-designer/.stylelintrc.js b/packages/react-designer/.stylelintrc.js deleted file mode 100644 index e2ac02f..0000000 --- a/packages/react-designer/.stylelintrc.js +++ /dev/null @@ -1,20 +0,0 @@ -module.exports = { - extends: [ - 'stylelint-config-standard', - 'stylelint-config-css-modules', - 'stylelint-config-rational-order', - 'stylelint-config-prettier', - 'stylelint-no-unsupported-browser-features', - ], - plugins: ['stylelint-order', 'stylelint-declaration-block-no-ignored-properties'], - rules: { - 'no-descending-specificity': null, - //https://github.com/stylelint/stylelint/issues/4114 - 'function-calc-no-invalid': null, - 'function-url-quotes': 'always', - 'font-family-no-missing-generic-family-keyword': null, // iconfont - 'plugin/declaration-block-no-ignored-properties': true, - 'unit-no-unknown': [true, { ignoreUnits: ['rpx'] }], - }, - ignoreFiles: ['**/*.js', '**/*.jsx', '**/*.tsx', '**/*.ts'], -}; \ No newline at end of file diff --git a/packages/react-designer/.umirc.ts b/packages/react-designer/.umirc.ts deleted file mode 100644 index a0cf2ae..0000000 --- a/packages/react-designer/.umirc.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { defineConfig } from 'umi'; - -export default defineConfig({ - nodeModulesTransform: { - type: 'none', - }, - routes: [ - { path: '/', component: '@/pages/index' }, - ], - fastRefresh: {}, -}); diff --git a/packages/react-designer/README.md b/packages/react-designer/README.md deleted file mode 100644 index 07afeb7..0000000 --- a/packages/react-designer/README.md +++ /dev/null @@ -1,15 +0,0 @@ -# umi project - -## Getting Started - -Install dependencies, - -```bash -$ yarn -``` - -Start the dev server, - -```bash -$ yarn start -``` diff --git a/packages/react-designer/package.json b/packages/react-designer/package.json deleted file mode 100644 index eab2b75..0000000 --- a/packages/react-designer/package.json +++ /dev/null @@ -1,66 +0,0 @@ -{ - "name": "@qiaoyuwen/react-designer", - "version": "1.0.3", - "author": "qiaoyuwen", - "license": "MIT", - "main": "dist/index.js", - "scripts": { - "start": "umi dev", - "build": "umi build", - "postinstall": "umi generate tmp", - "prettier": "prettier --write '**/*.{js,jsx,tsx,ts,less,md,json}'", - "test": "umi-test", - "test:coverage": "umi-test --coverage", - "lint-staged": "lint-staged" - }, - "dependencies": { - "@designable/core": "^0.4.35", - "@designable/react": "^0.4.35", - "@designable/react-settings-form": "^0.4.35", - "@formily/designable-antd": "^2.0.0-rc.1", - "eslint-plugin-jest": "^24.4.0", - "react": "^17.0.0", - "react-dom": "^17.0.0", - "umi": "^3.5.13" - }, - "devDependencies": { - "@types/react": "^17.0.0", - "@types/react-dom": "^17.0.0", - "@typescript-eslint/eslint-plugin": "^4.28.4", - "@typescript-eslint/parser": "^4.28.4", - "@umijs/preset-react": "^1.7.4", - "@umijs/test": "^3.5.13", - "eslint": "^7.31.0", - "eslint-config-airbnb-base": "^14.2.1", - "eslint-config-prettier": "^8.3.0", - "eslint-formatter-pretty": "^4.1.0", - "eslint-plugin-compat": "^3.11.1", - "eslint-plugin-eslint-comments": "^3.2.0", - "eslint-plugin-import": "^2.23.4", - "eslint-plugin-jsx-a11y": "^6.4.1", - "eslint-plugin-markdown": "^2.2.0", - "eslint-plugin-promise": "^5.1.0", - "eslint-plugin-react": "^7.24.0", - "eslint-plugin-react-hooks": "^4.2.0", - "eslint-plugin-unicorn": "^34.0.1", - "lint-staged": "^11.1.0", - "prettier": "^2.2.0", - "stylelint": "^13.13.1", - "stylelint-config-css-modules": "^2.2.0", - "stylelint-config-prettier": "^8.0.2", - "stylelint-config-rational-order": "^0.1.2", - "stylelint-config-standard": "^22.0.0", - "stylelint-declaration-block-no-ignored-properties": "^2.4.0", - "stylelint-no-unsupported-browser-features": "^5.0.1", - "stylelint-order": "^4.1.0", - "typescript": "^4.3.5", - "yorkie": "^2.0.0" - }, - "lint-staged": { - "**/*.{js,jsx,tsx,ts,md,json}": [ - "prettier --write", - "git add" - ], - "**/*.{js,jsx,ts,tsx}": "eslint --ext .js,.jsx,.ts,.tsx" - } -} diff --git a/packages/react-designer/src/designer.md b/packages/react-designer/src/designer.md deleted file mode 100644 index 1d034b4..0000000 --- a/packages/react-designer/src/designer.md +++ /dev/null @@ -1,10 +0,0 @@ ---- -title: Designer -nav: - title: Designer - path: /designer -sidemenu: false -order: 1 ---- - - diff --git a/packages/react-designer/src/pages/index.less b/packages/react-designer/src/pages/index.less deleted file mode 100644 index 586302b..0000000 --- a/packages/react-designer/src/pages/index.less +++ /dev/null @@ -1,3 +0,0 @@ -.title { - background: rgb(121, 242, 157); -} diff --git a/packages/react-designer/src/pages/index.tsx b/packages/react-designer/src/pages/index.tsx deleted file mode 100644 index 52e35b9..0000000 --- a/packages/react-designer/src/pages/index.tsx +++ /dev/null @@ -1,87 +0,0 @@ -import { - Designer, - DesignerToolsWidget, - ViewToolsWidget, - Workspace, - DragSourceWidget, - MainPanel, - CompositePanel, - WorkspacePanel, - ToolbarPanel, - ViewportPanel, - ViewPanel, - SettingsPanel, - ComponentTreeWidget, -} from '@designable/react'; -import { SettingsForm } from '@designable/react-settings-form'; -import { createDesigner, GlobalRegistry } from '@designable/core'; -import { - createDesignableField, - createDesignableForm, -} from '@formily/designable-antd'; -import '@formily/designable-antd'; -import 'antd/dist/antd.less'; - -GlobalRegistry.registerDesignerLocales({ - 'zh-CN': { - sources: { - Inputs: '输入控件', - Layouts: '布局组件', - Arrays: '自增组件', - }, - }, -}); - -const Root = createDesignableForm({ - registryName: 'Root', -}); - -const DesignableField = createDesignableField({ - registryName: 'DesignableField', -}); - -const engine = createDesigner(); - -const App = () => { - return ( - - - - - - - - - - - - - - - - - - - - {() => ( - - )} - - - - - - - - - - - ); -}; - -export default App; diff --git a/packages/react-designer/tsconfig.json b/packages/react-designer/tsconfig.json deleted file mode 100644 index bd7d41d..0000000 --- a/packages/react-designer/tsconfig.json +++ /dev/null @@ -1,38 +0,0 @@ -{ - "compilerOptions": { - "target": "esnext", - "module": "esnext", - "moduleResolution": "node", - "resolveJsonModule": true, - "importHelpers": true, - "jsx": "react-jsx", - "esModuleInterop": true, - "sourceMap": true, - "baseUrl": "./", - "strict": true, - "paths": { - "@/*": ["src/*"], - "@@/*": ["src/.umi/*"] - }, - "allowSyntheticDefaultImports": true - }, - "include": [ - "mock/**/*", - "src/**/*", - "config/**/*", - ".umirc.ts", - "typings.d.ts", - ".eslintrc.js" - ], - "exclude": [ - "node_modules", - "lib", - "es", - "dist", - "typings", - "**/__test__", - "test", - "docs", - "tests" - ] -} diff --git a/packages/react-designer/typings.d.ts b/packages/react-designer/typings.d.ts deleted file mode 100644 index 06c8a5b..0000000 --- a/packages/react-designer/typings.d.ts +++ /dev/null @@ -1,10 +0,0 @@ -declare module '*.css'; -declare module '*.less'; -declare module '*.png'; -declare module '*.svg' { - export function ReactComponent( - props: React.SVGProps, - ): React.ReactElement; - const url: string; - export default url; -} diff --git a/packages/react-hooks/.eslintrc.js b/packages/react-hooks/.eslintrc.js deleted file mode 100644 index 872b0d6..0000000 --- a/packages/react-hooks/.eslintrc.js +++ /dev/null @@ -1,254 +0,0 @@ -module.exports = { - extends: ['eslint-config-airbnb-base', 'prettier', 'plugin:@typescript-eslint/recommended'], - parser: '@typescript-eslint/parser', - plugins: ['eslint-comments', 'react', 'jest', 'unicorn', 'react-hooks'], - env: { - browser: true, - node: true, - es6: true, - mocha: true, - jest: true, - jasmine: true, - }, - rules: { - 'react/display-name': 0, - 'react/jsx-props-no-spreading': 0, - 'react/state-in-constructor': 0, - 'react/static-property-placement': 0, - // Too restrictive: https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/destructuring-assignment.md - 'react/destructuring-assignment': 'off', - 'react/jsx-filename-extension': 'off', - 'react/no-array-index-key': 'warn', - 'react-hooks/rules-of-hooks': 'error', // Checks rules of Hooks - 'react-hooks/exhaustive-deps': 'warn', // Checks deps of Hooks - 'react/require-default-props': 0, - 'react/jsx-fragments': 0, - 'react/jsx-wrap-multilines': 0, - 'react/prop-types': 0, - 'react/forbid-prop-types': 0, - 'react/sort-comp': 0, - 'react/react-in-jsx-scope': 0, - 'react/jsx-one-expression-per-line': 0, - 'generator-star-spacing': 0, - 'function-paren-newline': 0, - 'import/no-unresolved': 0, - 'import/order': 0, - 'import/no-named-as-default': 0, - 'import/no-cycle': 0, - 'import/prefer-default-export': 0, - 'import/no-default-export': 0, - 'import/no-extraneous-dependencies': 0, - 'import/named': 0, - 'import/no-named-as-default-member': 0, - 'import/no-duplicates': 0, - 'import/no-self-import': 0, - 'import/extensions': 0, - 'import/no-useless-path-segments': 0, - 'jsx-a11y/no-noninteractive-element-interactions': 0, - 'jsx-a11y/click-events-have-key-events': 0, - 'jsx-a11y/no-static-element-interactions': 0, - 'jsx-a11y/anchor-is-valid': 0, - 'sort-imports': 0, - 'class-methods-use-this': 0, - 'no-confusing-arrow': 0, - 'linebreak-style': 0, - // Too restrictive, writing ugly code to defend against a very unlikely scenario: https://eslint.org/docs/rules/no-prototype-builtins - 'no-prototype-builtins': 'off', - 'unicorn/prevent-abbreviations': 'off', - // Conflict with prettier - 'arrow-body-style': 0, - 'arrow-parens': 0, - 'object-curly-newline': 0, - 'implicit-arrow-linebreak': 0, - 'operator-linebreak': 0, - 'eslint-comments/no-unlimited-disable': 0, - 'no-param-reassign': 2, - 'space-before-function-paren': 0, - // tsEslintConfig - 'no-undef': 0, - '@typescript-eslint/adjacent-overload-signatures': 0, - '@typescript-eslint/array-type': 'error', - '@typescript-eslint/await-thenable': 0, - '@typescript-eslint/ban-ts-comment': 0, - '@typescript-eslint/ban-tslint-comment': 0, - 'brace-style': 'off', - '@typescript-eslint/brace-style': 0, - '@typescript-eslint/class-literal-property-style': 0, - 'comma-dangle': 'off', - '@typescript-eslint/comma-dangle': 0, - 'comma-spacing': 'off', - '@typescript-eslint/comma-spacing': 0, - '@typescript-eslint/consistent-indexed-object-style': 1, - '@typescript-eslint/consistent-type-assertions': 0, - '@typescript-eslint/consistent-type-definitions': 0, - '@typescript-eslint/consistent-type-imports': 1, - 'default-param-last': 'off', - '@typescript-eslint/default-param-last': 0, - 'dot-notation': 'off', - '@typescript-eslint/dot-notation': 1, - '@typescript-eslint/explicit-function-return-type': 0, - 'func-call-spacing': 'off', - '@typescript-eslint/func-call-spacing': 0, - indent: 'off', - 'init-declarations': 'off', - '@typescript-eslint/init-declarations': 0, - 'keyword-spacing': 'off', - '@typescript-eslint/keyword-spacing': 0, - 'lines-between-class-members': 'off', - '@typescript-eslint/lines-between-class-members': 0, - '@typescript-eslint/member-delimiter-style': 0, - '@typescript-eslint/member-ordering': 0, - '@typescript-eslint/method-signature-style': 'error', - 'no-array-constructor': 'off', - '@typescript-eslint/no-array-constructor': 0, - '@typescript-eslint/no-base-to-string': 0, - '@typescript-eslint/no-confusing-non-null-assertion': 'error', - '@typescript-eslint/no-confusing-void-expression': 0, - 'no-dupe-class-members': 'off', - '@typescript-eslint/no-dupe-class-members': 'error', - 'no-duplicate-imports': 'off', - '@typescript-eslint/no-duplicate-imports': 0, - '@typescript-eslint/no-dynamic-delete': 0, - 'no-empty-function': 'off', - '@typescript-eslint/no-empty-function': 0, - '@typescript-eslint/no-empty-interface': 1, - '@typescript-eslint/no-extra-non-null-assertion': 0, - 'no-extra-parens': 'off', - '@typescript-eslint/no-extra-parens': 0, - 'no-extra-semi': 'off', - '@typescript-eslint/no-extra-semi': 0, - '@typescript-eslint/no-extraneous-class': 0, - '@typescript-eslint/no-floating-promises': 0, - '@typescript-eslint/no-for-in-array': 'error', - '@typescript-eslint/no-implicit-any-catch': 0, - 'no-implied-eval': 'off', - '@typescript-eslint/no-implied-eval': 0, - '@typescript-eslint/no-inferrable-types': 0, - 'no-invalid-this': 'off', - '@typescript-eslint/no-invalid-this': 'error', - '@typescript-eslint/no-invalid-void-type': 0, - 'no-loop-func': 'off', - '@typescript-eslint/no-loop-func': 'error', - 'no-loss-of-precision': 'off', - '@typescript-eslint/no-loss-of-precision': 0, - 'no-magic-numbers': 'off', - '@typescript-eslint/no-magic-numbers': 0, - '@typescript-eslint/no-misused-new': 'error', - '@typescript-eslint/no-misused-promises': 0, - '@typescript-eslint/no-namespace': 1, - '@typescript-eslint/no-non-null-asserted-optional-chain': 'error', - '@typescript-eslint/no-parameter-properties': 'error', - 'no-redeclare': 'off', - '@typescript-eslint/no-redeclare': 'error', - '@typescript-eslint/no-require-imports': 0, - 'no-shadow': 'off', - '@typescript-eslint/no-shadow': 'error', - '@typescript-eslint/no-this-alias': 'error', - 'no-throw-literal': 'off', - '@typescript-eslint/no-throw-literal': 'error', - '@typescript-eslint/no-type-alias': 0, - '@typescript-eslint/no-unnecessary-boolean-literal-compare': 0, - '@typescript-eslint/no-unnecessary-condition': 0, - '@typescript-eslint/no-unnecessary-qualifier': 0, - '@typescript-eslint/no-unnecessary-type-arguments': 0, - '@typescript-eslint/no-unnecessary-type-assertion': 0, - '@typescript-eslint/no-unnecessary-type-constraint': 0, - '@typescript-eslint/no-unsafe-assignment': 0, - '@typescript-eslint/no-unsafe-call': 0, - '@typescript-eslint/no-unsafe-member-access': 0, - '@typescript-eslint/no-unsafe-return': 0, - 'no-unused-expressions': 'off', - '@typescript-eslint/no-unused-expressions': 'error', - 'no-unused-vars': 'off', - 'no-use-before-define': 'off', - 'no-useless-constructor': 'off', - '@typescript-eslint/no-useless-constructor': 'error', - '@typescript-eslint/non-nullable-type-assertion-style': 0, - '@typescript-eslint/prefer-as-const': 0, - '@typescript-eslint/prefer-enum-initializers': 0, - '@typescript-eslint/prefer-for-of': 0, - '@typescript-eslint/prefer-function-type': 0, - '@typescript-eslint/prefer-includes': 0, - '@typescript-eslint/prefer-literal-enum-member': 0, - '@typescript-eslint/prefer-namespace-keyword': 0, - '@typescript-eslint/prefer-nullish-coalescing': 0, - '@typescript-eslint/prefer-optional-chain': 0, - '@typescript-eslint/prefer-readonly': 0, - '@typescript-eslint/prefer-readonly-parameter-types': 0, - '@typescript-eslint/prefer-reduce-type-parameter': 0, - '@typescript-eslint/prefer-regexp-exec': 0, - '@typescript-eslint/prefer-string-starts-ends-with': 0, - '@typescript-eslint/prefer-ts-expect-error': 0, - '@typescript-eslint/promise-function-async': 0, - quotes: 'off', - '@typescript-eslint/quotes': 0, - '@typescript-eslint/require-array-sort-compare': 0, - 'require-await': 'off', - '@typescript-eslint/require-await': 0, - '@typescript-eslint/restrict-plus-operands': 0, - '@typescript-eslint/restrict-template-expressions': 0, - 'no-return-await': 'off', - '@typescript-eslint/return-await': 0, - semi: 'off', - '@typescript-eslint/semi': 0, - '@typescript-eslint/space-before-function-paren': 0, - 'space-infix-ops': 'off', - '@typescript-eslint/space-infix-ops': 0, - '@typescript-eslint/strict-boolean-expressions': 0, - '@typescript-eslint/switch-exhaustiveness-check': 'error', - '@typescript-eslint/triple-slash-reference': 'error', - '@typescript-eslint/type-annotation-spacing': 'error', - '@typescript-eslint/typedef': 'error', - '@typescript-eslint/unbound-method': 0, - '@typescript-eslint/unified-signatures': 'error', - '@typescript-eslint/indent': 0, - // Makes no sense to allow type inferrence for expression parameters, but require typing the response - '@typescript-eslint/no-use-before-define': [ - 'error', - { functions: false, classes: true, variables: true, typedefs: true }, - ], - camelcase: 0, - '@typescript-eslint/camelcase': 0, - '@typescript-eslint/no-var-requires': 0, - // Common abbreviations are known and readable - '@typescript-eslint/explicit-member-accessibility': 0, - '@typescript-eslint/interface-name-prefix': 0, - '@typescript-eslint/no-non-null-assertion': 0, - '@typescript-eslint/no-explicit-any': 0, - '@typescript-eslint/ban-types': 1, - '@typescript-eslint/explicit-module-boundary-types': 0, - '@typescript-eslint/naming-convention': 0, - '@typescript-eslint/no-unused-vars': [ - 'error', - { vars: 'all', args: 'after-used', ignoreRestSiblings: true }, - ], - }, - settings: { - // support import modules from TypeScript files in JavaScript files - 'import/resolver': { - node: { - extensions: ['.js', '.jsx', '.ts', '.tsx', '.d.ts'], - }, - }, - 'import/parsers': { - '@typescript-eslint/parser': ['.ts', '.tsx', '.d.ts'], - }, - 'import/extensions': ['.js', '.mjs', '.jsx', '.ts', '.tsx', '.d.ts'], - 'import/external-module-folders': ['node_modules', 'node_modules/@types'], - polyfills: ['fetch', 'Promise', 'URL', 'object-assign'], - }, - parserOptions: { - ecmaFeatures: { - jsx: true, - }, - babelOptions: { - presets: ['@babel/preset-env', '@babel/preset-react', '@babel/preset-typescript'], - plugins: [ - ['@babel/plugin-proposal-decorators', { legacy: true }], - ['@babel/plugin-proposal-class-properties', { loose: true }], - ], - }, - requireConfigFile: false, - project: './tsconfig.json', - }, -}; diff --git a/packages/react-hooks/.npmignore b/packages/react-hooks/.npmignore deleted file mode 100644 index 8b7cf2d..0000000 --- a/packages/react-hooks/.npmignore +++ /dev/null @@ -1,10 +0,0 @@ -dist/*/__tests__ -dist/*/demo -coverage/ -src/ -.eslintrc.js -.npmignore -.prettierrc.js -jest.config.js -tsconfig.json -*.tgz diff --git a/packages/react-hooks/.prettierrc.js b/packages/react-hooks/.prettierrc.js deleted file mode 100644 index 7f4f9aa..0000000 --- a/packages/react-hooks/.prettierrc.js +++ /dev/null @@ -1,15 +0,0 @@ -module.exports = { - singleQuote: true, - trailingComma: 'all', - printWidth: 100, - proseWrap: 'never', - endOfLine: 'lf', - overrides: [ - { - files: '.prettierrc', - options: { - parser: 'json', - }, - }, - ], -}; diff --git a/packages/react-hooks/README.md b/packages/react-hooks/README.md deleted file mode 100644 index 6ad603c..0000000 --- a/packages/react-hooks/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# react-hooks - -一些常用的 hooks 的实现 diff --git a/packages/react-hooks/jest.config.js b/packages/react-hooks/jest.config.js deleted file mode 100644 index 8892626..0000000 --- a/packages/react-hooks/jest.config.js +++ /dev/null @@ -1,13 +0,0 @@ -module.exports = { - moduleNameMapper: { - '@/(.*)': '/src/$1', - }, - transform: { - '^.+\\.tsx?$': 'ts-jest', // 匹配 .ts 或者 .tsx 结尾的文件 - }, - collectCoverage: true, // 统计覆盖率 - testEnvironment: 'node', // 测试环境 - // 不算入覆盖率的文件夹 - coveragePathIgnorePatterns: ['/node_modules/', '/__test__/'], - testPathIgnorePatterns: ['/dist'], -}; diff --git a/packages/react-hooks/package.json b/packages/react-hooks/package.json deleted file mode 100644 index cb5d5a9..0000000 --- a/packages/react-hooks/package.json +++ /dev/null @@ -1,62 +0,0 @@ -{ - "name": "@qiaoyuwen/react-hooks", - "version": "1.0.4", - "author": "qiaoyuwen", - "license": "MIT", - "main": "dist/index.js", - "scripts": { - "build": "rm -rf ./dist && tsc", - "test": "jest test --coverage --detectOpenHandles", - "lint-staged": "lint-staged" - }, - "dependencies": { - "@ant-design/icons": "^4.6.2", - "eslint-plugin-jest": "^24.4.0", - "immer": "^9.0.2", - "lodash": "^4.17.21", - "swr": "^0.5.6", - "use-immer": "^0.5.2" - }, - "devDependencies": { - "@testing-library/react-hooks": "^7.0.0", - "@types/jest": "^26.0.23", - "@types/jsdom": "^16.2.12", - "@types/react": "^17.0.9", - "@types/react-beautiful-dnd": "^13.1.1", - "@types/react-infinite-scroller": "^1.2.1", - "@typescript-eslint/eslint-plugin": "^4.26.0", - "@typescript-eslint/parser": "^4.26.0", - "antd": "^4.16.2", - "eslint": "^7.27.0", - "eslint-config-airbnb-base": "^14.2.1", - "eslint-config-prettier": "^8.3.0", - "eslint-formatter-pretty": "^4.0.0", - "eslint-plugin-compat": "^3.9.0", - "eslint-plugin-eslint-comments": "^3.2.0", - "eslint-plugin-import": "^2.23.4", - "eslint-plugin-jsx-a11y": "^6.4.1", - "eslint-plugin-markdown": "^2.2.0", - "eslint-plugin-promise": "^5.1.0", - "eslint-plugin-react": "^7.24.0", - "eslint-plugin-react-hooks": "^4.2.0", - "eslint-plugin-unicorn": "^33.0.0", - "jest": "26.5.3", - "jsdom": "^16.6.0", - "lint-staged": "^11.0.0", - "prettier": "^2.3.0", - "prettier-plugin-style-order": "^0.2.2", - "react": "^17.0.2", - "react-beautiful-dnd": "12.0.0-beta.10", - "react-infinite-scroller": "^1.2.4", - "react-test-renderer": "^17.0.2", - "ts-jest": "26.5.3", - "typescript": "^4.3.2" - }, - "lint-staged": { - "**/*.{js,jsx,tsx,ts,md,json}": [ - "prettier --write", - "git add" - ], - "**/*.{js,jsx,ts,tsx}": "eslint --ext .js,.jsx,.ts,.tsx" - } -} diff --git a/packages/react-hooks/src/array/__tests__/index.test.ts b/packages/react-hooks/src/array/__tests__/index.test.ts deleted file mode 100644 index de1234e..0000000 --- a/packages/react-hooks/src/array/__tests__/index.test.ts +++ /dev/null @@ -1,169 +0,0 @@ -import { renderHook, act } from '@testing-library/react-hooks'; -import { useArray } from '../index'; - -describe('useArray', () => { - test('valid return type', () => { - const { result } = renderHook(() => useArray()); - const [value, methods] = result.current; - expect(value).toStrictEqual([]); - Object.keys(methods).forEach((key) => { - expect(typeof methods[key]).toBe('function'); - }); - }); - - test('initial value true', () => { - const { result } = renderHook(() => useArray([1])); - const [value] = result.current; - expect(value).toStrictEqual([1]); - }); - - test('push', () => { - const { result } = renderHook(() => useArray([1])); - act(() => result.current[1].push(2)); - const [value] = result.current; - expect(value).toStrictEqual([1, 2]); - }); - - test('unshift', () => { - const { result } = renderHook(() => useArray([1])); - act(() => result.current[1].unshift(2)); - const [value] = result.current; - expect(value).toStrictEqual([2, 1]); - }); - - test('pop', () => { - const { result } = renderHook(() => useArray([1])); - act(() => result.current[1].pop()); - expect(result.current[0]).toStrictEqual([]); - - act(() => result.current[1].pop()); - expect(result.current[0]).toStrictEqual([]); - }); - - test('shift', () => { - const { result } = renderHook(() => useArray([1])); - act(() => result.current[1].shift()); - expect(result.current[0]).toStrictEqual([]); - - act(() => result.current[1].shift()); - expect(result.current[0]).toStrictEqual([]); - }); - - test('slice', () => { - const { result } = renderHook(() => useArray([1, 2, 3])); - act(() => result.current[1].slice(0, 1)); - const [value] = result.current; - expect(value).toStrictEqual([1]); - }); - - test('splice', () => { - const { result } = renderHook(() => useArray([1, 2, 3])); - act(() => result.current[1].splice(0, 1, 4)); - const [value] = result.current; - expect(value).toStrictEqual([4, 2, 3]); - }); - - test('remove', () => { - const { result } = renderHook(() => useArray([1, 2, 3])); - act(() => result.current[1].remove(2)); - expect(result.current[0]).toStrictEqual([1, 3]); - - act(() => result.current[1].remove(2)); - expect(result.current[0]).toStrictEqual([1, 3]); - }); - - test('removeAt', () => { - const { result } = renderHook(() => useArray([1, 2, 3])); - act(() => result.current[1].removeAt(1)); - const [value] = result.current; - expect(value).toStrictEqual([1, 3]); - }); - - test('insertAt', () => { - const { result } = renderHook(() => useArray([1, 2, 3])); - act(() => result.current[1].insertAt(1, 4)); - const [value] = result.current; - expect(value).toStrictEqual([1, 4, 2, 3]); - }); - - test('concat', () => { - const { result } = renderHook(() => useArray([1, 2, 3])); - act(() => result.current[1].concat(4)); - const [value] = result.current; - expect(value).toStrictEqual([1, 2, 3, 4]); - }); - - test('replace', () => { - const { result } = renderHook(() => useArray([1, 2, 3])); - act(() => result.current[1].replace(2, 4)); - expect(result.current[0]).toStrictEqual([1, 4, 3]); - - act(() => result.current[1].replace(2, 1)); - expect(result.current[0]).toStrictEqual([1, 4, 3]); - }); - - test('replaceAll', () => { - const { result } = renderHook(() => useArray([1, 2, 2, 3, 2])); - act(() => result.current[1].replaceAll(2, 4)); - expect(result.current[0]).toStrictEqual([1, 4, 4, 3, 4]); - - act(() => result.current[1].replaceAll(5, 0)); - expect(result.current[0]).toStrictEqual([1, 4, 4, 3, 4]); - }); - - test('replaceAt', () => { - const { result } = renderHook(() => useArray([1, 2, 3])); - act(() => result.current[1].replaceAt(1, 4)); - const [value] = result.current; - expect(value).toStrictEqual([1, 4, 3]); - }); - - test('filter', () => { - const { result } = renderHook(() => useArray([1, 2, 3])); - act(() => result.current[1].filter((item) => item === 1)); - const [value] = result.current; - expect(value).toStrictEqual([1]); - }); - - test('union', () => { - const { result } = renderHook(() => useArray([1, 2, 3])); - act(() => result.current[1].union([4, 5])); - const [value] = result.current; - expect(value).toStrictEqual([1, 2, 3, 4, 5]); - }); - - test('intersect', () => { - const { result } = renderHook(() => useArray([1, 2, 3])); - act(() => result.current[1].intersect([2, 3, 4])); - const [value] = result.current; - expect(value).toStrictEqual([2, 3]); - }); - - test('difference', () => { - const { result } = renderHook(() => useArray([1, 2, 3])); - act(() => result.current[1].difference([2, 3, 4])); - const [value] = result.current; - expect(value).toStrictEqual([1]); - }); - - test('reverse', () => { - const { result } = renderHook(() => useArray([1, 2, 3])); - act(() => result.current[1].reverse()); - const [value] = result.current; - expect(value).toStrictEqual([3, 2, 1]); - }); - - test('sort', () => { - const { result } = renderHook(() => useArray([1, 2, 3])); - act(() => result.current[1].sort((item1, item2) => item2 - item1)); - const [value] = result.current; - expect(value).toStrictEqual([3, 2, 1]); - }); - - test('clear', () => { - const { result } = renderHook(() => useArray([1, 2, 3])); - act(() => result.current[1].clear()); - const [value] = result.current; - expect(value).toStrictEqual([]); - }); -}); diff --git a/packages/react-hooks/src/array/demo/useArray.tsx b/packages/react-hooks/src/array/demo/useArray.tsx deleted file mode 100644 index 61252c7..0000000 --- a/packages/react-hooks/src/array/demo/useArray.tsx +++ /dev/null @@ -1,36 +0,0 @@ -import type { FunctionComponent } from 'react'; -import { Space, Button, List } from 'antd'; -import 'antd/dist/antd.css'; -import { useArray } from '..'; - -const Component: FunctionComponent = () => { - const [data, { push, remove, reverse }] = useArray([]); - - return ( - <> - - ( - - {item} - - - )} - /> - - - - - - - ); -}; - -export default Component; diff --git a/packages/react-hooks/src/array/index.ts b/packages/react-hooks/src/array/index.ts deleted file mode 100644 index 9f2becc..0000000 --- a/packages/react-hooks/src/array/index.ts +++ /dev/null @@ -1,85 +0,0 @@ -import { useMethodsNative } from '../methods'; - -function createMethods() { - return { - push(state: T[], item: T) { - return [...state, item]; - }, - unshift(state: T[], item: T) { - return [item, ...state]; - }, - pop(state: T[]) { - if (!state.length) { - return state; - } - return state.slice(0, -1); - }, - shift(state: T[]) { - if (!state.length) { - return state; - } - return state.slice(1); - }, - slice(state: T[], start?: number, end?: number) { - return state.slice(start, end); - }, - splice(state: T[], index: number, deleteCount: number, ...insertions: T[]) { - return [...state.slice(0, index), ...insertions, ...state.slice(index + deleteCount)]; - }, - remove(state: T[], item: T) { - const next = state.filter((v) => v !== item); - return state.length === next.length ? state : next; - }, - removeAt(state: T[], index: number) { - return [...state.slice(0, index), ...state.slice(index + 1)]; - }, - insertAt(state: T[], index: number, item: T) { - return [...state.slice(0, index), item, ...state.slice(index)]; - }, - concat(state: T[], item: T | T[]) { - return state.concat(item); - }, - replace(state: T[], from: T, to: T) { - const index = state.indexOf(from); - if (index >= 0) { - state.splice(index, 1, to); - return [...state.slice(0, index), to, ...state.slice(index + 1)]; - } - return state; - }, - replaceAll(state: T[], from: T, to: T) { - const has = state.includes(from); - return has ? state.map((v) => (v === from ? to : v)) : state; - }, - replaceAt(state: T[], index: number, item: T) { - return [...state.slice(0, index), item, ...state.slice(index + 1)]; - }, - filter(state: T[], predicate: (item: T, index: number) => boolean) { - return state.filter(predicate); - }, - union(state: T[], array: T[]) { - return [...new Set([...state, ...array])]; - }, - intersect(state: T[], array: T[]) { - const coming = new Set(array); - return [...new Set(state.filter((v) => coming.has(v)))]; - }, - difference(state: T[], array: T[]) { - const coming = new Set(array); - return state.filter((v) => !coming.has(v)); - }, - reverse(state: T[]) { - return state.slice().reverse(); - }, - sort(state: T[], compare?: (x: T, y: T) => number) { - return state.slice().sort(compare); - }, - clear() { - return []; - }, - }; -} - -export function useArray(initialValue: T[] = []) { - return useMethodsNative(createMethods(), initialValue); -} diff --git a/packages/react-hooks/src/array/useArray.md b/packages/react-hooks/src/array/useArray.md deleted file mode 100644 index a95c225..0000000 --- a/packages/react-hooks/src/array/useArray.md +++ /dev/null @@ -1,56 +0,0 @@ ---- -title: useArray -nav: - title: Array - path: /hook -order: 1 ---- - -# useArray - -管理数组的 hook - - - -# API - -```typescript -const [ - state, - methods, -] = useArray( - initialValue?: T[], -); -``` - -# 返回值 - -| 参数 | 说明 | 类型 | -| ------- | ------ | ------- | -| state | 状态值 | boolean | -| methods | 方法集 | object | - -# methods - -| 属性 | 说明 | 类型 | -| ---------- | ---- | ---------------------------------------------------------------- | -| push | - | (item: T) => void | -| unshift | - | (item: T) => void | -| pop | - | (item: T) => void | -| shift | - | (item: T) => void | -| slice | - | (start?: number, end?: number) => void | -| splice | - | (index: number, deleteCount: number, ...insertions: T[]) => void | -| remove | - | (item: T) => void | -| removeAt | - | (index: number) => void | -| insertAt | - | (index: number, item: T) => void | -| concat | - | (item: T \| T[]) => void | -| replace | - | (from: T, to: T) => void | -| replaceAll | - | (from: T, to: T) => void | -| replaceAt | - | (index: number, item: T) => void | -| filter | - | (predicate: (item: T, index: number) => boolean) => void | -| union | - | (array: T[]) => void | -| intersect | - | (array: T[]) => void | -| difference | - | (array: T[]) => void | -| reverse | - | () => void | -| sort | - | (compare?: (x: T, y: T) => number) => void | -| clear | - | () => void | diff --git a/packages/react-hooks/src/boolean/__tests__/index.test.ts b/packages/react-hooks/src/boolean/__tests__/index.test.ts deleted file mode 100644 index 15cb313..0000000 --- a/packages/react-hooks/src/boolean/__tests__/index.test.ts +++ /dev/null @@ -1,75 +0,0 @@ -import { renderHook, act } from '@testing-library/react-hooks'; -import { useBoolean, useSwitch } from '../index'; - -describe('useBoolean', () => { - test('valid return type', () => { - const { result } = renderHook(() => useBoolean()); - const [value, methods] = result.current; - expect(value).toBe(false); - expect(typeof methods.setTrue).toBe('function'); - expect(typeof methods.setFalse).toBe('function'); - expect(typeof methods.toggle).toBe('function'); - }); - - test('initial value true', () => { - const { result } = renderHook(() => useBoolean(true)); - const [value] = result.current; - expect(value).toBe(true); - }); - - test('setTrue', () => { - const { result } = renderHook(() => useBoolean(false)); - act(() => result.current[1].setTrue()); - const [value] = result.current; - expect(value).toBe(true); - }); - - test('setFalse', () => { - const { result } = renderHook(() => useBoolean(true)); - act(() => result.current[1].setFalse()); - const [value] = result.current; - expect(value).toBe(false); - }); - - test('toggle', () => { - const { result } = renderHook(() => useBoolean(false)); - act(() => result.current[1].toggle()); - const [value] = result.current; - expect(value).toBe(true); - }); -}); - -describe('useSwitch', () => { - test('valid return type', () => { - const { result } = renderHook(() => useSwitch()); - const [value, { on, off, toggle }] = result.current; - expect(value).toBe(false); - expect(typeof on).toBe('function'); - expect(typeof off).toBe('function'); - expect(typeof toggle).toBe('function'); - }); - - test('initial value', () => { - const { result } = renderHook(() => useSwitch(true)); - const [value] = result.current; - expect(value).toBe(true); - }); - - test('on', () => { - const { result } = renderHook(() => useSwitch(false)); - act(() => result.current[1].on()); - expect(result.current[0]).toBe(true); - }); - - test('off', () => { - const { result } = renderHook(() => useSwitch(true)); - act(() => result.current[1].off()); - expect(result.current[0]).toBe(false); - }); - - test('toggle', () => { - const { result } = renderHook(() => useSwitch(false)); - act(() => result.current[1].toggle()); - expect(result.current[0]).toBe(true); - }); -}); diff --git a/packages/react-hooks/src/boolean/demo/useBoolean.tsx b/packages/react-hooks/src/boolean/demo/useBoolean.tsx deleted file mode 100644 index b9c0e2f..0000000 --- a/packages/react-hooks/src/boolean/demo/useBoolean.tsx +++ /dev/null @@ -1,31 +0,0 @@ -import type { FunctionComponent } from 'react'; -import { Space, Checkbox, Button } from 'antd'; -import 'antd/dist/antd.css'; -import { useBoolean } from '..'; - -const Component: FunctionComponent = () => { - const [checked, { setTrue, setFalse, toggle }] = useBoolean(false); - - return ( - <> - - - 单选框 - - - - - - - - - ); -}; - -export default Component; diff --git a/packages/react-hooks/src/boolean/demo/useSwitch.tsx b/packages/react-hooks/src/boolean/demo/useSwitch.tsx deleted file mode 100644 index 5a8c7ab..0000000 --- a/packages/react-hooks/src/boolean/demo/useSwitch.tsx +++ /dev/null @@ -1,31 +0,0 @@ -import type { FunctionComponent } from 'react'; -import { Space, Switch, Button } from 'antd'; -import 'antd/dist/antd.css'; -import { useSwitch } from '..'; - -const Component: FunctionComponent = () => { - const [checked, { on, off, toggle }] = useSwitch(false); - - return ( - <> - - - - - - - - - - - - ); -}; - -export default Component; diff --git a/packages/react-hooks/src/boolean/index.ts b/packages/react-hooks/src/boolean/index.ts deleted file mode 100644 index d79a182..0000000 --- a/packages/react-hooks/src/boolean/index.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { useMethodsNative } from '..'; - -const reducers = { - setTrue() { - return true; - }, - setFalse() { - return false; - }, - toggle(value: boolean) { - return !value; - }, -}; - -export function useBoolean(initialValue: boolean = false) { - return useMethodsNative(reducers, initialValue); -} - -export function useSwitch(initialValue: boolean = false) { - const [state, { setTrue: on, setFalse: off, toggle }] = useBoolean(initialValue); - return [state, { on, off, toggle }] as const; -} diff --git a/packages/react-hooks/src/boolean/useBoolean.md b/packages/react-hooks/src/boolean/useBoolean.md deleted file mode 100644 index 8395f9c..0000000 --- a/packages/react-hooks/src/boolean/useBoolean.md +++ /dev/null @@ -1,39 +0,0 @@ ---- -title: useBoolean -nav: - title: Boolean - path: /hook -order: 2 ---- - -# useBoolean - -管理布尔值的 hook - - - -# API - -```typescript -const [ - state, - methods, -] = useBoolean( - initialValue?: boolean, -); -``` - -# 返回值 - -| 参数 | 说明 | 类型 | -| ------- | ------ | ------- | -| state | 状态值 | boolean | -| methods | 方法集 | object | - -# methods - -| 属性 | 说明 | 类型 | -| -------- | ------------ | ---------- | -| setTrue | 设置为 true | () => void | -| setFalse | 设置为 false | () => void | -| toggle | 切换状态值 | () => void | diff --git a/packages/react-hooks/src/boolean/useSwitch.md b/packages/react-hooks/src/boolean/useSwitch.md deleted file mode 100644 index 490ea70..0000000 --- a/packages/react-hooks/src/boolean/useSwitch.md +++ /dev/null @@ -1,39 +0,0 @@ ---- -title: useSwitch -nav: - title: Boolean - path: /hook -order: 3 ---- - -# useSwitch - -管理布尔值的 hook - - - -# API - -```typescript -const [ - state, - methods, -] = useSwitch( - initialValue?: boolean, -); -``` - -# 返回值 - -| 参数 | 说明 | 类型 | -| ------- | ------ | ------- | -| state | 状态值 | boolean | -| methods | 方法集 | object | - -# methods - -| 属性 | 说明 | 类型 | -| ------ | ------------ | ---------- | -| on | 设置为 true | () => void | -| off | 设置为 false | () => void | -| toggle | 切换状态值 | () => void | diff --git a/packages/react-hooks/src/index.ts b/packages/react-hooks/src/index.ts deleted file mode 100644 index 93d3658..0000000 --- a/packages/react-hooks/src/index.ts +++ /dev/null @@ -1,11 +0,0 @@ -export * from './array'; -export * from './boolean'; -export * from './methods'; -export * from './number'; -export * from './selection'; -export * from './set'; -export * from './timeout'; -export * from './pagination'; -export * from './local-storage'; -export * from './size'; -export * from './virtual-list'; diff --git a/packages/react-hooks/src/local-storage/__tests__/index.test.ts b/packages/react-hooks/src/local-storage/__tests__/index.test.ts deleted file mode 100644 index 506aa6a..0000000 --- a/packages/react-hooks/src/local-storage/__tests__/index.test.ts +++ /dev/null @@ -1,113 +0,0 @@ -import { renderHook, act } from '@testing-library/react-hooks'; -import { useLocalStorage } from '../index'; -import { JSDOM } from 'jsdom'; - -/** - * @jest-environment jsdom - */ - -describe('useLocalStorage', () => { - beforeAll(() => { - const { window } = new JSDOM('', { - url: 'http://localhost:8000', - }); - global.window = window as any; - }); - - test('initial value', () => { - const { result } = renderHook(() => useLocalStorage('testKey', 'testValue')); - const [value] = result.current; - expect(value).toBe('testValue'); - }); - - test('existing value', () => { - window.localStorage.setItem('testKey', JSON.stringify('originTestValue')); - const { result } = renderHook(() => useLocalStorage('testKey', 'testValue')); - const [value] = result.current; - expect(value).toBe('originTestValue'); - }); - - test('inital value on parse error', () => { - window.localStorage.setItem('testKey', 'invalid json'); - const { result } = renderHook(() => useLocalStorage('testKey', 'testValue')); - expect(result.current[0]).toBe('testValue'); - }); - - test('listen on change', () => { - const { result } = renderHook(() => useLocalStorage('testKey', 'testValue')); - const event = new window.StorageEvent('storage', { - key: 'testKey', - oldValue: '"testValue"', - newValue: '"newTestValue"', - storageArea: window.localStorage, - }); - act(() => { - window.dispatchEvent(event); - }); - expect(result.current[0]).toBe('newTestValue'); - }); - - test('listen on change value parse error', () => { - const { result } = renderHook(() => useLocalStorage('testKey', 'testValue')); - const event = new window.StorageEvent('storage', { - key: 'testKey', - oldValue: '"testValue"', - newValue: 'invalid json', - storageArea: window.localStorage, - }); - act(() => { - window.dispatchEvent(event); - }); - expect(result.current[0]).toBe('testValue'); - }); - - test('listen on change value null', () => { - const { result } = renderHook(() => useLocalStorage('testKey', 'testValue')); - const event = new window.StorageEvent('storage', { - key: 'testKey', - oldValue: '"testValue"', - newValue: null, - storageArea: window.localStorage, - }); - act(() => { - window.dispatchEvent(event); - }); - expect(result.current[0]).toBe('testValue'); - }); - - test('ignore unexpected change key', () => { - const { result } = renderHook(() => useLocalStorage('testKey', 'testValue')); - const event = new window.StorageEvent('storage', { - key: 'otherKey', - oldValue: '"value"', - newValue: '"newValue"', - storageArea: window.localStorage, - }); - act(() => { - window.dispatchEvent(event); - }); - expect(result.current[0]).toBe('testValue'); - }); - - test('ignore session storage change', () => { - const { result } = renderHook(() => useLocalStorage('testKey', 'testValue')); - const event = new window.StorageEvent('storage', { - key: 'testKey', - oldValue: '"testValue"', - newValue: '"newTestValue"', - storageArea: window.sessionStorage, - }); - act(() => { - window.dispatchEvent(event); - }); - expect(result.current[0]).toBe('testValue'); - }); - - test('set local storage value', () => { - const { result } = renderHook(() => useLocalStorage('testKey', 'testValue')); - act(() => { - result.current[1]('newTestValue'); - }); - expect(result.current[0]).toBe('newTestValue'); - }); -}); diff --git a/packages/react-hooks/src/local-storage/demo/useLocalStorage.tsx b/packages/react-hooks/src/local-storage/demo/useLocalStorage.tsx deleted file mode 100644 index d0bd21c..0000000 --- a/packages/react-hooks/src/local-storage/demo/useLocalStorage.tsx +++ /dev/null @@ -1,30 +0,0 @@ -/** - * title: 将 state 持久化在 localStorage 中 - * desc: 刷新页面后,可以看到输入框中的内容被从 localStorage 中恢复了。 - */ - -import type { FunctionComponent } from 'react'; -import { Space, Input, Button } from 'antd'; -import 'antd/dist/antd.css'; -import { useLocalStorage } from '..'; - -const Component: FunctionComponent = () => { - const [value, setValue] = useLocalStorage('localStorageDemo', ''); - - return ( - <> - - - setValue(e.target.value)} /> - - - - - - - ); -}; - -export default Component; diff --git a/packages/react-hooks/src/local-storage/index.ts b/packages/react-hooks/src/local-storage/index.ts deleted file mode 100644 index 8c29e41..0000000 --- a/packages/react-hooks/src/local-storage/index.ts +++ /dev/null @@ -1,41 +0,0 @@ -import { useCallback, useEffect, useState } from 'react'; - -function getLocalStorageValue(key: string, initialValue: T): T { - try { - const valueString = window.localStorage.getItem(key); - return valueString ? JSON.parse(valueString) : initialValue; - } catch { - return initialValue; - } -} - -export function useLocalStorage(key: string, initialValue: T) { - const [value, setValue] = useState(() => getLocalStorageValue(key, initialValue)); - - const setLocalStorageValue = useCallback( - (newValue: T) => { - window.localStorage.setItem(key, JSON.stringify(newValue)); - setValue(newValue); - }, - [key], - ); - - useEffect(() => { - const onStorageChange = (e: StorageEvent) => { - if (e.storageArea === window.localStorage && key === e.key) { - try { - setValue(e.newValue ? JSON.parse(e.newValue) : initialValue); - } catch { - setValue(initialValue); - } - } - }; - window.addEventListener('storage', onStorageChange); - - return () => { - window.removeEventListener('storage', onStorageChange); - }; - }, [initialValue, key]); - - return [value, setLocalStorageValue] as const; -} diff --git a/packages/react-hooks/src/local-storage/useLocalStorage.md b/packages/react-hooks/src/local-storage/useLocalStorage.md deleted file mode 100644 index 676de68..0000000 --- a/packages/react-hooks/src/local-storage/useLocalStorage.md +++ /dev/null @@ -1,32 +0,0 @@ ---- -title: useLocalStorage -nav: - title: Storage - path: /hook -order: 2 ---- - -# useLocalStorage - -管理 localStorage 的 hook - - - -# API - -```typescript -const [ - state, - setState, -] = useLocalStorage( - key: string, - initialValue: T, -); -``` - -# 返回值 - -| 参数 | 说明 | 类型 | -| -------- | --------------------------------- | ------------------ | -| state | 状态值 | boolean | -| setState | 设置新状态值并同步到 localStorage | (value: T) => void | diff --git a/packages/react-hooks/src/methods/__tests__/index.test.ts b/packages/react-hooks/src/methods/__tests__/index.test.ts deleted file mode 100644 index 487fd73..0000000 --- a/packages/react-hooks/src/methods/__tests__/index.test.ts +++ /dev/null @@ -1,133 +0,0 @@ -import { useState } from 'react'; -import { renderHook, act } from '@testing-library/react-hooks'; -import { useImmer } from 'use-immer'; -import { - useMethods, - useMethodsExtension, - useMethodsNative, - useMethodsExtensionNative, -} from '../index'; - -describe('useMethods', () => { - test('valid return type', () => { - const methods = { - inc(value: number) { - return value + 1; - }, - }; - const { result } = renderHook(() => useMethods(methods, 0)); - const [state, { inc }, setState] = result.current; - expect(state).toBe(0); - expect(typeof inc).toBe('function'); - expect(typeof setState).toBe('function'); - }); - - test('factory initial value', () => { - const { result } = renderHook(() => useMethods({}, () => 0)); - const [state] = result.current; - expect(state).toBe(0); - }); - - test('method call immutable', () => { - const methods = { - inc(value: number) { - return value + 1; - }, - }; - const { result } = renderHook(() => useMethods(methods, 0)); - act(() => result.current[1].inc()); - expect(result.current[0]).toBe(1); - }); - - test('method call immer', () => { - const methods = { - inc(state: { value: number }) { - // eslint-disable-next-line no-param-reassign - state.value += 1; - }, - }; - const { result } = renderHook(() => useMethods(methods, { value: 0 })); - act(() => result.current[1].inc()); - expect(result.current[0].value).toBe(1); - }); - - test('set state', () => { - const { result } = renderHook(() => useMethods({}, { value: 0 })); - act(() => result.current[2]((s) => ({ value: s.value + 1 }))); - expect(result.current[0].value).toBe(1); - }); -}); - -describe('useMethodsExtension', () => { - test('with builtin state', () => { - const { result } = renderHook(() => useImmer({ foo: 1 })); - const methods = { - inc(state: { foo: number }, value: number) { - return { - ...state, - foo: state.foo + value, - }; - }, - }; - const { result: extension } = renderHook(() => useMethodsExtension(methods, result.current[1])); - act(() => extension.current.inc(3)); - expect(result.current[0]).toEqual({ foo: 4 }); - }); -}); - -describe('useMethodsNative', () => { - test('valid return type', () => { - const methods = { - inc(value: number) { - return value + 1; - }, - }; - const { result } = renderHook(() => useMethodsNative(methods, 0)); - const [state, { inc }, setState] = result.current; - expect(state).toBe(0); - expect(typeof inc).toBe('function'); - expect(typeof setState).toBe('function'); - }); - - test('factory initial value', () => { - const { result } = renderHook(() => useMethodsNative({}, () => 0)); - const [state] = result.current; - expect(state).toBe(0); - }); - - test('method call', () => { - const methods = { - inc(value: number) { - return value + 1; - }, - }; - const { result } = renderHook(() => useMethodsNative(methods, 0)); - act(() => result.current[1].inc()); - expect(result.current[0]).toBe(1); - }); - - test('set state', () => { - const { result } = renderHook(() => useMethodsNative({}, { value: 0 })); - act(() => result.current[2]({ value: 1 })); - expect(result.current[0].value).toBe(1); - }); -}); - -describe('useMethodsExtensionNative', () => { - test('with builtin state', () => { - const { result: resultState } = renderHook(() => useState({ foo: 1 })); - const methods = { - inc(state: { foo: number }, value: number) { - return { - ...state, - foo: state.foo + value, - }; - }, - }; - const { result: extension } = renderHook(() => - useMethodsExtensionNative(methods, resultState.current[1]), - ); - act(() => extension.current.inc(3)); - expect(resultState.current[0]).toEqual({ foo: 4 }); - }); -}); diff --git a/packages/react-hooks/src/methods/immer.ts b/packages/react-hooks/src/methods/immer.ts deleted file mode 100644 index 9b40cb8..0000000 --- a/packages/react-hooks/src/methods/immer.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { useRef } from 'react'; -import type { Updater } from 'use-immer'; -import { useImmer } from 'use-immer'; -import type { ImmerReducers, Methods } from './interface'; - -export type MethodsHook> = [S, Methods, Updater]; - -export function useMethodsExtension>( - reducers: R, - setState: Updater, -): Methods { - const methodsRef = useRef | undefined>(undefined); - - if (!methodsRef.current) { - methodsRef.current = Object.keys(reducers).reduce((methods, key) => { - const fn = reducers[key]; - const bound = (...args: any[]) => setState((s) => fn(s as any, ...args)); - Object.assign(methods, { [key]: bound }); - return methods; - }, {} as Methods); - } - - return methodsRef.current; -} - -export function useMethods>( - reducers: R, - initialState: S | (() => S), -): MethodsHook { - const [state, setState] = useImmer(initialState); - const boundMethods = useMethodsExtension(reducers, setState); - return [state, boundMethods, setState]; -} diff --git a/packages/react-hooks/src/methods/index.ts b/packages/react-hooks/src/methods/index.ts deleted file mode 100644 index 03ea886..0000000 --- a/packages/react-hooks/src/methods/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -export * from './interface'; -export * from './native'; -export * from './immer'; diff --git a/packages/react-hooks/src/methods/interface.ts b/packages/react-hooks/src/methods/interface.ts deleted file mode 100644 index 1f60b56..0000000 --- a/packages/react-hooks/src/methods/interface.ts +++ /dev/null @@ -1,9 +0,0 @@ -export type NativeReducers = Record S>; - -export type ImmerReducers = Record S | void>; - -type Strip = T extends (state: any, ...args: infer P) => any ? (...args: P) => void : never; - -export type Methods | ImmerReducers> = { - [K in keyof R]: Strip; -}; diff --git a/packages/react-hooks/src/methods/native.ts b/packages/react-hooks/src/methods/native.ts deleted file mode 100644 index b7fe363..0000000 --- a/packages/react-hooks/src/methods/native.ts +++ /dev/null @@ -1,36 +0,0 @@ -import type { Dispatch, SetStateAction } from 'react'; -import { useRef, useState } from 'react'; -import type { NativeReducers, Methods } from './interface'; - -export type NativeMethodsHook> = [ - S, - Methods, - Dispatch>, -]; - -export function useMethodsExtensionNative>( - reducers: R, - setState: Dispatch>, -): Methods { - const methodsRef = useRef | undefined>(undefined); - - if (!methodsRef.current) { - methodsRef.current = Object.keys(reducers).reduce((methods, key) => { - const fn = reducers[key]; - const bound = (...args: any[]) => setState((s) => fn(s, ...args)); - Object.assign(methods, { [key]: bound }); - return methods; - }, {} as Methods); - } - - return methodsRef.current; -} - -export function useMethodsNative>( - reducers: R, - initialState: S | (() => S), -): NativeMethodsHook { - const [state, setState] = useState(initialState); - const boundMethods = useMethodsExtensionNative(reducers, setState); - return [state, boundMethods, setState]; -} diff --git a/packages/react-hooks/src/number/__tests__/index.test.ts b/packages/react-hooks/src/number/__tests__/index.test.ts deleted file mode 100644 index 4b647f4..0000000 --- a/packages/react-hooks/src/number/__tests__/index.test.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { renderHook, act } from '@testing-library/react-hooks'; -import { useCounter } from '../index'; - -describe('useCounter', () => { - test('valid return type', () => { - const { result } = renderHook(() => useCounter()); - const [value, methods] = result.current; - expect(value).toBe(0); - expect(typeof methods.inc).toBe('function'); - expect(typeof methods.dec).toBe('function'); - expect(typeof methods.reset).toBe('function'); - }); - - test('initial value true', () => { - const { result } = renderHook(() => useCounter(1)); - const [value] = result.current; - expect(value).toBe(1); - }); - - test('inc', () => { - const { result } = renderHook(() => useCounter()); - act(() => result.current[1].inc()); - const [value] = result.current; - expect(value).toBe(1); - }); - - test('dec', () => { - const { result } = renderHook(() => useCounter(1)); - act(() => result.current[1].dec()); - const [value] = result.current; - expect(value).toBe(0); - }); - - test('reset', () => { - const { result } = renderHook(() => useCounter()); - act(() => result.current[1].reset(10)); - const [value] = result.current; - expect(value).toBe(10); - }); -}); diff --git a/packages/react-hooks/src/number/demo/useCounter.tsx b/packages/react-hooks/src/number/demo/useCounter.tsx deleted file mode 100644 index 62f0290..0000000 --- a/packages/react-hooks/src/number/demo/useCounter.tsx +++ /dev/null @@ -1,32 +0,0 @@ -import type { FunctionComponent } from 'react'; -import { Space, Button, InputNumber } from 'antd'; -import 'antd/dist/antd.css'; -import { useCounter } from '..'; - -const Component: FunctionComponent = () => { - const [count, { inc, dec, reset }] = useCounter(0, { - min: 0, - max: 10, - }); - - return ( - <> - - - - - - - - - - ); -}; - -export default Component; diff --git a/packages/react-hooks/src/number/index.ts b/packages/react-hooks/src/number/index.ts deleted file mode 100644 index 234d290..0000000 --- a/packages/react-hooks/src/number/index.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { useMethodsNative } from '../methods'; - -export interface CounterOptions { - min?: number; - max?: number; - step?: number; -} - -export function useCounter(initialValue: number = 0, options: CounterOptions = {}) { - const { min = -Infinity, max = Infinity, step = 1 } = options; - return useMethodsNative( - { - inc(state: number) { - return Math.min(max, state + step); - }, - dec(state: number) { - return Math.max(min, state - step); - }, - reset(state: number, value: number) { - return value; - }, - }, - initialValue, - ); -} diff --git a/packages/react-hooks/src/number/useCounter.md b/packages/react-hooks/src/number/useCounter.md deleted file mode 100644 index 896f223..0000000 --- a/packages/react-hooks/src/number/useCounter.md +++ /dev/null @@ -1,54 +0,0 @@ ---- -title: useCounter -nav: - title: Number - path: /hook -order: 3 ---- - -# useCounter - -管理计数的 hook - - - -# API - -```typescript -interface CounterOptions { - min?: number; - max?: number; - step?: number; -} - -const [ - state, - methods, -] = useCounter( - initialValue: number = 0, - options: CounterOptions = {} -); -``` - -# CounterOptions - -| 参数 | 说明 | 类型 | -| ---- | ------ | ------ | -| min | 最小值 | number | -| max | 最大值 | number | -| step | 间距 | number | - -# 返回值 - -| 参数 | 说明 | 类型 | -| ------- | ------ | ------- | -| state | 状态值 | boolean | -| methods | 方法集 | object | - -# methods - -| 属性 | 说明 | 类型 | -| ----- | ---------------- | ----------------------- | -| inc | 按设置的间距增加 | () => void | -| dec | 按设置的间距减少 | () => void | -| reset | 重置 | (value: number) => void | diff --git a/packages/react-hooks/src/pagination/__tests__/index.test.ts b/packages/react-hooks/src/pagination/__tests__/index.test.ts deleted file mode 100644 index 83169e8..0000000 --- a/packages/react-hooks/src/pagination/__tests__/index.test.ts +++ /dev/null @@ -1,116 +0,0 @@ -import { act, renderHook } from '@testing-library/react-hooks'; -import { useInfinitePagination } from '..'; -import { cache } from 'swr'; - -const timeout = (ms: number) => { - return new Promise((resolve) => { - setTimeout(resolve, ms); - }); -}; - -const fetcher = (current: number, pageSize: number) => { - return new Promise((resolve) => { - setTimeout(() => { - const data: string[] = []; - for (let i = 0; i < pageSize; i += 1) { - data.push(`${current + (current - 1) * pageSize}`); - } - if (current > 1) { - resolve(data.slice(0, 5)); - } else { - resolve(data); - } - }, 100); - }); -}; - -const emptyFetcher = () => { - return new Promise((resolve) => { - setTimeout(() => { - resolve([]); - }, 100); - }); -}; - -const errorFetcher = (current: number, pageSize: number) => { - return new Promise((resolve, reject) => { - setTimeout(() => { - const data: string[] = []; - for (let i = 0; i < pageSize; i += 1) { - data.push(`${current + (current - 1) * pageSize}`); - } - if (current > 1) { - reject(); - } else { - resolve(data); - } - }, 100); - }); -}; - -describe('useInfinitePagination', () => { - test('init', async () => { - const { result } = renderHook(() => useInfinitePagination('init test', fetcher, 10)); - expect(result.current[0]).toStrictEqual([]); - expect(result.current[1].isLoading).toEqual(true); - expect(result.current[1].isEmpty).toEqual(false); - expect(result.current[1].isReachingEnd).toEqual(false); - expect(result.current[1].isRefreshing).toEqual(false); - await act(async () => { - await timeout(150); - expect(result.current[0].length).toStrictEqual(10); - expect(result.current[1].isLoading).toEqual(false); - expect(result.current[1].isEmpty).toEqual(false); - expect(result.current[1].isReachingEnd).toEqual(false); - expect(result.current[1].isRefreshing).toEqual(false); - }); - }); - - test('empty', async () => { - const { result } = renderHook(() => useInfinitePagination('empty test', emptyFetcher, 10)); - expect(result.current[1].isReachingEnd).toEqual(false); - await act(async () => { - await timeout(150); - expect(result.current[0].length).toStrictEqual(0); - expect(result.current[1].isLoading).toEqual(false); - expect(result.current[1].isReachingEnd).toEqual(true); - }); - }); - - test('loadMore', async () => { - const { result } = renderHook(() => useInfinitePagination('loadMore test', fetcher, 10)); - await act(async () => { - await timeout(150); - result.current[1].loadMore(); - result.current[1].loadMore(); - await timeout(150); - expect(result.current[0].length).toStrictEqual(15); - expect(result.current[1].isLoading).toEqual(false); - expect(result.current[1].isReachingEnd).toEqual(true); - result.current[1].reset(); - await timeout(150); - expect(result.current[0].length).toStrictEqual(10); - expect(result.current[1].isLoading).toEqual(false); - expect(result.current[1].isReachingEnd).toEqual(false); - }); - }); - - test('error', async () => { - const { result } = renderHook(() => useInfinitePagination('error test', errorFetcher, 10)); - await act(async () => { - await timeout(150); - result.current[1].loadMore(); - await timeout(150); - expect(result.current[0].length).toStrictEqual(10); - expect(result.current[1].isLoading).toEqual(true); - expect(result.current[1].isReachingEnd).toEqual(false); - }); - }); - - afterEach(async () => { - await new Promise((resolve) => { - resolve(); - }); - cache.clear(); - }); -}); diff --git a/packages/react-hooks/src/pagination/demo/useInfinitePagination.tsx b/packages/react-hooks/src/pagination/demo/useInfinitePagination.tsx deleted file mode 100644 index 8a47661..0000000 --- a/packages/react-hooks/src/pagination/demo/useInfinitePagination.tsx +++ /dev/null @@ -1,74 +0,0 @@ -import type { FunctionComponent } from 'react'; -import React from 'react'; -import 'antd/dist/antd.css'; -import { useInfinitePagination } from '..'; -import { Button, List, Space } from 'antd'; -import InfiniteScroll from 'react-infinite-scroller'; - -const fetcher = (current: number, pageSize: number) => { - return new Promise((resolve) => { - setTimeout(() => { - const data: string[] = []; - for (let i = 0; i < pageSize; i += 1) { - data.push(`${Math.random()}`); - } - if (current > 5) { - resolve(data.slice(0, 4)); - } else { - resolve(data); - } - }, 500); - }); -}; - -const Component: FunctionComponent = () => { - const [data, { loadMore, refresh, reset, isReachingEnd, isLoading, isRefreshing }] = - useInfinitePagination('key', fetcher, 10); - - return ( - <> - - - - - - -
- - ( - -
- {index + 1}、 {item} -
-
- )} - loading={isLoading || isRefreshing} - /> -
-
- - ); -}; - -export default Component; diff --git a/packages/react-hooks/src/pagination/index.ts b/packages/react-hooks/src/pagination/index.ts deleted file mode 100644 index 9798d69..0000000 --- a/packages/react-hooks/src/pagination/index.ts +++ /dev/null @@ -1,53 +0,0 @@ -import { useMemo } from 'react'; -import type { SWRInfiniteConfiguration } from 'swr'; -import { useSWRInfinite } from 'swr'; - -export function useInfinitePagination>( - key: string, - fetcher: (current: number, pageSize: number, params?: P) => Promise, - pageSize: number, - params?: P, - options?: SWRInfiniteConfiguration, -) { - const { data, error, mutate, size, setSize, isValidating } = useSWRInfinite( - (current) => [key, current + 1, pageSize, params], - (_, current, _pageSize, _params) => fetcher(current, _pageSize, _params), - options, - ); - - const returnObj = useMemo(() => { - const isLoadingInitialData = !data && !error; - const isLoading = - isLoadingInitialData || !!(size > 0 && data && typeof data[size - 1] === 'undefined'); - const isEmpty = data?.[0]?.length === 0; - const isRefreshing = isValidating && !!(data && data.length === size); - const isReachingEnd = isEmpty || !!(data && data[data.length - 1].length < pageSize); - - const loadMore = () => { - if (!isLoading && !isReachingEnd) { - setSize(size + 1); - } - }; - - const reset = () => { - setSize(1); - }; - - return { - refresh: mutate, - loadMore, - reset, - isLoading, - isEmpty, - isReachingEnd, - isRefreshing, - }; - }, [data, error, size, pageSize, isValidating, mutate, setSize]); - - return [ - data ? ([] as T[]).concat(...data) : [], - { - ...returnObj, - }, - ] as const; -} diff --git a/packages/react-hooks/src/pagination/useInfinitePagination.md b/packages/react-hooks/src/pagination/useInfinitePagination.md deleted file mode 100644 index 696780b..0000000 --- a/packages/react-hooks/src/pagination/useInfinitePagination.md +++ /dev/null @@ -1,55 +0,0 @@ ---- -title: useInfinitePagination -nav: - title: Pagination - path: /hook -order: 2 ---- - -# useInfinitePagination - -无限加载分页的 hook - - - -# API - -```typescript -const [ - data, - methods, -] = useInfinitePagination( - key: string, - fetcher: (current: number, pageSize: number, params?: Record) => Promise>, - pageSize: number, - params?: P, -); -``` - -# 参数 - -| 参数名 | 说明 | 类型 | -| -------- | --------------- | --------------------------------------------------------------- | -| key | swr 唯一 key 值 | string | -| fetcher | 获取数据的方法 | (current: number, pageSize: number, params?: P) => Promise | -| pageSize | 每页大小 | number | -| params | 其它参数 | P | - -# 返回值 - -| 参数名 | 说明 | 类型 | -| ------- | -------- | ------ | -| data | 分页数据 | T[] | -| methods | 方法集 | object | - -# methods - -| 属性 | 说明 | 类型 | -| ------------- | ------------ | ---------- | -| refresh | 刷新 | () => void | -| loadMore | 加载下一页 | () => void | -| reset | 重置到第一页 | () => void | -| isLoading | 是否加载中 | boolean | -| isEmpty | 是否为空 | boolean | -| isReachingEnd | 是否加载完毕 | boolean | -| isRefreshing | 是否刷新中 | boolean | diff --git a/packages/react-hooks/src/selection/__tests__/index.test.ts b/packages/react-hooks/src/selection/__tests__/index.test.ts deleted file mode 100644 index 2551bf7..0000000 --- a/packages/react-hooks/src/selection/__tests__/index.test.ts +++ /dev/null @@ -1,88 +0,0 @@ -import { renderHook, act } from '@testing-library/react-hooks'; -import { useSelection } from '../index'; - -describe('useSelection', () => { - test('valid return type', () => { - const { result } = renderHook(() => useSelection([1, 2, 3])); - const [selections, singleMethods, allMethods] = result.current; - expect(selections).toStrictEqual(new Set()); - expect(typeof singleMethods.isSelected).toBe('function'); - expect(typeof singleMethods.select).toBe('function'); - expect(typeof singleMethods.unSelect).toBe('function'); - expect(typeof singleMethods.toggle).toBe('function'); - expect(typeof allMethods.allSelected).toBe('boolean'); - expect(typeof allMethods.noneSelected).toBe('boolean'); - expect(typeof allMethods.partiallySelected).toBe('boolean'); - expect(typeof allMethods.selectAll).toBe('function'); - expect(typeof allMethods.unSelectAll).toBe('function'); - expect(typeof allMethods.toggleAll).toBe('function'); - }); - - test('initial value true', () => { - const { result } = renderHook(() => useSelection([1], [1])); - const [value] = result.current; - expect(value).toStrictEqual(new Set([1])); - }); - - test('isSelected', () => { - const { result } = renderHook(() => useSelection([1, 2, 3], [1])); - expect(result.current[1].isSelected(1)).toBe(true); - }); - - test('select', () => { - const { result } = renderHook(() => useSelection([1, 2, 3])); - act(() => result.current[1].select(2)); - expect(result.current[0]).toStrictEqual(new Set([2])); - }); - - test('unSelect', () => { - const { result } = renderHook(() => useSelection([1, 2, 3], [1, 2])); - act(() => result.current[1].unSelect(2)); - expect(result.current[0]).toStrictEqual(new Set([1])); - }); - - test('toggle', () => { - const { result } = renderHook(() => useSelection([1, 2, 3], [1, 2])); - act(() => result.current[1].toggle(2)); - expect(result.current[0]).toStrictEqual(new Set([1])); - - act(() => result.current[1].toggle(2)); - expect(result.current[0]).toStrictEqual(new Set([1, 2])); - }); - - test('selectAll', () => { - const { result } = renderHook(() => useSelection([1, 2, 3])); - act(() => result.current[2].selectAll()); - expect(result.current[0]).toStrictEqual(new Set([1, 2, 3])); - }); - - test('unSelectAll', () => { - const { result } = renderHook(() => useSelection([1, 2, 3], [1, 2])); - act(() => result.current[2].unSelectAll()); - expect(result.current[0]).toStrictEqual(new Set()); - }); - - test('toggleAll', () => { - const { result } = renderHook(() => useSelection([1, 2, 3], [1, 2])); - act(() => result.current[2].toggleAll()); - expect(result.current[0]).toStrictEqual(new Set([1, 2, 3])); - - act(() => result.current[2].toggleAll()); - expect(result.current[0]).toStrictEqual(new Set([])); - }); - - test('noneSelected', () => { - const { result } = renderHook(() => useSelection([1, 2, 3], [])); - expect(result.current[2].noneSelected).toBe(true); - }); - - test('allSelected', () => { - const { result } = renderHook(() => useSelection([1, 2, 3], [1, 2, 3])); - expect(result.current[2].allSelected).toBe(true); - }); - - test('partiallySelected', () => { - const { result } = renderHook(() => useSelection([1, 2, 3], [1, 2])); - expect(result.current[2].partiallySelected).toBe(true); - }); -}); diff --git a/packages/react-hooks/src/selection/demo/useSelection.tsx b/packages/react-hooks/src/selection/demo/useSelection.tsx deleted file mode 100644 index d855b57..0000000 --- a/packages/react-hooks/src/selection/demo/useSelection.tsx +++ /dev/null @@ -1,32 +0,0 @@ -import type { FunctionComponent } from 'react'; -import { Space, Checkbox } from 'antd'; -import 'antd/dist/antd.css'; -import { useSelection } from '..'; - -const Component: FunctionComponent = () => { - const data = [1, 2, 3, 4]; - const defaltSelections = [1, 2]; - const [, { isSelected, toggle }, { allSelected, partiallySelected, toggleAll }] = useSelection( - data, - defaltSelections, - ); - - return ( - <> - - {data.map((item) => ( - toggle(item)}> - {item} - - ))} - - - 全选 - - - - - ); -}; - -export default Component; diff --git a/packages/react-hooks/src/selection/index.ts b/packages/react-hooks/src/selection/index.ts deleted file mode 100644 index cdd088d..0000000 --- a/packages/react-hooks/src/selection/index.ts +++ /dev/null @@ -1,78 +0,0 @@ -import { useSet } from '../set'; -import { useMemo } from 'react'; - -export function useSelection(items: T[], defaultSelected: T[] = []) { - const [selections, operator] = useSet(defaultSelected); - - const singleActions = useMemo(() => { - const isSelected = (item: T) => { - return selections.has(item); - }; - - const select = (item: T) => { - operator.add(item); - }; - - const unSelect = (item: T) => { - operator.remove(item); - }; - - const toggle = (item: T) => { - if (isSelected(item)) { - unSelect(item); - } else { - select(item); - } - }; - - return { - isSelected, - select, - unSelect, - toggle, - }; - }, [selections, operator]); - - const allActions = useMemo(() => { - const selectAll = () => { - operator.addAll(items); - }; - - const unSelectAll = () => { - operator.removeAll(items); - }; - - const noneSelected = items.every((item) => !selections.has(item)); - - const allSelected = items.every((item) => selections.has(item)) && !noneSelected; - - const partiallySelected = !noneSelected && !allSelected; - - const toggleAll = () => { - if (allSelected) { - unSelectAll(); - } else { - selectAll(); - } - }; - - return { - selectAll, - unSelectAll, - noneSelected, - allSelected, - partiallySelected, - toggleAll, - }; - }, [selections, operator, items]); - - return [ - selections, - { - ...singleActions, - }, - { - ...allActions, - }, - ] as const; -} diff --git a/packages/react-hooks/src/selection/useSelection.md b/packages/react-hooks/src/selection/useSelection.md deleted file mode 100644 index 59beaab..0000000 --- a/packages/react-hooks/src/selection/useSelection.md +++ /dev/null @@ -1,54 +0,0 @@ ---- -title: useSelection -nav: - title: Selection - path: /hook -order: 3 ---- - -# useSelection - -管理选择项的 hook - - - -# API - -```typescript -const [ - state, - singleActions, - allActions, -] = useSelection( - items: T[], - defaultSelected: T[] = [] -); -``` - -# 返回值 - -| 参数 | 说明 | 类型 | -| ------------- | ---------- | ------- | -| state | 状态值 | boolean | -| singleActions | 单选操作集 | object | -| allActions | 全选操作集 | object | - -# singleActions - -| 属性 | 说明 | 类型 | -| ---------- | -------- | -------------------- | -| isSelected | 是否选中 | (item: T) => boolean | -| select | 勾选 | (item: T) => void | -| unSelect | 去掉勾选 | (item: T) => void | -| toggle | 切换勾选 | (item: T) => void | - -# allActions - -| 属性 | 说明 | 类型 | -| ----------------- | -------------- | ---------- | -| noneSelected | 是否为全未选中 | boolean | -| allSelected | 是否为全选中 | boolean | -| partiallySelected | 是否为部分选中 | boolean | -| selectAll | 全选 | () => void | -| unSelectAll | 全未选 | () => void | -| toggleAll | 切换全选 | () => void | diff --git a/packages/react-hooks/src/set/__tests__/index.test.ts b/packages/react-hooks/src/set/__tests__/index.test.ts deleted file mode 100644 index 582270f..0000000 --- a/packages/react-hooks/src/set/__tests__/index.test.ts +++ /dev/null @@ -1,55 +0,0 @@ -import { renderHook, act } from '@testing-library/react-hooks'; -import { useSet } from '../index'; - -describe('useSet', () => { - test('valid return type', () => { - const { result } = renderHook(() => useSet()); - const [value, methods] = result.current; - expect(value).toStrictEqual(new Set()); - Object.keys(methods).forEach((key) => { - expect(typeof methods[key]).toBe('function'); - }); - }); - - test('initial value true', () => { - const { result } = renderHook(() => useSet([1])); - const [value] = result.current; - expect(value).toStrictEqual(new Set([1])); - }); - - test('add', () => { - const { result } = renderHook(() => useSet([1])); - act(() => result.current[1].add(2)); - expect(result.current[0]).toStrictEqual(new Set([1, 2])); - - act(() => result.current[1].add(2)); - expect(result.current[0]).toStrictEqual(new Set([1, 2])); - }); - - test('addAll', () => { - const { result } = renderHook(() => useSet([1])); - act(() => result.current[1].addAll([2, 3])); - expect(result.current[0]).toStrictEqual(new Set([1, 2, 3])); - }); - - test('remove', () => { - const { result } = renderHook(() => useSet([1, 2])); - act(() => result.current[1].remove(2)); - expect(result.current[0]).toStrictEqual(new Set([1])); - - act(() => result.current[1].remove(2)); - expect(result.current[0]).toStrictEqual(new Set([1])); - }); - - test('removeAll', () => { - const { result } = renderHook(() => useSet([1, 2, 3])); - act(() => result.current[1].removeAll([2, 3])); - expect(result.current[0]).toStrictEqual(new Set([1])); - }); - - test('clear', () => { - const { result } = renderHook(() => useSet([1, 2, 3])); - act(() => result.current[1].clear()); - expect(result.current[0]).toStrictEqual(new Set()); - }); -}); diff --git a/packages/react-hooks/src/set/demo/useSet.tsx b/packages/react-hooks/src/set/demo/useSet.tsx deleted file mode 100644 index 24039c0..0000000 --- a/packages/react-hooks/src/set/demo/useSet.tsx +++ /dev/null @@ -1,36 +0,0 @@ -import type { FunctionComponent } from 'react'; -import { Space, Button, List } from 'antd'; -import 'antd/dist/antd.css'; -import { useSet } from '..'; - -const Component: FunctionComponent = () => { - const [data, { add, remove, clear }] = useSet([]); - - return ( - <> - - ( - - {item} - - - )} - /> - - - - - - - ); -}; - -export default Component; diff --git a/packages/react-hooks/src/set/index.ts b/packages/react-hooks/src/set/index.ts deleted file mode 100644 index c612183..0000000 --- a/packages/react-hooks/src/set/index.ts +++ /dev/null @@ -1,46 +0,0 @@ -/* eslint-disable no-restricted-syntax */ -import { useMethodsNative } from '../methods'; - -const clone = (set: Set) => new Set(set.values()); - -function createReducers() { - return { - add(state: Set, item: T) { - if (state.has(item)) { - return state; - } - - return clone(state).add(item); - }, - addAll(state: Set, items: Iterable) { - const output = clone(state); - for (const item of items) { - output.add(item); - } - return output; - }, - remove(state: Set, item: T) { - if (state.has(item)) { - const output = clone(state); - output.delete(item); - return output; - } - - return state; - }, - removeAll(state: Set, items: Iterable) { - const output = clone(state); - for (const item of items) { - output.delete(item); - } - return output; - }, - clear() { - return new Set(); - }, - }; -} - -export function useSet(initialValue: Iterable = []) { - return useMethodsNative(createReducers(), () => new Set(initialValue)); -} diff --git a/packages/react-hooks/src/set/useArray.md b/packages/react-hooks/src/set/useArray.md deleted file mode 100644 index 226f152..0000000 --- a/packages/react-hooks/src/set/useArray.md +++ /dev/null @@ -1,41 +0,0 @@ ---- -title: useSet -nav: - title: Set - path: /hook -order: 1 ---- - -# useSet - -管理 Set 的 hook - - - -# API - -```typescript -const [ - state, - methods, -] = useSet( - initialValue?: T[], -); -``` - -# 返回值 - -| 参数 | 说明 | 类型 | -| ------- | ------ | ------- | -| state | 状态值 | boolean | -| methods | 方法集 | object | - -# methods - -| 属性 | 说明 | 类型 | -| --------- | ---- | --------------------------- | -| add | - | (item: T) => void | -| addAll | - | (item: Iterable) => void | -| remove | - | (item: T) => void | -| removeAll | - | (item: Iterable) => void | -| clear | - | () => void | diff --git a/packages/react-hooks/src/size/demo/useSize.tsx b/packages/react-hooks/src/size/demo/useSize.tsx deleted file mode 100644 index c54ce47..0000000 --- a/packages/react-hooks/src/size/demo/useSize.tsx +++ /dev/null @@ -1,28 +0,0 @@ -/** - * title: 使用ref监听元素尺寸变化 - */ - -import type { FunctionComponent } from 'react'; -import { useRef } from 'react'; -import { Space } from 'antd'; -import 'antd/dist/antd.css'; -import { useSize } from '..'; - -const Component: FunctionComponent = () => { - const elRef = useRef(null); - const size = useSize(elRef); - - return ( - <> - - - 宽:{size.width} - 高:{size.height} - -