首页 > 其他分享 >react+antd 需求demo实现

react+antd 需求demo实现

时间:2023-12-14 23:00:14浏览次数:22  
标签:box index const name selectedBoxes demo react record antd

BoxSelectionComponent.tsx

import React, { useState } from 'react';
import { Modal, Button, Table, message } from 'antd';

const BoxSelectionComponent: React.FC = () => {
  const [modalVisible, setModalVisible] = useState(false);
  const [selectedBoxes, setSelectedBoxes] = useState<string[]>([]);

  const boxes = ['A', 'B', 'C', 'D', 'E'];

  const showModal = () => {
    setModalVisible(true);
  };

  const handleBoxSelect = (box: string) => {
    const isSelected = selectedBoxes.includes(box);
    if (isSelected) {
      const updatedSelection = selectedBoxes.filter((selectedBox) => selectedBox !== box);
      setSelectedBoxes(updatedSelection);
    } else {
      if (selectedBoxes.length < 3) {
        setSelectedBoxes([...selectedBoxes, box]);
      } else {
        message.warning('最多可以选中三个盒子');
      }
    }
  };

  const handleDelete = (box: string) => {
    const updatedSelection = selectedBoxes.filter((selectedBox) => selectedBox !== box);
    setSelectedBoxes(updatedSelection);

    if (updatedSelection.length === 0) {
      message.warning('至少选择一个盒子');
    }
  };

  const handleOk = () => {
    if (selectedBoxes.length === 0) {
      message.warning('至少选择一个盒子');
      return;
    }

    // 处理选中的盒子,可以在这里进行进一步操作,比如提交数据
    console.log('选中的盒子:', selectedBoxes);

    setModalVisible(false);
  };

  const handleCancel = () => {
    setModalVisible(false);
  };

  const columns = [
    {
      title: '序号',
      dataIndex: 'index',
      key: 'index',
      render: (_: any, record: { index: number }) => record.index + 1,
    },
    {
      title: '名称',
      dataIndex: 'name',
      key: 'name',
    },
    {
      title: '操作',
      key: 'action',
      render: (_: any, record: { index: number; name: string }) => (
        <>
          <Button type="link" onClick={() => handleView(record)}>
            查看
          </Button>
          <Button type="link" onClick={() => handleDelete(record.name)}>
            删除
          </Button>
        </>
      ),
    },
  ];

  const data = selectedBoxes.map((box, index) => ({
    key: index,
    index,
    name: box,
  }));

  const handleView = (record: { index: number; name: string }) => {
    // 处理查看操作,可以根据需要展示盒子的详细信息等
    console.log('查看盒子:', record);
  };

  return (
    <>
      <Button type="primary" onClick={showModal}>
        打开Modal
      </Button>
      <Modal
        title="选择盒子"
        visible={modalVisible}
        onOk={handleOk}
        onCancel={handleCancel}
      >
        <div>
          {boxes.map((box) => (
            <div
              key={box}
              style={{
                border: '1px solid #ccc',
                padding: '10px',
                marginBottom: '10px',
                backgroundColor: selectedBoxes.includes(box) ? '#e6f7ff' : 'white',
                cursor: 'pointer',
              }}
              onClick={() => handleBoxSelect(box)}
            >
              {box}
            </div>
          ))}
        </div>
      </Modal>
      <Table columns={columns} dataSource={data} />
    </>
  );
};

export default BoxSelectionComponent;

  

 

标签:box,index,const,name,selectedBoxes,demo,react,record,antd
From: https://www.cnblogs.com/cyanKoi/p/17902392.html

相关文章

  • React Native package.json 控制App的版本号
    原文:https://blog.csdn.net/gu1920948999/article/details/117984844package.json"version":"1.0.0",android配置android/app/build.gradleimportgroovy.json.JsonSlurper.../***获取版本号*/defgetAppVersion(){definputFile=new......
  • react-native 在ios中使用react-native-vector-icons图标
    github地址:https://github.com/oblador/react-native-vector-icons1.引入npminstall--savereact-native-vector-iconsios项目引入进入node_modules/react-native-vector-icons文件夹,将Fonts文件夹拖拽到Xcode的项目根目录下,然后在Info.plist文件中添加如下内容:<key>U......
  • WPF透明框设置Demo
    <Windowx:Class="GuiDB.EBMultiEditTextWin"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:mc="http......
  • react-native中嵌套的WebView发版后未更新问题
    问题使用了react-native-webview的WebView嵌套h5页面,但是当h5发版之后,重新打开app,h5还是发版前的页面。并且这个缓存严重,每次都要清理缓存或者重装app,页面不能及时更新,影响用户体验。解决rn项目中,在h5链接后边拼接时间戳,代码如下:注意:这里缓存使用的是@react-native-async-sto......
  • React-native ios运行报错 No bundle URL present
    新建项目报错如下:需要手动生成ios下的main.jsbundle文件解决方法:1、生成main.jsbundle文件,在终端项目目录下执行:react-nativebundle--entry-fileindex.js--bundle-output./ios/main.jsbundle--platformios--assets-dest./ios--devfalse或在package.json里面添加以下执......
  • react_hooks系列 useCallback,高阶函数memo
    react_hooks的useCallback,高阶函数memo一、概念和作用1、memo高阶函数:memo解决的是函数式组件的无效渲染问题,当函数式组件重新渲染时,会先判断数据是否发生了变化。相当于类组件的PureComponent(默认提供ShouldComponentUpdate)2、useCallback:1)、useCallback会返回一个函数的memoiz......
  • React Hooks 钩子特性
    人在身处逆境时,适应环境的能力实在惊人。人可以忍受不幸,也可以战胜不幸,因为人有着惊人的潜力,只要立志发挥它,就一定能渡过难关。Hooks是React16.8的新增特性。它可以让你在不编写class组件的情况下使用state以及其他的React特性。ReactHooks表现形式是以use开头......
  • React 逃离闭包陷阱
    众所周知,JavaScript 中的闭包(Closures)一定是这种语言最可怕的特性之一,即使是无所不知的 ChatGPT 也是这样说的。另外它可能也是最隐蔽的语言特性之一,我们在编写 React 代码时经常会用到它,但是大多数时候我们甚至没有意识到这一点。但是,我们终究还是离不开它:如果我们想编写复......
  • 写了一个flinkcdc的简单demo,大概说一下实现过程和建议点
    架构图大致如下:版本信息大致如下,具体版本信息根据自己的需求进行调整即可:oracle:19cflinkcdc:2.4.0kafka:3.1.2flink:1.15.4mysql:8.0.27springboot:2.5.6实现需求:1.使用flinkcdc采集oracle中的数据(历史数据+增量数据:含增删改)同步至kafka的某个topic中2.使用flink消费kafka中......
  • react设置多个className
    react设置多个className 在一个元素上设置样式,有一个固定的样式,然后还有一个使用三元运算符根据条件添加的样式。123456比如说有一个固定样式"title":<divclassName="title">标题</div>,然后还要一个点击高亮的样式:<divclassName={index=== this.sta......