Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: components can be searched #2757

Open
wants to merge 1 commit into
base: feat_v3.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions packages/nutui-taro-demo/src/pages/index/index.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
import Taro from '@tarojs/taro'
import { useState } from 'react'
import { View, Image, Text, ScrollView, Button, Input, Video } from '@tarojs/components'
import pkg from '@/packages/../config.json'
import packageJson from '@/packages/../../package.json'
import './index.scss'
// import Schema from 'async-validator'

const navs = pkg.nav
// console.log(navs)


// hack taro load button xml
console.log(Button, Input, Video )

// try {
// console.log('xxx', Schema)
// } catch (e) {}
const Index = () => {
const [search, setSearch] = useState()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

建议改进状态声明

当前的状态声明存在以下问题:

  1. 缺少初始值,可能导致未定义状态
  2. 缺少类型注解,不利于代码维护

建议按如下方式修改:

- const [search, setSearch] = useState()
+ const [search, setSearch] = useState<string>('')
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const [search, setSearch] = useState()
const [search, setSearch] = useState<string>('')

const gotoNext = (name: string, enName: string) => {
// 跳转到目的页面,打开新页面
Taro.navigateTo({
Expand Down Expand Up @@ -55,6 +51,9 @@ const Index = () => {
</View>
</View>
</View>
<Input value={search} onInput={(e) => {
setSearch(e.detail.value)
}} />
<View className='index-components'>
{navs.map((nav) => (
<View key={nav.enName} className='index-components-item'>
Expand All @@ -63,7 +62,7 @@ const Index = () => {
)}
<View className='index-components-sublist'>
{nav.packages.map((com) =>
com.show && com.taro && com.version === '3.0.0' ? (
com.show && com.taro && com.version === '3.0.0' && (!search || search == com.name.toLowerCase()) ? (
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

优化搜索过滤逻辑

当前的过滤逻辑存在以下问题:

  1. 搜索比较区分大小写,可能影响用户体验
  2. 条件判断过于复杂,影响代码可读性
  3. 静态条件判断在 map 循环中重复执行,影响性能

建议按如下方式重构:

- com.show && com.taro && com.version === '3.0.0' && (!search || search == com.name.toLowerCase()) ? (
+ const isValidComponent = (com) => com.show && com.taro && com.version === '3.0.0';
+ const matchesSearch = (com) => !search || com.name.toLowerCase().includes(search.toLowerCase());
+ 
+ {nav.packages
+   .filter(isValidComponent)
+   .filter(matchesSearch)
+   .map((com) => (

Committable suggestion skipped: line range outside the PR's diff.

<View
key={com.name}
className='index-components-sublist-item'
Expand Down
Loading