Skip to content

Commit f600670

Browse files
authored
feat: optimize SearchResult (#2)
* deps: add rc-util * feat: add empty * chore: update demo * chore: update jest config
1 parent 8b80c4a commit f600670

File tree

6 files changed

+67
-51
lines changed

6 files changed

+67
-51
lines changed

jest.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ module.exports = {
1616
// '\\.(css|less)$/': 'identity-obj-proxy',
1717
'\\.(css|less)$': '<rootDir>/tests/mocks/style-mock.js',
1818
'^lodash-es$': 'lodash',
19+
'^rc-util/es/(.*)$': 'rc-util/lib/$1',
1920
},
2021
}

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"lodash-es": "^4.17.21",
3232
"merge2": "^1.4.1",
3333
"postcss": "^8.4.16",
34+
"rc-util": "^5.27.1",
3435
"react": "^18.2.0",
3536
"react-dom": "^18.2.0"
3637
},

packages/desktop-ui/src/search-result/demo/base.tsx

+1-5
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,7 @@ export default () => {
2828

2929
return (
3030
<SearchResult onSearch={setKeyword}>
31-
{
32-
results.length
33-
? results.map(item => <p key={item}>{item}</p>)
34-
: <div className="empty">未匹配到搜索结果</div> // empty 是预设的样式
35-
}
31+
{results.map(item => <p key={item}>{item}</p>)}
3632
</SearchResult>
3733
)
3834
}

packages/desktop-ui/src/search-result/index.tsx

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import Input from 'antd/es/input'
2+
import defaultRenderEmpty from 'antd/es/config-provider/defaultRenderEmpty'
3+
import { ConfigContext } from 'antd/es/config-provider/context'
24
import type { InputProps } from 'antd/es/input'
35
import React from 'react'
46
import { ConditionInput } from '@template-pro/rc-ui'
7+
import toArray from 'rc-util/es/Children/toArray'
58
import classNames from 'classnames'
69
import { useBoolean, useClickAway, useControllableValue } from 'ahooks'
710
import { defaultPrefixCls } from '../constants'
@@ -33,6 +36,8 @@ const SearchResult = (props: SearchResultProps, ref: React.Ref<SearchResultActio
3336
{ onChange: onSearch }, { defaultValue: '' },
3437
)
3538

39+
const { renderEmpty } = React.useContext(ConfigContext)
40+
3641
const searchResultRef = React.useRef<HTMLDivElement>(null)
3742
const [visibleDropdown, {
3843
setTrue: openDropdown,
@@ -52,6 +57,8 @@ const SearchResult = (props: SearchResultProps, ref: React.Ref<SearchResultActio
5257

5358
React.useImperativeHandle(ref, () => ({ close: handlerClose }), [])
5459

60+
const arrayChildren = toArray(children)
61+
5562
return (
5663
<div
5764
className={classNames(prefixedClassName, className)}
@@ -73,7 +80,11 @@ const SearchResult = (props: SearchResultProps, ref: React.Ref<SearchResultActio
7380
{
7481
showChildren && (
7582
<div className={classNames(`${prefixedClassName}-result-wrapper`, resultWrapperClassName)}>
76-
{children}
83+
{
84+
arrayChildren.length
85+
? arrayChildren
86+
: (renderEmpty || defaultRenderEmpty)('SearchResult')
87+
}
7788
</div>
7889
)
7990
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
@import "../../styles/themes/index";
22
@import "~antd/es/input/style/index.less";
3+
@import "~antd/es/empty/style/index.less";
34

45
.@{cls-prefix}-search-result {
56
position: relative;
@@ -15,13 +16,5 @@
1516
overflow: overlay;
1617
background-color: #fff;
1718
box-shadow: 0 3px 6px rgba(0, 0, 0, .16);
18-
19-
&>.empty {
20-
display: flex;
21-
justify-content: center;
22-
align-items: center;
23-
height: 60px;
24-
color: @text-color-generally;
25-
}
2619
}
2720
}

0 commit comments

Comments
 (0)