react-color
Version:
A Collection of Color Pickers from Sketch, Photoshop, Chrome & more
204 lines (197 loc) • 5.59 kB
JavaScript
import React from 'react';
import reactCSS from 'reactcss';
import * as color from '../../helpers/color';
import { EditableInput } from '../common';
export var GoogleFields = function GoogleFields(_ref) {
var onChange = _ref.onChange,
rgb = _ref.rgb,
hsl = _ref.hsl,
hex = _ref.hex,
hsv = _ref.hsv;
var handleChange = function handleChange(data, e) {
if (data.hex) {
color.isValidHex(data.hex) && onChange({
hex: data.hex,
source: 'hex'
}, e);
} else if (data.rgb) {
var values = data.rgb.split(',');
color.isvalidColorString(data.rgb, 'rgb') && onChange({
r: values[0],
g: values[1],
b: values[2],
a: 1,
source: 'rgb'
}, e);
} else if (data.hsv) {
var _values = data.hsv.split(',');
if (color.isvalidColorString(data.hsv, 'hsv')) {
_values[2] = _values[2].replace('%', '');
_values[1] = _values[1].replace('%', '');
_values[0] = _values[0].replace('°', '');
if (_values[1] == 1) {
_values[1] = 0.01;
} else if (_values[2] == 1) {
_values[2] = 0.01;
}
onChange({
h: Number(_values[0]),
s: Number(_values[1]),
v: Number(_values[2]),
source: 'hsv'
}, e);
}
} else if (data.hsl) {
var _values2 = data.hsl.split(',');
if (color.isvalidColorString(data.hsl, 'hsl')) {
_values2[2] = _values2[2].replace('%', '');
_values2[1] = _values2[1].replace('%', '');
_values2[0] = _values2[0].replace('°', '');
if (hsvValue[1] == 1) {
hsvValue[1] = 0.01;
} else if (hsvValue[2] == 1) {
hsvValue[2] = 0.01;
}
onChange({
h: Number(_values2[0]),
s: Number(_values2[1]),
v: Number(_values2[2]),
source: 'hsl'
}, e);
}
}
};
var styles = reactCSS({
'default': {
wrap: {
display: 'flex',
height: '100px',
marginTop: '4px'
},
fields: {
width: '100%'
},
column: {
paddingTop: '10px',
display: 'flex',
justifyContent: 'space-between'
},
double: {
padding: '0px 4.4px',
boxSizing: 'border-box'
},
input: {
width: '100%',
height: '38px',
boxSizing: 'border-box',
padding: '4px 10% 3px',
textAlign: 'center',
border: '1px solid #dadce0',
fontSize: '11px',
textTransform: 'lowercase',
borderRadius: '5px',
outline: 'none',
fontFamily: 'Roboto,Arial,sans-serif'
},
input2: {
height: '38px',
width: '100%',
border: '1px solid #dadce0',
boxSizing: 'border-box',
fontSize: '11px',
textTransform: 'lowercase',
borderRadius: '5px',
outline: 'none',
paddingLeft: '10px',
fontFamily: 'Roboto,Arial,sans-serif'
},
label: {
textAlign: 'center',
fontSize: '12px',
background: '#fff',
position: 'absolute',
textTransform: 'uppercase',
color: '#3c4043',
width: '35px',
top: '-6px',
left: '0',
right: '0',
marginLeft: 'auto',
marginRight: 'auto',
fontFamily: 'Roboto,Arial,sans-serif'
},
label2: {
left: '10px',
textAlign: 'center',
fontSize: '12px',
background: '#fff',
position: 'absolute',
textTransform: 'uppercase',
color: '#3c4043',
width: '32px',
top: '-6px',
fontFamily: 'Roboto,Arial,sans-serif'
},
single: {
flexGrow: '1',
margin: '0px 4.4px'
}
}
});
var rgbValue = rgb.r + ', ' + rgb.g + ', ' + rgb.b;
var hslValue = Math.round(hsl.h) + '\xB0, ' + Math.round(hsl.s * 100) + '%, ' + Math.round(hsl.l * 100) + '%';
var hsvValue = Math.round(hsv.h) + '\xB0, ' + Math.round(hsv.s * 100) + '%, ' + Math.round(hsv.v * 100) + '%';
return React.createElement(
'div',
{ style: styles.wrap, className: 'flexbox-fix' },
React.createElement(
'div',
{ style: styles.fields },
React.createElement(
'div',
{ style: styles.double },
React.createElement(EditableInput, {
style: { input: styles.input, label: styles.label },
label: 'hex',
value: hex,
onChange: handleChange
})
),
React.createElement(
'div',
{ style: styles.column },
React.createElement(
'div',
{ style: styles.single },
React.createElement(EditableInput, {
style: { input: styles.input2, label: styles.label2 },
label: 'rgb',
value: rgbValue,
onChange: handleChange
})
),
React.createElement(
'div',
{ style: styles.single },
React.createElement(EditableInput, {
style: { input: styles.input2, label: styles.label2 },
label: 'hsv',
value: hsvValue,
onChange: handleChange
})
),
React.createElement(
'div',
{ style: styles.single },
React.createElement(EditableInput, {
style: { input: styles.input2, label: styles.label2 },
label: 'hsl',
value: hslValue,
onChange: handleChange
})
)
)
)
);
};
export default GoogleFields;