表单自定义组件 - 可选择卡片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;
}
相关推荐
万叶学编程2 小时前
Day02-JavaScript-Vue
前端·javascript·vue.js
天涯学馆4 小时前
Next.js与NextAuth:身份验证实践
前端·javascript·next.js
HEX9CF5 小时前
【CTF Web】Pikachu xss之href输出 Writeup(GET请求+反射型XSS+javascript:伪协议绕过)
开发语言·前端·javascript·安全·网络安全·ecmascript·xss
ConardLi5 小时前
Chrome:新的滚动捕捉事件助你实现更丝滑的动画效果!
前端·javascript·浏览器
ConardLi5 小时前
安全赋值运算符,新的 JavaScript 提案让你告别 trycatch !
前端·javascript
积水成江5 小时前
关于Generator,async 和 await的介绍
前端·javascript·vue.js
Z3r4y5 小时前
【Web】portswigger 服务端原型污染 labs 全解
javascript·web安全·nodejs·原型链污染·wp·portswigger
人生の三重奏5 小时前
前端——js补充
开发语言·前端·javascript
Tandy12356_5 小时前
js逆向——webpack实战案例(一)
前端·javascript·安全·webpack