表单自定义组件 - 可选择卡片SelectCard

javascript 复制代码
import React from 'react';
import styles from './index.module.less';

type OptionsType = {
  /**
   * 每个item渲染一行,第0项为标题
   */
  labels?: any[];
  /**
   * 自定义渲染内容
   */
  label?: string | React.ReactNode;
  value: any;
};
interface IProps {
  value?: any;
  onChange?: Function;
  options: OptionsType[];
  layout?: 'horizontal' | 'vertical';
  disabled?: boolean;
}

export default function Index(props: IProps) {
  const { value, onChange = () => {}, options, layout = 'horizontal', disabled = false } = props;

  const clickHandler = (row) => {
    if (disabled) return;
    if (value === row.value) {
      onChange(undefined);
    } else {
      onChange(row.value);
    }
  };

  return (
    <div className={`${styles.container} ${styles[layout]}`}>
      {options?.map((item, index) => {
        const activeStyles = item.value === value ? styles['select-card-checked'] : styles['select-card'];
        const disabledStyles = disabled ? styles['select-card-disabled'] : '';

        return (
          <div key={index} className={`${activeStyles} ${disabledStyles}`.trim()} onClick={() => clickHandler(item)}>
            <div className={styles.corner}></div>
            <div className={styles.content}>
              {item?.label
                ? item?.label
                : item?.labels?.map((v, idx) => {
                    return (
                      <React.Fragment key={idx}>
                        {idx === 0 && v ? (
                          <div className={styles.title}>{v}</div>
                        ) : (
                          <div className={styles.text}>{v}</div>
                        )}
                      </React.Fragment>
                    );
                  })}
            </div>
          </div>
        );
      })}
    </div>
  );
}
css 复制代码
@primaric-color: #5050e6;
@primary-color-opacity: #5050e680;

.container {
  .select-card {
    position: relative;
    width: 300px;
    padding: 14px;
    border-radius: 5px;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
    border: 1px solid #f9f9f9;
    background-color: #fff;
    cursor: pointer;
    margin: 0 14px 14px 0;

    .corner {
      position: absolute;
      top: 2px;
      right: 2px;
      width: 0;
      height: 0;
      border-top: 12px solid #5050e6;
      border-left: 12px solid transparent;
      border-radius: 0 3px 0 0;
      opacity: 0;
    }

    .content {
      .title {
        margin: 0;
        font-size: 15px;
        font-weight: bold;
        color: #333;
        margin-bottom: 10px;
      }
      .title:nth-last-of-type(1) {
        margin-bottom: 0;
      }

      .text {
        margin-bottom: 10px;
        font-size: 14px;
        color: #666;
      }
      .text:nth-last-of-type(1) {
        margin-bottom: 0;
      }
    }
  }
  .select-card:hover {
    border-color: @primary-color-opacity;
    box-shadow: 0 0 3px 0 @primaric-color;
  }
  .select-card:nth-last-of-type(1) {
    margin: 0;
  }

  .select-card-checked {
    position: relative;
    width: 300px;
    padding: 14px;
    border-radius: 5px;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
    border: 1px solid @primary-color-opacity;
    background-color: #f0f2ff;
    cursor: pointer;
    margin: 0 14px 14px 0;
    box-shadow: 0 0 3px 0 @primaric-color;

    .corner {
      position: absolute;
      top: 2px;
      right: 2px;
      width: 0;
      height: 0;
      border-top: 12px solid @primaric-color;
      border-left: 12px solid transparent;
      border-radius: 0 3px 0 0;
      opacity: 1;
    }

    .content {
      .title {
        margin: 0;
        font-size: 15px;
        font-weight: bold;
        margin-bottom: 10px;
      }

      .title:nth-last-of-type(1) {
        margin-bottom: 0;
      }

      .text {
        margin-bottom: 10px;
        font-size: 14px;
        color: #666;
      }

      .text:nth-last-of-type(1) {
        margin-bottom: 0;
      }
    }
  }
  
  .select-card-checked:nth-last-of-type(1) {
    margin: 0;
  }

  .select-card-disabled {
    background-color: #e0e0e0;
    border-color: #a0a0a0;
    color: #666;
    box-shadow: none;
    cursor: not-allowed;

    .corner {
      border-top-color: #a0a0a0;
    }
  }
}

.horizontal {
}

.vertical {
  display: flex;
  flex-wrap: wrap;
}
相关推荐
【ql君】qlexcel几秒前
Notepad++ 复制宏、编辑宏的方法
开发语言·javascript·notepad++··宏编辑·宏复制
就改了35 分钟前
Ajax——在OA系统提升性能的局部刷新
前端·javascript·ajax
凌冰_37 分钟前
Ajax 入门
前端·javascript·ajax
奋飛1 小时前
TypeScript系列:第六篇 - 编写高质量的TS类型
javascript·typescript·ts·declare·.d.ts
sunbyte1 小时前
50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | ThemeClock(主题时钟)
前端·javascript·css·vue.js·前端框架·tailwindcss
小飞悟1 小时前
🎯 什么是模块化?CommonJS 和 ES6 Modules 到底有什么区别?小白也能看懂
前端·javascript·设计
浏览器API调用工程师_Taylor1 小时前
AOP魔法:一招实现登录弹窗的全局拦截与动态处理
前端·javascript·vue.js
FogLetter1 小时前
初识图片懒加载:让网页像"懒人"一样聪明加载
前端·javascript
呆呆的心1 小时前
JavaScript 深入理解闭包与柯里化:从原理到实践 🚀
javascript·面试
快起来别睡了1 小时前
看完这篇文章,你就知道什么是proxy
javascript