jetformbuilder
/
modules
/
framework
/
blocks-style-manager
/
assets
/
src
/
components
/
controls
/
range.jsx
jetformbuilder
/
modules
/
framework
/
blocks-style-manager
/
assets
/
src
/
components
/
controls
Last commit date
border.jsx
3 months ago
choose.jsx
3 months ago
color.jsx
3 months ago
dimensions.jsx
3 months ago
heading.jsx
3 months ago
range.jsx
3 months ago
text.jsx
3 months ago
toggle.jsx
3 months ago
typography.jsx
3 months ago
range.jsx
214 lines
| 1 | import { |
| 2 | BaseControl, |
| 3 | RangeControl, |
| 4 | __experimentalHStack as HStack, |
| 5 | __experimentalUnitControl as UnitControl |
| 6 | } from '@wordpress/components'; |
| 7 | import { useState } from '@wordpress/element'; |
| 8 | |
| 9 | /** |
| 10 | * Allowed units format: |
| 11 | * [ |
| 12 | * { value: 'px', intervals: { step: 1, min: 1, max: 1000 } }, |
| 13 | * { value: '%', intervals: { step: 1, min: 1, max: 100 } }, |
| 14 | * { value: 'vw', intervals: { step: 1, min: 1, max: 100 } }, |
| 15 | * ] |
| 16 | */ |
| 17 | const ControlRange = ( { control, value, handleChange } ) => { |
| 18 | |
| 19 | const withoutUnits = ( value ) => { |
| 20 | // Remove units from the value |
| 21 | return value ? parseFloat( value ) : 0; |
| 22 | } |
| 23 | |
| 24 | const sanitizeValue = ( val ) => { |
| 25 | |
| 26 | // Sanitize the value based on min and max |
| 27 | let sanitizedValue = val; |
| 28 | |
| 29 | const min = control.min || 0; |
| 30 | const max = control.max || 100; |
| 31 | |
| 32 | if ( typeof sanitizedValue === 'number' ) { |
| 33 | if ( sanitizedValue < min ) { |
| 34 | sanitizedValue = min; |
| 35 | } else if ( sanitizedValue > max ) { |
| 36 | sanitizedValue = max; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | if ( typeof sanitizedValue === 'object' && sanitizedValue !== null ) { |
| 41 | |
| 42 | let result = ''; |
| 43 | |
| 44 | for ( const key in sanitizedValue ) { |
| 45 | if ( sanitizedValue.hasOwnProperty( key ) ) { |
| 46 | result += sanitizeValue( sanitizedValue[ key ] ); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | sanitizedValue = result; |
| 51 | } |
| 52 | |
| 53 | if ( true === control.cut_units ) { |
| 54 | // Remove units from the value |
| 55 | sanitizedValue = withoutUnits( sanitizedValue ); |
| 56 | } |
| 57 | |
| 58 | return sanitizedValue; |
| 59 | } |
| 60 | |
| 61 | const [ currentValue, setCurrentValue ] = useState( sanitizeValue( value ) ); |
| 62 | |
| 63 | const getDefaultUnit = () => { |
| 64 | // Return the first unit from the allowed units or 'px' if no units are specified |
| 65 | return control.units && control.units.length > 0 ? control.units[ 0 ].value : ''; |
| 66 | } |
| 67 | |
| 68 | const getCurrentUnit = () => { |
| 69 | |
| 70 | if ( true === control.cut_units ) { |
| 71 | return ''; |
| 72 | } |
| 73 | |
| 74 | if ( currentValue ) { |
| 75 | |
| 76 | // adjust current value to string in case it's number |
| 77 | let currentValueStr = typeof currentValue === 'number' ? currentValue.toString() : currentValue; |
| 78 | |
| 79 | const match = currentValueStr.match( /[a-zA-Z%]+/ ); |
| 80 | |
| 81 | if ( match && match.length > 0 ) { |
| 82 | return match[ 0 ]; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | return getDefaultUnit(); |
| 87 | } |
| 88 | |
| 89 | const addUnits = ( value ) => { |
| 90 | |
| 91 | // Get current unit from current value |
| 92 | const currentUnit = getCurrentUnit(); |
| 93 | const result = currentUnit ? `${ value }${ currentUnit }` : value; |
| 94 | |
| 95 | return result; |
| 96 | } |
| 97 | |
| 98 | const allowedUnits = () => { |
| 99 | |
| 100 | // Return allowed units list in the format required by UnitControl |
| 101 | const units = []; |
| 102 | |
| 103 | if ( control.units && control.units.length > 0 ) { |
| 104 | for ( const unit of control.units ) { |
| 105 | units.push( { |
| 106 | value: unit.value, |
| 107 | label: unit.label || unit.value, |
| 108 | default: unit.default || 0, |
| 109 | } ); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | return units; |
| 114 | } |
| 115 | |
| 116 | const getCurrentUnitMin = () => { |
| 117 | // Get the minimum value for the current unit |
| 118 | const currentUnit = getCurrentUnit(); |
| 119 | const unit = control.units.find( ( u ) => u.value === currentUnit ); |
| 120 | |
| 121 | return unit && unit.intervals ? unit.intervals.min : 0; |
| 122 | } |
| 123 | |
| 124 | |
| 125 | const getCurrentUnitMax = () => { |
| 126 | // Get the maximum value for the current unit |
| 127 | const currentUnit = getCurrentUnit(); |
| 128 | const unit = control.units.find( ( u ) => u.value === currentUnit ); |
| 129 | |
| 130 | return unit && unit.intervals ? unit.intervals.max : 100; |
| 131 | } |
| 132 | |
| 133 | // Return simple RangeControl if no units are specified |
| 134 | if ( ! control.units ) { |
| 135 | |
| 136 | const min = control.min || 0; |
| 137 | const max = control.max || 100; |
| 138 | |
| 139 | return ( |
| 140 | <RangeControl |
| 141 | label={ control.label } |
| 142 | value={ currentValue } |
| 143 | onChange={ ( newValue ) => { |
| 144 | setCurrentValue( newValue ); |
| 145 | handleChange( newValue ); |
| 146 | } } |
| 147 | placeholder={ control.placeholder } |
| 148 | className="crocoblock-style-manager__text-control" |
| 149 | help={ control.help } |
| 150 | min={ min } |
| 151 | max={ max } |
| 152 | disabled={ control.disabled } |
| 153 | __nextHasNoMarginBottom |
| 154 | __next40pxDefaultSize |
| 155 | /> |
| 156 | ); |
| 157 | } |
| 158 | |
| 159 | // Return RangeControl with units if specified |
| 160 | return ( |
| 161 | <BaseControl |
| 162 | label={ control.label } |
| 163 | help={ control.help } |
| 164 | className="crocoblock-style-manager__range-control" |
| 165 | __nextHasNoMarginBottom |
| 166 | __next40pxDefaultSize |
| 167 | > |
| 168 | <HStack |
| 169 | className="crocoblock-style-manager__range-control__hstack" |
| 170 | alignment="center" |
| 171 | > |
| 172 | <div |
| 173 | style={ { |
| 174 | flex: '1 1 0%', |
| 175 | } } |
| 176 | > |
| 177 | <RangeControl |
| 178 | value={ withoutUnits( currentValue ) } |
| 179 | onChange={ ( newValue ) => { |
| 180 | setCurrentValue( addUnits( newValue ) ); |
| 181 | handleChange( addUnits( newValue ) ); |
| 182 | } } |
| 183 | placeholder={ control.placeholder } |
| 184 | className="crocoblock-style-manager__text-control" |
| 185 | withInputField={ false } |
| 186 | min={ getCurrentUnitMin() } |
| 187 | max={ getCurrentUnitMax() } |
| 188 | __nextHasNoMarginBottom |
| 189 | __next40pxDefaultSize |
| 190 | /> |
| 191 | </div> |
| 192 | <div |
| 193 | style={ { |
| 194 | flex: '0 0 80px', |
| 195 | } } |
| 196 | > |
| 197 | <UnitControl |
| 198 | value={ currentValue } |
| 199 | onChange={ ( newValue ) => { |
| 200 | handleChange( newValue ); |
| 201 | setCurrentValue( newValue ); |
| 202 | } } |
| 203 | units={ allowedUnits() } |
| 204 | className="crocoblock-style-manager__unit-control" |
| 205 | __nextHasNoMarginBottom |
| 206 | __next40pxDefaultSize |
| 207 | /> |
| 208 | </div> |
| 209 | </HStack> |
| 210 | </BaseControl> |
| 211 | ); |
| 212 | } |
| 213 | |
| 214 | export default ControlRange; |