ColorEditor.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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.value || '');
  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. },
  41. defaultInput: {
  42. marginTop:6,
  43. // borderWidth:1,
  44. width:100
  45. },
  46. capFirst: {
  47. fontSize: 14,
  48. fontFamily: 'roboto-light',
  49. textTransform: 'capitalize'
  50. },
  51. prevCont:{
  52. flex:1,
  53. flexDirection:'row',
  54. justifyContent: 'space-between',
  55. alignItems: 'baseline'
  56. },
  57. preview:{
  58. width: 43,
  59. height: 19,
  60. cursor: 'pointer',
  61. boxShadow: '0px 3px 6px #00000029',
  62. borderColor: '#FFFFFF66',
  63. borderWidth: 1,
  64. borderRadius: 14,
  65. opacity: 1
  66. }
  67. })