如何使带有反应钩子的虚线进度指示器组件?

时间:2019-11-08 05:10:02

标签: html css reactjs react-hooks

我想用点缀的方式制作点进度指示器。 因此,我有四个指示进度的点。它从满到空,如果是四个点,则为绿色,三个为黄色,两个为淡红色,一个为红色。 我想控制用道具照亮的点的颜色和数量,并通过反应钩做到这一点。

我能找到的唯一接近的东西是在Codepen上的“虚线进度条”,但它仍然与我想要的东西不同。

我期望输出为:如果我通过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 个答案:

答案 0 :(得分:0)

我找到了解决方案。我收到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;
  }
`