ColorEditor.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import React, { useState } from 'react';
  2. import {View , Text, TextInput, Button,StyleSheet} from 'react-native';
  3. import { Icon } from 'react-native-elements'
  4. import SketchPicker from 'react-color';
  5. let rgbaTemplate = /rgba\([ ]*([0-9.]+)[ ]*,[ ]*([0-9.]+)[ ]*,[ ]*([0-9.]+)[ ]*,[ ]*([0-9.]+)[ ]*\)/;
  6. export default class ColorEditor extends React.Component {
  7. constructor(props){
  8. super(props)
  9. this.state = {
  10. open:false
  11. }
  12. }
  13. render(){
  14. let matches = rgbaTemplate.exec(this.props.text || '');
  15. let defaultColor = {
  16. r: matches && parseFloat(matches[1]) || 0,
  17. g: matches && parseFloat(matches)[2] || 0,
  18. b: matches && parseFloat(matches)[3] || 0,
  19. a: matches && parseFloat(matches)[4] || 1
  20. };
  21. let color = this.state.color || defaultColor;
  22. let title = this.props.title || "Input";
  23. return <View style={styles.container}>
  24. <View style={styles.prevCont}>
  25. <Text style={styles.capFirst}>{title}</Text>
  26. <View onClick = {(e) => this.setState({open:!this.state.open})} style={[{backgroundColor:this.props.text},styles.preview]}></View>
  27. </View>
  28. {this.state.open ? <SketchPicker
  29. color={color}
  30. width="180px" onChangeComplete ={(newColor) => {
  31. this.props.onChange && this.props.onChange(`rgba(${newColor.rgb.r},${newColor.rgb.g},${newColor.rgb.b},${newColor.rgb.a})`);
  32. this.setState({color:newColor.rgb});
  33. }}
  34. /> : null}
  35. </View>
  36. }
  37. }
  38. const styles = StyleSheet.create({
  39. container:{
  40. padding:24,
  41. paddingTop: 0
  42. },
  43. defaultInput: {
  44. marginTop:6,
  45. // borderWidth:1,
  46. width:100
  47. },
  48. capFirst: {
  49. fontSize: 14,
  50. fontFamily: 'roboto-light',
  51. textTransform: 'capitalize'
  52. },
  53. prevCont:{
  54. flex:1,
  55. flexDirection:'row',
  56. justifyContent: 'space-between',
  57. alignItems: 'baseline'
  58. },
  59. preview:{
  60. width: 43,
  61. height: 19,
  62. cursor: 'pointer',
  63. boxShadow: '0px 3px 6px #00000029',
  64. borderColor: '#FFFFFF66',
  65. borderWidth: 1,
  66. borderRadius: 14,
  67. opacity: 1
  68. }
  69. })