import React, { useState, useEffect } from 'react' import { Styles } from '../shared/types.js' import { formatInputValues, round } from '../utils/formatters.js' import { rgb2cmyk, cmykToRgb, getHexAlpha } from '../utils/converters.js' import { usePicker } from '../context.js' import tc from 'tinycolor2' const Input = ({ label, value, callback, max = 100, hideOpacity, defaultStyles, pickerIdSuffix, }: { max?: number label: string value: number hideOpacity: boolean defaultStyles: Styles pickerIdSuffix: string callback: (arg0: number) => void }) => { const [temp, setTemp] = useState(value) const width = hideOpacity ? '25%' : '20%' useEffect(() => { setTemp(value) }, [value]) const onChange = (e: any) => { const newVal = formatInputValues(parseFloat(e.target.value), 0, max) setTemp(newVal) callback(newVal) } return (
onChange(e)} style={{ ...defaultStyles.rbgcpInput }} id={`rbgcp-${label}-input${pickerIdSuffix}`} // className="rbgcp-input" />
{label}
) } const HexInput = ({ opacity, tinyColor, showHexAlpha, handleChange, defaultStyles, pickerIdSuffix, }: { tinyColor: any opacity: number showHexAlpha: boolean defaultStyles: Styles pickerIdSuffix: string handleChange: (arg0: string) => void }) => { const [disable, setDisable] = useState('') const hex = tinyColor.toHex() const [newHex, setNewHex] = useState(hex) useEffect(() => { if (disable !== 'hex') { setNewHex(hex) } }, [tinyColor, disable, hex]) const hexFocus = () => { setDisable('hex') } const hexBlur = () => { setDisable('') } const handleHex = (e: any) => { const tinyHex = tc(e.target.value) setNewHex(e.target.value) if (tinyHex.isValid()) { const { r, g, b } = tinyHex.toRgb() const newColor = `rgba(${r}, ${g}, ${b}, ${opacity})` handleChange(newColor) } } const displayValue = showHexAlpha ? `${newHex}${getHexAlpha(opacity)}` : newHex const label = showHexAlpha ? 'HEXA' : 'HEX' const width = showHexAlpha ? 88 : 76 return (
handleHex(e)} value={displayValue?.toUpperCase()} id={`rbgcp-hex-input${pickerIdSuffix}`} style={{ ...defaultStyles.rbgcpInput, ...defaultStyles.rbgcpHexInput }} />
{label}
) } const RGBInputs = ({ hc, hideOpacity, handleChange, defaultStyles, pickerIdSuffix, }: { hc: any hideOpacity: boolean defaultStyles: Styles pickerIdSuffix: string handleChange: (arg0: string) => void }) => { const handleRgb = ({ r, g, b }: { r: number; g: number; b: number }) => { handleChange(`rgba(${r}, ${g}, ${b}, ${hc?.a})`) } return ( <> handleRgb({ r: newVal, g: hc?.g, b: hc?.b })} /> handleRgb({ r: hc?.r, g: newVal, b: hc?.b })} /> handleRgb({ r: hc?.r, g: hc?.g, b: newVal })} /> ) } const HSLInputs = ({ hc, setHc, tinyColor, hideOpacity, handleChange, defaultStyles, pickerIdSuffix, }: { hc: any tinyColor: any hideOpacity: boolean defaultStyles: Styles pickerIdSuffix: string setHc: (arg0: any) => void handleChange: (arg0: string) => void }) => { const { s, l } = tinyColor.toHsl() const handleH = (h: number, s: number, l: number) => { const { r, g, b } = tc({ h: h, s: s, l: l }).toRgb() handleChange(`rgba(${r}, ${g}, ${b}, ${hc?.a})`) setHc({ ...hc, h }) } const handleSl = (value: any) => { const { r, g, b } = tc(value).toRgb() handleChange(`rgba(${r}, ${g}, ${b}, ${hc?.a})`) } return ( <> handleH(newVal, s, l)} /> handleSl({ h: hc?.h, s: newVal, l: l })} /> handleSl({ h: hc?.h, s: s, l: newVal })} /> ) } const HSVInputs = ({ hc, setHc, hideOpacity, handleChange, defaultStyles, pickerIdSuffix, }: { hc: any hideOpacity: boolean defaultStyles: Styles pickerIdSuffix: string setHc: (arg0: any) => void handleChange: (arg0: string) => void }) => { const handleH = (h: number, s: number, v: number) => { const { r, g, b } = tc({ h: h, s: s, v: v }).toRgb() handleChange(`rgba(${r}, ${g}, ${b}, ${hc?.a})`) setHc({ ...hc, h }) } const handleSV = (value: any) => { const { r, g, b } = tc(value).toRgb() handleChange(`rgba(${r}, ${g}, ${b}, ${hc?.a})`) } return ( <> handleH(newVal, hc?.s, hc?.v)} /> handleSV({ h: hc?.h, s: newVal, v: hc?.v })} /> handleSV({ h: hc?.h, s: hc?.s, v: newVal })} /> ) } const CMKYInputs = ({ hc, hideOpacity, handleChange, defaultStyles, pickerIdSuffix, }: { hc: any hideOpacity: boolean defaultStyles: Styles pickerIdSuffix: string handleChange: (arg0: string) => void }) => { const { c, m, y, k } = rgb2cmyk(hc?.r, hc?.g, hc?.b) const handleCmyk = (value: any) => { const { r, g, b } = cmykToRgb(value) handleChange(`rgba(${r}, ${g}, ${b}, ${hc?.a})`) } return ( <> handleCmyk({ c: newVal / 100, m: m, y: y, k: k })} /> handleCmyk({ c: c, m: newVal / 100, y: y, k: k })} /> handleCmyk({ c: c, m: m, y: newVal / 100, k: k })} /> handleCmyk({ c: c, m: m, y: y, k: newVal / 100 })} /> ) } const Inputs = () => { const { hc, setHc, inputType, tinyColor, hideOpacity, showHexAlpha, handleChange, defaultStyles, pickerIdSuffix, } = usePicker() return (
{inputType !== 'cmyk' && ( )} {inputType === 'hsl' && ( )} {inputType === 'rgb' && ( )} {inputType === 'hsv' && ( )} {inputType === 'cmyk' && ( )} {!hideOpacity && ( handleChange(`rgba(${hc?.r}, ${hc?.g}, ${hc?.b}, ${newVal / 100})`) } /> )}
) } export default Inputs