如何制作带有react钩子的点进度指示器组件



我想制作一个带有react钩子的点进度指示器。所以我有四个点来表示进展。它从满到空,如果是四个点,它是绿色的,三个黄色的,两个红色的,一个红色的。我想控制用道具照亮的点的颜色和数量,并用反应钩来完成。

我能找到的唯一接近它的东西是代码笔上的"点进度条",但它仍然与我想要的不同。

我希望输出是,如果我通过0-25%,它只会亮一个红色的点,50-75%的黄色,以此类推

我已经尝试了一段时间,我走得最远的是:

const OnGoingRequest = ({ image, name, profession, progress, color }) => {
const [dotColor, setDotColor] = useState('#c4c4dd')
if (progress > 0 && progress <= 25) {
setDotColor('red')
} else if (progress > 25) {
setDotColor('blue')
}
return (
<EachReview>
<UserContainer>
<UserImage src={image} />
<UserInfoContainer>
<UserName>{name} </UserName>
<UserProffession>{profession} </UserProffession>
</UserInfoContainer>
</UserContainer>
<ReviewStatusContainer>
<Box>
<Score />
<Bar>
<Progress progress={progress} color={dotColor} />
<Dot />
<Dot />
<Dot />
<Dot />
</Bar>
</Box>
<ReviewStatusReachout>REACHOUT AGAIN</ReviewStatusReachout>
</ReviewStatusContainer>
</EachReview>
)
}
export default OnGoingRequest
const EachReview = styled.div`
display: grid;
grid-template-columns: 2.5fr 1fr;
padding: 25px 20px 15px 20px;
`
const UserContainer = styled.div`
display: flex;
align-items: center;
`
const UserImage = styled.img``
const UserInfoContainer = styled.div`
display: flex;
flex-direction: column;
margin-left: 10px;
`
const UserName = styled.p`
font-family: SFUIDisplay-Regular;
font-size: 16px;
font-weight: 500;
font-stretch: normal;
font-style: normal;
line-height: 1.19;
letter-spacing: normal;
text-align: left;
color: #000000;
margin-bottom: 5px;
`
const UserProffession = styled.p`
font-family: SFUIDisplay-Regular;
font-size: 14px;
font-weight: 500;
font-stretch: normal;
font-style: normal;
line-height: 1.21;
letter-spacing: normal;
text-align: left;
color: #6b737d;
margin: 0;
`
const ReviewStatusContainer = styled.div`
display: flex;
align-items: center;
justify-content: space-between;
`
const ReviewStatusReachout = styled.p`
font-family: SFUIDisplay-Regular;
font-size: 12px;
font-weight: 500;
font-stretch: normal;
font-style: normal;
line-height: 1.17;
letter-spacing: normal;
text-transform: uppercase;
text-align: right;
color: #30aabc;
margin: 0;
`
const Box = styled.div`
width: 200px;
height: 150px;
background: #63a3e3;
text-align: center;
`
const Score = styled.div`
font-size: 48px;
line-height: 88px;
color: #fff;
}
`
const Bar = styled.div`
background: #c4c4dd;
position: relative;
height: 20px;
width: 80%;
margin: 0 auto;
text-align: left;
`
const Progress = styled.div`
position: absolute;
height: 100%;
transition: width 0.4s ease-in;
width: ${props => props.progress};
background-color: ${props => props.color};
z-index: 1;
`
const Dot = styled.div`
position: relative;
width: 5%;
height: 20px;
margin-left: 16%;
background-color: rgba(99, 163, 227, 1);
/* background-color: rgba(199,12,127,.8);*/
/*display: inline-block; */
float: left;
z-index: 2;

进度是作为道具传递的,我正在根据它进行检查,但我得到了太多重新渲染的错误。我猜是因为在退货前检查。

我找到了解决方案。我收到1,2,3或4作为道具,我正在根据它进行渲染。

const OnGoingRequest = ({ image, name, profession, progress }) => {
return (
<EachReview>
<UserContainer>
<UserImage src={image} />
<UserInfoContainer>
<UserName>{name} </UserName>
<UserProffession>{profession} </UserProffession>
</UserInfoContainer>
</UserContainer>
<ReviewStatusContainer>
{
progress == 1 &&(
<DotsContainer>
<Dot color={'red'} />
</DotsContainer>)
}
{ progress === 2 && (
<DotsContainer>
<Dot color={'purple'} />
<Dot color={'purple'} />
</DotsContainer>
)
}
{
progress === 3 && (
<DotsContainer>
<Dot color={'yellow'} />
<Dot color={'yellow'} />
<Dot color={'yellow'} />
</DotsContainer>
) 
}
{
progress == 4 && ( 
<DotsContainer>
<Dot color={'green'} />
<Dot color={'green'} />
<Dot color={'green'} />
<Dot color={'green'} />
</DotsContainer>)
}
<ReviewStatusReachout>REACHOUT AGAIN</ReviewStatusReachout>
</ReviewStatusContainer>
</EachReview>
)
}
export default OnGoingRequest
const EachReview = styled.div`
display: grid;
grid-template-columns: 1.5fr 1fr;
padding: 25px 20px 15px 20px;
`
const UserContainer = styled.div`
display: flex;
align-items: center;
`
const UserImage = styled.img``
const UserInfoContainer = styled.div`
display: flex;
flex-direction: column;
margin-left: 10px;
`
const UserName = styled.p`
font-family: SFUIDisplay-Regular;
font-size: 16px;
font-weight: 500;
font-stretch: normal;
font-style: normal;
line-height: 1.19;
letter-spacing: normal;
text-align: left;
color: #000000;
margin-bottom: 5px;
`
const UserProffession = styled.p`
font-family: SFUIDisplay-Regular;
font-size: 14px;
font-weight: 500;
font-stretch: normal;
font-style: normal;
line-height: 1.21;
letter-spacing: normal;
text-align: left;
color: #6b737d;
margin: 0;
`
const ReviewStatusContainer = styled.div`
display: flex;
align-items: center;
justify-content: space-between;
`
const ReviewStatusReachout = styled.p`
font-family: SFUIDisplay-Regular;
font-size: 12px;
font-weight: 500;
font-stretch: normal;
font-style: normal;
line-height: 1.17;
letter-spacing: normal;
text-transform: uppercase;
text-align: right;
color: #30aabc;
margin: 0;
`
const DotsContainer = styled.div`
background: #fff;
position: relative;
height: 20px;
margin: 0 10px 0 0;
text-align: left;
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
justify-content: space-evenly;
align-items: center;
`
const Dot = styled.div`
position: relative;
width: 6px;
height: 6px;
border-radius: 50%;
background-color: ${props => props.color};
/*display: inline-block; */
float: left;
z-index: 2;
margin-right: 10px;
&:last-of-type{
margin-right: 0;
}
`

相关内容

  • 没有找到相关文章

最新更新