gpt4 book ai didi

html - 如何使用 react Hook 制作点状进度指示器组件?

转载 作者:行者123 更新时间:2023-11-27 23:05:29 27 4
gpt4 key购买 nike

我想用 react-hooks 制作一个点状进度指示器。所以我有四个点表示进度。它从满到空,如果它是四个点,它是绿色的,三个黄色的,两个 - 红色的,一个是红色的。我想控制用 Prop 点亮的点的颜色和数量,并用 react-hooks 来完成。

我能找到的唯一接近它的是codepen上的“Dotted progress bar”,但它仍然与我想要的不同。

我希望输出是,如果我通过 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;

progress 作为 props 传递,我正在根据它进行检查,但我得到了太多重新渲染的错误。估计是返回前检查的缘故。

最佳答案

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

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;
}
`

关于html - 如何使用 react Hook 制作点状进度指示器组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58760713/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com