React应用react-color

一、 安装与引入

 	安装:
 	
 		npm install react-color --save 
 		
 	引入:
 		import React from 'react'
 		
		import { SketchPicker } from 'react-color'
		
		class Component extends React.Component {
		
  			render() {
  			
    			return <SketchPicker />
    			
  			}
  			
		}	

二、实际开发中(案例)

1、 父组件调用封装的color

<Color 
		defaultColor={backgroundColor} 
		align={'right'} 
		setColor={(hex: string) => { setBackgroundColor(hex) }} 
/>

2. 封装处理数据,引入color供给父组件使用

index.tsx


import React, { useState, useEffect } from 'react'
import { SketchPicker } from 'react-color';
import classnames from 'classnames'
import styles from './index.less'

interface ColorProps{
  size?: 'small' | 'middle' | 'large'
  setColor: Function;
  defaultColor?: string;
  align?: 'left' | 'right'
}
const Color: React.FC<ColorProps> = (props) => {
  const { setColor, defaultColor, size, align='left' } = props;
  const [displayColorPicker, setDisplayColorPicker] = useState(false)
  const [backgroundColor, setBackgroundColor] = useState(defaultColor || '#F3B74B')

  useEffect(()=>{
    setBackgroundColor(defaultColor as string)
  }, [defaultColor])

  return (
    <div className={classnames({
      [styles.colorBox]: true,
      [styles.small]: size === 'small',
    })}>
      <div className={styles.swatch} onClick={() => {
        setDisplayColorPicker(true)
      }} >
        <div className={styles.hex}>{backgroundColor}</div>
        <div className={styles.color} style={{ backgroundColor: backgroundColor }} />
      </div>
      {displayColorPicker ? <div className={classnames({
        [styles.popover]: true,
        [styles.left]: align === 'left',
        [styles.right]: align === 'right',
      })}>
        <div className={styles.cover} onClick={() => setDisplayColorPicker(false)} />
        <SketchPicker color={backgroundColor} onChange={(color) => {
          let hex = color.hex || '';
          let hexUpperCase = hex.toLocaleUpperCase();
          setBackgroundColor(hexUpperCase)
          setColor(hexUpperCase)
        }} />
      </div> : null}
    </div>
  )
}

export default Color;

index.less


.popover{
  position: absolute;
  z-index: 2;
  &.left{
    left: 0;
  }
  &.right{
    right: 0;
  }
}
.colorBox{
  position: relative;
  input{
    line-height: 1;
  }
  span{
    line-height: 1;
  }
  input{
    box-sizing: content-box;
  }
}

.swatch{
  width: 110px;
  padding: 3px 5px;
  background: #fff;
  border-radius: 1px;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.2);
  display: flex;
  justify-content: space-between;
  align-items: center;
  cursor: pointer;
}
.hex{
  font-size: 12px;
}

.color{
  width: 25px;
  height: 25px;
  border-radius: 2px;
  background: rgba(0, 0, 0, 1);
  margin-left: 10px;
}

.cover{
  position: fixed;
  top: 0px;
  right: 0px;
  bottom: 0px;
  left: 0px;
}


.small{
  .swatch{
    width: 100px;
  }
  .color{
    width: 20px;
    height: 20px;
  }
}
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值