-
Notifications
You must be signed in to change notification settings - Fork 269
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
fix(badge): 四端适配 #2620
Merged
Merged
fix(badge): 四端适配 #2620
Changes from 4 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
fe1a648
fix(badge): 适配多端
irisSong d906bc9
fix: badge适配
irisSong 9a61a86
fix: 鸿蒙适配
irisSong ccd7ed7
fix: 移除无用的rtl文件
irisSong c3e4b2e
fix: 合并3.0
irisSong 0ff6260
fix: 修改单元测试
irisSong 02f4881
Merge branch '3.0' into dev-harmony-badge
irisSong cd9032a
fix: cr 修改
irisSong 0ba9051
Merge branch '3.0' into dev-harmony-badge
irisSong 91d4857
fix: icon 适配
irisSong d4adac8
fix: update icons-react-taro version
irisSong 847c2ed
chore: update pnpm-lock.yaml
irisSong 7151d2b
Merge branch '3.0' into dev-harmony-badge
irisSong a4f2168
fix: 合并v3.0
irisSong 526dc88
Merge branch '3.0' into dev-harmony-badge
irisSong 9194598
Merge remote-tracking branch 'upstream/V3.0' into dev-harmony-badge
irisSong d610397
fix: 合并3.0
irisSong 969e5ae
Merge remote-tracking branch 'upstream/V3.0' into dev-harmony-badge
irisSong 951e521
fix: 修改主题色
irisSong 0236ecc
fix(badge): harmony兼容
irisSong 3b022fa
Merge branch 'V3.0' into dev-harmony-badge
xiaoyatong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,18 @@ | ||
import React, { CSSProperties, FunctionComponent, ReactNode } from 'react' | ||
import React, { | ||
CSSProperties, | ||
FunctionComponent, | ||
ReactNode, | ||
useEffect, | ||
useRef, | ||
useState, | ||
} from 'react' | ||
import classNames from 'classnames' | ||
import { View } from '@tarojs/components' | ||
import { BasicComponent, ComponentDefaults } from '@/utils/typings' | ||
import { useRtl } from '@/packages/configprovider/index.taro' | ||
import pxTransform from '@/utils/px-transform' | ||
import { getRectByTaro } from '@/utils/get-rect-by-taro' | ||
import { harmony, rn } from '@/utils/platform-taro' | ||
|
||
export type BadgeFill = 'solid' | 'outline' | ||
export interface BadgeProps extends BasicComponent { | ||
|
@@ -42,9 +52,9 @@ export const Badge: FunctionComponent<Partial<BadgeProps>> = (props) => { | |
...props, | ||
} | ||
const classPrefix = 'nut-badge' | ||
const classes = classNames(classPrefix, className, { | ||
[`${classPrefix}-${fill}`]: fill === 'outline', | ||
}) | ||
const classes = classNames(classPrefix, className) | ||
const badgeRef = useRef(null) | ||
const [contentStyle, setContentStyle] = useState({}) | ||
|
||
function content() { | ||
if (dot || typeof value === 'object' || value === 0) return null | ||
|
@@ -66,41 +76,73 @@ export const Badge: FunctionComponent<Partial<BadgeProps>> = (props) => { | |
if (typeof value === 'string' && value) return value | ||
} | ||
|
||
const contentClasses = classNames( | ||
{ [`${classPrefix}-dot`]: dot }, | ||
`${classPrefix}-content`, | ||
{ [`${classPrefix}-sup`]: isNumber() || isString() || dot }, | ||
{ | ||
[`${classPrefix}-one`]: | ||
typeof content() === 'string' && `${content()}`?.length === 1, | ||
const contentClasses = classNames(`${classPrefix}-content`, { | ||
[`${classPrefix}-sup`]: isNumber() || isString() || dot, | ||
[`${classPrefix}-one`]: | ||
typeof content() === 'string' && `${content()}`?.length === 1, | ||
[`${classPrefix}-dot`]: dot, | ||
[`${classPrefix}-${fill}`]: fill === 'outline', | ||
}) | ||
|
||
useEffect(() => { | ||
if (badgeRef.current) { | ||
getPositionStyle() | ||
} | ||
) | ||
const getStyle = () => { | ||
}, [badgeRef.current]) | ||
irisSong marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const getPositionStyle = async () => { | ||
const style: CSSProperties = {} | ||
style.top = `${Number(top) || parseFloat(String(top)) || 0}px` | ||
const dir = rtl ? 'left' : 'right' | ||
style[dir] = `${Number(right) || parseFloat(String(right)) || 0}px` | ||
style.top = pxTransform(Number(-top) || 0) | ||
if (rn()) { | ||
irisSong marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const reacts = await getRectByTaro(badgeRef.current) | ||
style.left = | ||
reacts?.width && reacts?.width > Number(right) | ||
? pxTransform(reacts.width - Number(right)) | ||
: 0 | ||
} else { | ||
const dir = rtl ? 'left' : 'right' | ||
style[dir] = harmony() | ||
? pxTransform(Number(right)) | ||
: `${Number(right) || parseFloat(String(right)) || 0}px` | ||
} | ||
setContentStyle(style) | ||
} | ||
|
||
const getStyle = () => { | ||
const style: CSSProperties = {} | ||
if (color) { | ||
if (fill === 'outline') { | ||
style.color = color | ||
style.background = '#fff' | ||
style.backgroundColor = '#fff' | ||
if (!color?.includes('gradient')) { | ||
style.border = `1px solid ${color}` | ||
style.borderColor = color | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion [建议] 避免在样式中硬编码颜色值,提升组件的可定制性 在第 115 行, 建议允许通过属性传入背景颜色,或者使用一个默认值,同时提供自定义的可能性: - style.backgroundColor = '#fff'
+ style.backgroundColor = backgroundColor || '#fff' 并在 export interface BadgeProps extends BasicComponent {
// 其他属性...
backgroundColor?: string
} |
||
} | ||
} else { | ||
style.color = '#fff' | ||
style.background = color | ||
style.backgroundColor = color | ||
} | ||
} | ||
return style | ||
} | ||
|
||
return ( | ||
<View className={classes} style={style}> | ||
{isIcon() && <View className={`${classPrefix}-icon`}>{value}</View>} | ||
<View className={classes} style={style} ref={badgeRef}> | ||
{isIcon() && ( | ||
<View | ||
className={classNames(`${classPrefix}-content`, { | ||
[`${classPrefix}-icon`]: true, | ||
[`${classPrefix}-icon-rtl`]: rtl, | ||
})} | ||
style={contentStyle} | ||
> | ||
{value} | ||
</View> | ||
)} | ||
{children} | ||
{!isIcon() && ( | ||
<View className={contentClasses} style={getStyle()}> | ||
<View | ||
className={contentClasses} | ||
style={{ ...contentStyle, ...getStyle() }} | ||
> | ||
{content()} | ||
</View> | ||
)} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
应该写 min-width:1px; #ifndef harmony min-width:auto。因为组件库纯H5 和 多端同一套样式文件。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已修改