| 1 |
import classnames from 'classnames/dedupe'; |
| 2 |
|
| 3 |
import { BaseControl } from '@wordpress/components'; |
| 4 |
import { useState } from '@wordpress/element'; |
| 5 |
|
| 6 |
import useResponsive from '../../hooks/use-responsive'; |
| 7 |
import ElementStateToggle from '../element-state-toggle'; |
| 8 |
import ImportantToggle from '../important-toggle'; |
| 9 |
import InputDrag from '../input-drag'; |
| 10 |
import ResponsiveToggle from '../responsive-toggle'; |
| 11 |
|
| 12 |
const noop = () => {}; |
| 13 |
|
| 14 |
function InputGroupWithChildren(props) { |
| 15 |
const { className, children, ...restProps } = props; |
| 16 |
|
| 17 |
return ( |
| 18 |
<BaseControl |
| 19 |
className={classnames('ghostkit-component-input-group', className)} |
| 20 |
{...restProps} |
| 21 |
> |
| 22 |
<div className="ghostkit-component-input-group-wrapper"> |
| 23 |
{children} |
| 24 |
</div> |
| 25 |
</BaseControl> |
| 26 |
); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Component |
| 31 |
* |
| 32 |
* @param props |
| 33 |
*/ |
| 34 |
export default function InputGroup(props) { |
| 35 |
if (props.children) { |
| 36 |
return <InputGroupWithChildren {...props} />; |
| 37 |
} |
| 38 |
|
| 39 |
const { |
| 40 |
label, |
| 41 |
inputs, |
| 42 |
hasValue = noop, |
| 43 |
getValue = noop, |
| 44 |
onChange = noop, |
| 45 |
withResponsive, |
| 46 |
withState, |
| 47 |
withImportant, |
| 48 |
expandOnFocus, |
| 49 |
className, |
| 50 |
...restProps |
| 51 |
} = props; |
| 52 |
|
| 53 |
const [isHover, setIsHover] = useState(false); |
| 54 |
const { device } = useResponsive(); |
| 55 |
|
| 56 |
return ( |
| 57 |
<BaseControl |
| 58 |
id={inputs[0].name} |
| 59 |
className={classnames('ghostkit-component-input-group', className)} |
| 60 |
label={ |
| 61 |
<> |
| 62 |
{label} |
| 63 |
{withResponsive && ( |
| 64 |
<ResponsiveToggle |
| 65 |
checkActive={(checkMedia) => |
| 66 |
inputs.some((inputDate) => |
| 67 |
hasValue( |
| 68 |
inputDate.name, |
| 69 |
checkMedia, |
| 70 |
isHover |
| 71 |
) |
| 72 |
) |
| 73 |
} |
| 74 |
/> |
| 75 |
)} |
| 76 |
{withState && ( |
| 77 |
<ElementStateToggle |
| 78 |
isHover={isHover} |
| 79 |
onChange={() => { |
| 80 |
setIsHover(!isHover); |
| 81 |
}} |
| 82 |
checkActive={() => |
| 83 |
inputs.some((inputDate) => |
| 84 |
hasValue(inputDate.name, device, true) |
| 85 |
) |
| 86 |
} |
| 87 |
/> |
| 88 |
)} |
| 89 |
</> |
| 90 |
} |
| 91 |
{...restProps} |
| 92 |
> |
| 93 |
<div className="ghostkit-component-input-group-wrapper"> |
| 94 |
{inputs.map((inputData) => { |
| 95 |
let value = getValue(inputData.name, device, isHover); |
| 96 |
let hasImportant = false; |
| 97 |
|
| 98 |
if (withImportant) { |
| 99 |
hasImportant = / !important$/.test(value); |
| 100 |
|
| 101 |
if (hasImportant) { |
| 102 |
value = value.replace(/ !important$/, ''); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
return ( |
| 107 |
<div |
| 108 |
key={inputData.name} |
| 109 |
className="ghostkit-component-input-group-item" |
| 110 |
> |
| 111 |
<InputDrag |
| 112 |
id={inputData.name} |
| 113 |
help={inputData.label} |
| 114 |
value={value} |
| 115 |
onChange={(newValue) => { |
| 116 |
newValue = newValue |
| 117 |
? `${newValue}${ |
| 118 |
hasImportant |
| 119 |
? ' !important' |
| 120 |
: '' |
| 121 |
}` |
| 122 |
: undefined; |
| 123 |
|
| 124 |
onChange( |
| 125 |
inputData.name, |
| 126 |
newValue, |
| 127 |
device, |
| 128 |
isHover |
| 129 |
); |
| 130 |
}} |
| 131 |
expandOnFocus={expandOnFocus} |
| 132 |
autoComplete="off" |
| 133 |
/> |
| 134 |
{withImportant && typeof value !== 'undefined' && ( |
| 135 |
<ImportantToggle |
| 136 |
onClick={(newWithImportant) => { |
| 137 |
if (value) { |
| 138 |
const newValue = `${value}${ |
| 139 |
newWithImportant |
| 140 |
? ' !important' |
| 141 |
: '' |
| 142 |
}`; |
| 143 |
|
| 144 |
onChange( |
| 145 |
inputData.name, |
| 146 |
newValue, |
| 147 |
device, |
| 148 |
isHover |
| 149 |
); |
| 150 |
} |
| 151 |
}} |
| 152 |
isActive={hasImportant} |
| 153 |
/> |
| 154 |
)} |
| 155 |
</div> |
| 156 |
); |
| 157 |
})} |
| 158 |
</div> |
| 159 |
</BaseControl> |
| 160 |
); |
| 161 |
} |
| 162 |
|