Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
31 changes: 15 additions & 16 deletions src/packages/badge/badge.taro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,17 @@ export const Badge: FunctionComponent<Partial<BadgeProps>> = (props) => {
right,
fill,
size,
} = {
...defaultProps,
...props,
}
} = { ...defaultProps, ...props }

const classPrefix = 'nut-badge'
const isHarmony = harmony()
const classes = classNames(classPrefix, className)
const badgeRef = useRef(null)
const [contentStyle, setContentStyle] = useState({})

function content() {
if (dot || typeof value === 'object' || value === 0) return null
if (typeof value === 'number' && typeof max === 'number') {
function getContent() {
if (dot || value === 0) return null
if (typeof value === 'number') {
return max < value ? `${max}+` : `${value}`
}
return value
Expand All @@ -70,7 +68,7 @@ export const Badge: FunctionComponent<Partial<BadgeProps>> = (props) => {
[`${classPrefix}-sup`]: isNumber() || isString() || dot,
[`${classPrefix}-number`]: isNumber(),
[`${classPrefix}-one`]:
typeof content() === 'string' && `${content()}`?.length === 1,
typeof getContent() === 'string' && `${getContent()}`?.length === 1,
[`${classPrefix}-dot`]: dot,
[`${classPrefix}-dot-${size}`]: dot,
[`${classPrefix}-${fill}`]: fill === 'outline',
Expand All @@ -82,13 +80,14 @@ export const Badge: FunctionComponent<Partial<BadgeProps>> = (props) => {
getPositionStyle()
}
}, [])

const getPositionStyle = async () => {
const style: CSSProperties = {}
style.top = pxTransform(Number(top) || 0)
const dir = rtl ? 'left' : 'right'
style[dir] = isHarmony
? pxTransform(Number(right))
: `${Number(right) || 0}px`
const style: CSSProperties = {
top: pxTransform(Number(top) || 0),
[rtl ? 'left' : 'right']: isHarmony
? pxTransform(Number(right))
: `${Number(right) || 0}px`,
}
setContentStyle(style)
}

Expand All @@ -106,9 +105,9 @@ export const Badge: FunctionComponent<Partial<BadgeProps>> = (props) => {
</View>
)}
{children}
{!isIcon() && (
{!isIcon() && (getContent() || dot) && (
<View className={contentClasses} style={contentStyle}>
{content()}
{getContent()}
</View>
)}
</View>
Expand Down
31 changes: 14 additions & 17 deletions src/packages/badge/badge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const defaultProps = {
fill: 'solid',
size: 'large',
} as BadgeProps

export const Badge: FunctionComponent<Partial<BadgeProps>> = (props) => {
const rtl = useRtl()
const {
Expand All @@ -27,16 +28,14 @@ export const Badge: FunctionComponent<Partial<BadgeProps>> = (props) => {
right,
fill,
size,
} = {
...defaultProps,
...props,
}
} = { ...defaultProps, ...props }

const classPrefix = 'nut-badge'
const classes = classNames(classPrefix, className)

function content() {
if (dot || typeof value === 'object' || value === 0) return null
if (typeof value === 'number' && typeof max === 'number') {
const getContent = () => {
if (dot || value === 0) return null
if (typeof value === 'number') {
return max < value ? `${max}+` : `${value}`
}
return value
Expand All @@ -58,19 +57,17 @@ export const Badge: FunctionComponent<Partial<BadgeProps>> = (props) => {
[`${classPrefix}-sup`]: isNumber() || isString() || dot,
[`${classPrefix}-number`]: isNumber(),
[`${classPrefix}-one`]:
typeof content() === 'string' && `${content()}`?.length === 1,
typeof getContent() === 'string' && `${getContent()}`?.length === 1,
[`${classPrefix}-dot`]: dot,
[`${classPrefix}-dot-${size}`]: dot,
[`${classPrefix}-${fill}`]: fill === 'outline',
[`${classPrefix}-content`]: children,
})
const getPositionStyle = () => {
const style: CSSProperties = {}
style.top = `${Number(top) || 0}px`
const dir = rtl ? 'left' : 'right'
style[dir] = `${Number(right) || parseFloat(String(right)) || 0}px`
return style
}

const getPositionStyle = (): CSSProperties => ({
top: `${Number(top) || 0}px`,
[rtl ? 'left' : 'right']: `${Number(right) || 0}px`,
})

return (
<div className={classes} style={style}>
Expand All @@ -86,9 +83,9 @@ export const Badge: FunctionComponent<Partial<BadgeProps>> = (props) => {
</div>
)}
{children}
{!isIcon() && (
{!isIcon() && (getContent() || dot) && (
<div className={contentClasses} style={getPositionStyle()}>
{content()}
{getContent()}
</div>
)}
</div>
Expand Down
12 changes: 2 additions & 10 deletions src/packages/tabbaritem/tabbaritem.scss
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,14 @@
&-active {
color: $tabbar-active-color;

.nut-tabbar-item-icon-box {
.nut-tabbar-item-icon-box,
.nut-icon {
/* #ifdef harmony*/
color: $tabbar-active-color;
/* #endif*/
/* #ifndef harmony*/
color: inherit;
/* #endif*/

.nut-icon {
/* #ifdef harmony*/
color: $tabbar-active-color;
/* #endif*/
/* #ifndef harmony*/
color: inherit;
/* #endif*/
}
}
}
}
35 changes: 17 additions & 18 deletions src/packages/tabbaritem/tabbaritem.taro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@ export const TabbarItem: FunctionComponent<Partial<TabbarItemProps>> = (
color: ctx?.activeColor,
}

const renderTitleText = () => {
return (
title && (
<View
className={titleClass}
style={{
color: active ? ctx?.activeColor : ctx?.inactiveColor,
}}
>
{title}
</View>
)
)
}

return (
<View
className={tabbarItemClass}
Expand All @@ -91,26 +106,10 @@ export const TabbarItem: FunctionComponent<Partial<TabbarItemProps>> = (
)}
</View>
</Badge>
<View
className={titleClass}
style={{
color: active ? ctx?.activeColor : ctx?.inactiveColor,
}}
>
{title}
</View>
{renderTitleText()}
</>
) : (
<Badge {...badgeProps}>
<View
className={titleClass}
style={{
color: active ? ctx?.activeColor : ctx?.inactiveColor,
}}
>
{title}
</View>
</Badge>
<Badge {...badgeProps}>{renderTitleText()}</Badge>
)}
</View>
)
Expand Down
10 changes: 6 additions & 4 deletions src/packages/tabbaritem/tabbaritem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ export const TabbarItem: FunctionComponent<Partial<TabbarItemProps>> = (
color: ctx?.activeColor,
}

const renderTitleText = () => {
return title && <div className={titleClass}>{title}</div>
}

return (
<div
className={tabbarItemClass}
Expand All @@ -84,12 +88,10 @@ export const TabbarItem: FunctionComponent<Partial<TabbarItemProps>> = (
<Badge {...badgeProps}>
<div className={boxPrefix}>{icon}</div>
</Badge>
<div className={titleClass}>{title}</div>
{renderTitleText()}
</>
) : (
<Badge {...badgeProps}>
<div className={titleClass}>{title}</div>
</Badge>
<Badge {...badgeProps}>{renderTitleText()}</Badge>
)}
</div>
)
Expand Down