【问题标题】:How to click the card and while clicking the next button it will redirect based on selected card?如何单击卡片并在单击下一步按钮时将根据所选卡片重定向?
【发布时间】:2021-01-21 11:24:24
【问题描述】:

在此代码中,有两张卡片,当点击卡片时,它将被标记为选中。点击卡片,点击下一步按钮时,它将根据所选卡片重定向。

在此代码中,当单击一张卡片时,它同时被选中,这也是一个问题。只有点击按钮才会重定向到所选卡片的页面。

用于三个页面的路由:CardSelectOptionPage、SuccessPage、FailurePage。

UI 设计:Antd 设计。

这里是代码沙盒链接:https://codesandbox.io/s/select-card-moloe?file=/src/index.js

import { Button, Card, Col, Image, Row, Typography } from "antd";
import React, { useState } from "react";
import { icons } from "../assets/icons";
import "./CardSelectOption.scss";

const CardSelectOption = () => {
   const [clicked, setClicked] = useState(false);
   const chooseOptions = [
    {
       title: "Yes",
       description: "I need to redirect into one Page (Successfull Page)",
       icon: clicked ? icons.check : icons.EmptyBox,
    },
    {
       title: "No",
       description: "I need to redirect into another Page (UnSuccessfull Page)",
       icon: clicked ? icons.check : icons.EmptyBox,
    },
   ];
    
   return (
      <Row className="select_page_container">
        <Col xl={18} md={18} sm={24} xs={24} className="select_page_header">
           <h1>Select One Option</h1>
        </Col>
        <Row className="select_card_section">
          {chooseOptions.map((option, index) => (
          <Col xl={8} lg={8} sm={24} key={index} className="select_cards">
           <Card onClick={() => setClicked(!clicked)} className="card">
            <Row className="card_body">
              <Col xl={7} lg={6} md={6} sm={24} xs={24} className="icon_part">
                  <Image src={option.icon} preview={false} />
              </Col>
              <Col xl={17} md={18} sm={24} xs={24} className="card_content">
                <Col xl={16} lg={16} md={16} className="title">
                  <Typography.Title level={4}>
                    {option.title}
                  </Typography.Title>
                </Col>
                <Col xl={22} md={22} sm={24} xs={24} className="description">
                  <Typography.Text>{option.description}</Typography.Text>
                </Col>
               </Col>
              </Row>
            </Card>
          </Col>
         ))}
        </Row>
        <Col xl={12} md={12} sm={24} xs={24} className="submit_button">
          <Button size="large" type="primary">
           Next
          </Button>
        </Col>
       </Row>
     );
    };
    
    export default CardSelectOption;

【问题讨论】:

    标签: javascript reactjs react-router antd


    【解决方案1】:

    根据您的点击状态重定向

    你可以这样导航

    const CardSelectOption = (props) => {
    const [clicked, setClicked] = useState(false);
    const chooseOptions = [
    {
      title: "Yes",
      description: "I need to redirect into one Page (Successfull Page)",
      icon: clicked ? icons.check : icons.EmptyBox,
      link: "/success"
    },
    {
      title: "No",
      description: "I need to redirect into another Page (UnSuccessfull Page)",
      icon: !clicked ? icons.check : icons.EmptyBox,
      link: "/failure"
    }
    ];
    
    const navigate = () => {
    if (clicked === true) {
      props.history.push("/Success");
    } else {
      props.history.push("/Failure");
    }
     };
    
    return (
    <Row className="select_page_container">
      <Col xl={12} md={12} sm={24} xs={24} className="select_page_header">
        <Typography.Title level={4}>Select One Option</Typography.Title>
      </Col>
      <Row className="select_card_section">
        {chooseOptions.map((option, index) => (
          <Col xl={8} lg={8} sm={10} key={index} className="select_cards">
            <Card onClick={() => setClicked(!clicked)} className="card">
              <Row className="card_body">
                <Col xl={7} lg={6} md={6} sm={7} xs={7} className="icon_part">
                  <Image src={option.icon} preview={false} />
                </Col>
                <Col xl={17} md={18} sm={17} xs={17} className="card_content">
                  <Col xl={16} lg={16} md={16} className="title">
                    <Typography.Title level={4}>
                      {option.title}
                    </Typography.Title>
                  </Col>
                  <Col xl={22} md={22} sm={24} xs={24} className="description">
                    <Typography.Text>{option.description}</Typography.Text>
                  </Col>
                </Col>
              </Row>
            </Card>
          </Col>
        ))}
      </Row>
      <Col xl={12} md={12} sm={24} xs={24} className="submit_button">
        <Button size="large" type="primary" onClick={navigate}>
          Next
        </Button>
      </Col>
    </Row>
     );
     };
    

    从你的按钮调用这个函数!

    【讨论】:

    • 在这段代码中,当点击一张卡片时,它被选中,这也是一个问题。
    • @Balaji 我修好了,这是我的解决方案
    • 非常感谢。我得到了它。这是正确的方法还是有其他方法?
    • @Balaji 这种方法没有错!但可能有比这更有效的方法。这里工作代码沙箱 - codesandbox.io/s/select-card-forked-q2hk7
    • 解决这个问题的有效方法是什么?
    猜你喜欢
    • 2021-05-31
    • 1970-01-01
    • 2020-12-09
    • 2020-12-24
    • 2012-11-17
    • 1970-01-01
    • 2020-12-18
    • 2021-08-26
    • 1970-01-01
    相关资源
    最近更新 更多