From e8317fd7495567c6fd5519097a5b1e7fff1ad06a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E7=82=BD=E9=94=AE?= <397201698@qq.com> Date: Tue, 27 Jul 2021 16:01:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=90=9C=E7=B4=A2=E6=A1=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .eslintrc.js | 5 +++ .gitignore | 0 .vscode/settings.json | 13 +++++++- README.md | 0 deploy | 0 index.html | 0 package-lock.json | 0 package.json | 0 src/App.css | 0 src/App.tsx | 0 src/api/api.d.ts | 0 src/api/repository.ts | 0 src/components/RepositoryItem.tsx | 0 src/components/RepositoryList.tsx | 26 +++++++++++----- src/components/RepositorySearchInput.tsx | 31 +++++++++++++++++++ .../style/RepositoryList.module.css | 0 src/context/RepositorySearchContext.ts | 23 ++++++++++++++ src/favicon.svg | 0 src/index.css | 0 src/logo.svg | 0 src/main.tsx | 0 src/utils/cache/RepositorySearch.ts | 0 src/vite-env.d.ts | 0 start.sh | 0 tsconfig.json | 0 vite.config.ts | 0 yarn.lock | 0 27 files changed, 90 insertions(+), 8 deletions(-) mode change 100644 => 100755 .eslintrc.js mode change 100644 => 100755 .gitignore mode change 100644 => 100755 .vscode/settings.json mode change 100644 => 100755 README.md mode change 100644 => 100755 deploy mode change 100644 => 100755 index.html mode change 100644 => 100755 package-lock.json mode change 100644 => 100755 package.json mode change 100644 => 100755 src/App.css mode change 100644 => 100755 src/App.tsx mode change 100644 => 100755 src/api/api.d.ts mode change 100644 => 100755 src/api/repository.ts mode change 100644 => 100755 src/components/RepositoryItem.tsx mode change 100644 => 100755 src/components/RepositoryList.tsx create mode 100755 src/components/RepositorySearchInput.tsx mode change 100644 => 100755 src/components/style/RepositoryList.module.css create mode 100755 src/context/RepositorySearchContext.ts mode change 100644 => 100755 src/favicon.svg mode change 100644 => 100755 src/index.css mode change 100644 => 100755 src/logo.svg mode change 100644 => 100755 src/main.tsx mode change 100644 => 100755 src/utils/cache/RepositorySearch.ts mode change 100644 => 100755 src/vite-env.d.ts mode change 100644 => 100755 start.sh mode change 100644 => 100755 tsconfig.json mode change 100644 => 100755 vite.config.ts mode change 100644 => 100755 yarn.lock diff --git a/.eslintrc.js b/.eslintrc.js old mode 100644 new mode 100755 index 33c8165..3ff777d --- a/.eslintrc.js +++ b/.eslintrc.js @@ -23,5 +23,10 @@ module.exports = { camelcase: 'off', 'no-unused-vars': 'warn', 'no-use-before-define': 'off' + }, + settings: { + react: { + version: '17.0.0' + } } } diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 diff --git a/.vscode/settings.json b/.vscode/settings.json old mode 100644 new mode 100755 index 48ea0e4..6c040cc --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,4 +1,15 @@ { "editor.defaultFormatter": "dbaeumer.vscode-eslint", - "editor.formatOnSave": true + "editor.formatOnSave": true, + "editor.tabSize": 2, + "eslint.trace.server": "verbose", + "eslint.validate": [ + "javascript", + "javascriptreact", + "typescript", + "typescriptreact", + "html", + "vue", + ], + "eslint.format.enable": true } \ No newline at end of file diff --git a/README.md b/README.md old mode 100644 new mode 100755 diff --git a/deploy b/deploy old mode 100644 new mode 100755 diff --git a/index.html b/index.html old mode 100644 new mode 100755 diff --git a/package-lock.json b/package-lock.json old mode 100644 new mode 100755 diff --git a/package.json b/package.json old mode 100644 new mode 100755 diff --git a/src/App.css b/src/App.css old mode 100644 new mode 100755 diff --git a/src/App.tsx b/src/App.tsx old mode 100644 new mode 100755 diff --git a/src/api/api.d.ts b/src/api/api.d.ts old mode 100644 new mode 100755 diff --git a/src/api/repository.ts b/src/api/repository.ts old mode 100644 new mode 100755 diff --git a/src/components/RepositoryItem.tsx b/src/components/RepositoryItem.tsx old mode 100644 new mode 100755 diff --git a/src/components/RepositoryList.tsx b/src/components/RepositoryList.tsx old mode 100644 new mode 100755 index 4522e4f..4ccd4c2 --- a/src/components/RepositoryList.tsx +++ b/src/components/RepositoryList.tsx @@ -1,9 +1,11 @@ -import React, { useEffect, useState } from 'react' +import React, { useEffect, useState, useContext } from 'react' import { search as SearchRepository } from '../api/repository' import RepopositoryItem from './RepositoryItem' import { RepositorySearchResultItem } from '../api/api' import classes from './style/RepositoryList.module.css' import { RepositorySearchCache } from '../utils/cache/RepositorySearch' +import RepositorySearchInput from './RepositorySearchInput' +import { RepositorySearchContext } from '../context/RepositorySearchContext' interface RepositoryListProps { @@ -21,7 +23,12 @@ interface RepositoryListProps { const DEFAULT_PER_PAGE = 30 export default function RepositoryList (props:RepositoryListProps) { - const cache = RepositorySearchCache(props.keyword, props.max_cache_page) + useContext(RepositorySearchContext) + + /** 搜索关键词 */ + const [keyword, setKeyword] = useState(props.keyword) + + const cache = RepositorySearchCache(keyword, props.max_cache_page) /** 当前页 */ const [current_page, setCurrentPage] = useState(1) @@ -36,10 +43,11 @@ export default function RepositoryList (props:RepositoryListProps) { /** * 调用搜索接口 + * @param {string} keyword 搜索关键词 * @param {number} page 页码 * @returns {Promise} */ - async function search (page:number):Promise { + async function search (keyword:string, page:number):Promise { setLoading(true) const cache_list = cache.read(page) @@ -54,7 +62,7 @@ export default function RepositoryList (props:RepositoryListProps) { // 没有缓存,调用接口 try { /* eslint-disable */ - var result = await SearchRepository(props.keyword, page) + var result = await SearchRepository(keyword, page) } catch (err) { console.log('fetch',err); @@ -80,20 +88,23 @@ export default function RepositoryList (props:RepositoryListProps) { } useEffect(() => { - search(1) + search(keyword,1) }, []) function onClickPrev(){ - search(current_page-1) + search(keyword,current_page-1) } function onClickNext(){ - search(current_page+1) + search(keyword,current_page+1) } return ( +
+
+
    { list.map(item => ) @@ -114,5 +125,6 @@ export default function RepositoryList (props:RepositoryListProps) {
+
) } diff --git a/src/components/RepositorySearchInput.tsx b/src/components/RepositorySearchInput.tsx new file mode 100755 index 0000000..0e165f0 --- /dev/null +++ b/src/components/RepositorySearchInput.tsx @@ -0,0 +1,31 @@ +import React, { useContext } from 'react' +import { RepositorySearchContext } from '../context/RepositorySearchContext' + +export default function RepositorySearchInput () { + const context = useContext(RepositorySearchContext) + + return ( +
+ + + +
+ ) +} diff --git a/src/components/style/RepositoryList.module.css b/src/components/style/RepositoryList.module.css old mode 100644 new mode 100755 diff --git a/src/context/RepositorySearchContext.ts b/src/context/RepositorySearchContext.ts new file mode 100755 index 0000000..c31def3 --- /dev/null +++ b/src/context/RepositorySearchContext.ts @@ -0,0 +1,23 @@ +import React from 'react' + +export const RepositorySearchContext = React.createContext({ + + /** + * 调用搜索接口 + * @param {string} keyword 搜索关键词 + * @param {number} page 页码 + */ + search (keyword:string, page:number) {}, + + /** + * 更新搜索关键词 + * @param {string} keyword 搜索关键词 + */ + setKeyword (keyword:string) {}, + + /** 搜索关键词 */ + keyword: '', + + /** 接口调用中 */ + loading: false +}) diff --git a/src/favicon.svg b/src/favicon.svg old mode 100644 new mode 100755 diff --git a/src/index.css b/src/index.css old mode 100644 new mode 100755 diff --git a/src/logo.svg b/src/logo.svg old mode 100644 new mode 100755 diff --git a/src/main.tsx b/src/main.tsx old mode 100644 new mode 100755 diff --git a/src/utils/cache/RepositorySearch.ts b/src/utils/cache/RepositorySearch.ts old mode 100644 new mode 100755 diff --git a/src/vite-env.d.ts b/src/vite-env.d.ts old mode 100644 new mode 100755 diff --git a/start.sh b/start.sh old mode 100644 new mode 100755 diff --git a/tsconfig.json b/tsconfig.json old mode 100644 new mode 100755 diff --git a/vite.config.ts b/vite.config.ts old mode 100644 new mode 100755 diff --git a/yarn.lock b/yarn.lock old mode 100644 new mode 100755