-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
259 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
@import '../../styles/variables.scss'; | ||
|
||
.vant-badge { | ||
display: inline-block; | ||
box-sizing: border-box; | ||
min-width: $badge-size; | ||
padding: $badge-padding; | ||
color: $badge-color; | ||
font-weight: $badge-font-weight; | ||
font-size: $badge-font-size; | ||
font-family: $badge-font-family; | ||
line-height: 1.2; | ||
text-align: center; | ||
background-color: $badge-background-color; | ||
border: $badge-border-width solid $white; | ||
border-radius: $border-radius-max; | ||
|
||
&__fixed { | ||
position: absolute; | ||
top: 0; | ||
right: 0; | ||
transform: translate(50%, -50%); | ||
transform-origin: 100%; | ||
} | ||
|
||
&__dot { | ||
width: $badge-dot-size; | ||
min-width: 0; | ||
height: $badge-dot-size; | ||
background-color: $badge-dot-color; | ||
border-radius: 100%; | ||
} | ||
|
||
&__wrapper { | ||
position: relative; | ||
display: inline-block; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import React from 'react'; | ||
import Badge from '.'; | ||
import '../../styles/stories.scss'; | ||
|
||
export default { | ||
title: 'Badge', | ||
component: Badge | ||
}; | ||
const style = { | ||
width: '50px', | ||
height: '50px', | ||
backgroundColor: '#f2f3f5', | ||
borderRadius: '8px' | ||
}; | ||
const Child = () => { | ||
return <div style={style} />; | ||
}; | ||
const Basic = () => { | ||
return ( | ||
<Badge content={12} max={99}> | ||
<Child /> | ||
</Badge> | ||
); | ||
}; | ||
const BasicHigher = () => { | ||
return ( | ||
<Badge content={120} max={99}> | ||
<Child /> | ||
</Badge> | ||
); | ||
}; | ||
const Color = () => { | ||
return ( | ||
<Badge content='120' max='102' color='#1989FA'> | ||
<Child /> | ||
</Badge> | ||
); | ||
}; | ||
const Dot = () => { | ||
return ( | ||
<Badge content={120} max={99} color='#1989FA' dot> | ||
<Child /> | ||
</Badge> | ||
); | ||
}; | ||
const FCContent = () => { | ||
return <div>FCContent</div>; | ||
}; | ||
const WithFC = () => { | ||
// console.log(+FCContent) | ||
return ( | ||
<Badge content={FCContent} max={99} color='#1989FA'> | ||
<Child /> | ||
</Badge> | ||
); | ||
}; | ||
class ClassContent extends React.Component { | ||
render() { | ||
return <div>ClassContent</div>; | ||
} | ||
} | ||
const WithClass = () => { | ||
return ( | ||
<Badge content={ClassContent} max={99} color='#1989FA'> | ||
<Child /> | ||
</Badge> | ||
); | ||
}; | ||
export const All = () => { | ||
return ( | ||
<div className='storybook__container badge'> | ||
<Basic /> | ||
<BasicHigher /> | ||
<Color /> | ||
<Dot /> | ||
<WithFC /> | ||
<WithClass /> | ||
</div> | ||
); | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import React, { FC, ReactNode } from 'react'; | ||
import './index.scss'; | ||
import classnames from '../../utils/classNames'; | ||
export interface BadgeProps { | ||
content: string | number | ReactNode; | ||
color?: string; | ||
dot?: boolean; | ||
max: number | string; | ||
} | ||
const baseClass = 'vant-badge'; | ||
const Badge: FC<BadgeProps> = ({ | ||
content, | ||
color = '#ee0a24', | ||
dot = false, | ||
max, | ||
children | ||
}) => { | ||
const renderContent = () => { | ||
if (dot) return ''; | ||
if (content) { | ||
if (!!max && Number.isInteger(+content) && +content > max) { | ||
return <>{`${max}+`}</>; | ||
} else { | ||
if (typeof content === 'string' || typeof content === 'number') { | ||
return <>{`${content}`}</>; | ||
} else { | ||
const ContentComponent = content as FC; | ||
return <>{ContentComponent ? <ContentComponent /> : null}</>; | ||
} | ||
} | ||
} | ||
return ''; | ||
}; | ||
const renderBadge = () => { | ||
if (content || dot) { | ||
return ( | ||
<div | ||
className={classnames(baseClass, [{ dot }, { fixed: !!children }])} | ||
style={{ background: color }} | ||
> | ||
{renderContent()} | ||
</div> | ||
); | ||
} | ||
}; | ||
if (children) { | ||
return ( | ||
<div className='vant-badge__wrapper'> | ||
{children} | ||
{renderBadge()} | ||
</div> | ||
); | ||
} | ||
return <>{renderBadge()}</>; | ||
}; | ||
export default Badge; |
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 |
---|---|---|
|
@@ -145,3 +145,7 @@ body { | |
font-weight: 100; | ||
} | ||
} | ||
.badge{ | ||
display: flex; | ||
justify-content: space-around; | ||
} |
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