ColorEditor.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. let open = this.state.open ? (
  24. <View style={styles.container}>
  25. <View style={styles.prevCont}>
  26. <Text> Color Fill </Text>
  27. <View onClick = {(e) => this.setState({open:false})} style={[{backgroundColor:this.props.text},styles.preview]}></View>
  28. </View>
  29. <SketchPicker
  30. color={color}
  31. width="180px" onChangeComplete ={(newColor) => {
  32. this.props.onChange && this.props.onChange(`rgba(${newColor.rgb.r},${newColor.rgb.g},${newColor.rgb.b},${newColor.rgb.a})`);
  33. this.setState({color:newColor.rgb});
  34. }}
  35. />
  36. </View>
  37. ):(
  38. <View key={Math.random()} style={styles.prevCont}>
  39. <Text>Color Fill</Text>
  40. <View onClick = {(e) => this.setState({open:true})} style={[{backgroundColor:this.props.text},styles.preview]}></View>
  41. </View>
  42. )
  43. return(
  44. <View>
  45. <Text>{this.props.title}</Text>
  46. {open}
  47. </View>
  48. )
  49. }
  50. }
  51. const styles = StyleSheet.create({
  52. container:{
  53. padding:10
  54. },
  55. defaultInput: {
  56. marginTop:6,
  57. borderWidth:1,
  58. width:100
  59. },
  60. prevCont:{
  61. flex:1,
  62. flexDirection:'row'
  63. },
  64. preview:{
  65. width: 43,
  66. height: 19,
  67. boxShadow: '0px 3px 6px #00000029',
  68. border: '1px solid #FFFFFF',
  69. borderRadius: 14,
  70. opacity: 1
  71. }
  72. })