| 1 |
import { BaseControl, RangeControl, TextControl } from '@wordpress/components'; |
| 2 |
import { useEffect, useState } from '@wordpress/element'; |
| 3 |
|
| 4 |
/** |
| 5 |
* Component Class |
| 6 |
* |
| 7 |
* @param root0 |
| 8 |
* @param root0.allowCustomMin |
| 9 |
* @param root0.allowCustomMax |
| 10 |
* @param root0.label |
| 11 |
*/ |
| 12 |
export default function CustomRangeControl({ |
| 13 |
allowCustomMin, |
| 14 |
allowCustomMax, |
| 15 |
label, |
| 16 |
...props |
| 17 |
}) { |
| 18 |
const [min, setMin] = useState(parseFloat(props.min) || 0); |
| 19 |
const [max, setMax] = useState(parseFloat(props.max) || 100); |
| 20 |
|
| 21 |
useEffect(() => { |
| 22 |
if (allowCustomMin && props.value && props.value < min) { |
| 23 |
setMin(props.value); |
| 24 |
} |
| 25 |
if (allowCustomMax && props.value && props.value > max) { |
| 26 |
setMax(props.value); |
| 27 |
} |
| 28 |
// eslint-disable-next-line react-hooks/exhaustive-deps |
| 29 |
}, [props.value]); |
| 30 |
|
| 31 |
if (allowCustomMin || allowCustomMax) { |
| 32 |
return ( |
| 33 |
<BaseControl |
| 34 |
id={label} |
| 35 |
label={label} |
| 36 |
className="ghostkit-component-range-control" |
| 37 |
> |
| 38 |
<RangeControl |
| 39 |
{...props} |
| 40 |
min={min} |
| 41 |
max={max} |
| 42 |
withInputField={false} |
| 43 |
/> |
| 44 |
<TextControl |
| 45 |
type="number" |
| 46 |
value={props.value} |
| 47 |
min={min} |
| 48 |
max={max} |
| 49 |
step={props.step} |
| 50 |
onChange={(val) => { |
| 51 |
const result = val === '' ? undefined : parseFloat(val); |
| 52 |
|
| 53 |
if (!allowCustomMin && val < min) { |
| 54 |
props.onChange(min); |
| 55 |
} else if (!allowCustomMax && val > max) { |
| 56 |
props.onChange(max); |
| 57 |
} else { |
| 58 |
props.onChange(result); |
| 59 |
} |
| 60 |
}} |
| 61 |
/> |
| 62 |
</BaseControl> |
| 63 |
); |
| 64 |
} |
| 65 |
|
| 66 |
return <RangeControl {...props} label={label} min={min} max={max} />; |
| 67 |
} |
| 68 |
|