| 1 |
import { |
| 2 |
ButtonGroup, |
| 3 |
Button, |
| 4 |
Tooltip, |
| 5 |
RangeControl, |
| 6 |
__experimentalNumberControl as NumberControl, |
| 7 |
} from '@wordpress/components'; |
| 8 |
import ResponsiveToggle from '../responsive-toggle'; |
| 9 |
import { __, sprintf } from '@wordpress/i18n'; |
| 10 |
import styles from './editor.lazy.scss'; |
| 11 |
import { |
| 12 |
useLayoutEffect, |
| 13 |
useEffect, |
| 14 |
useState, |
| 15 |
useRef, |
| 16 |
} from '@wordpress/element'; |
| 17 |
import { applyFilters } from '@wordpress/hooks'; |
| 18 |
import { select } from '@wordpress/data'; |
| 19 |
import { limitMax, limitMin } from '@Controls/unitWiseMinMaxOption'; |
| 20 |
import { getIdFromString, getPanelIdFromRef } from '@Utils/Helpers'; |
| 21 |
import SRFMReset from '../reset'; |
| 22 |
import SRFMHelpText from '@Components/help-text'; |
| 23 |
|
| 24 |
const isNumberControlSupported = !! NumberControl; |
| 25 |
|
| 26 |
const Range = ( props ) => { |
| 27 |
const [ panelNameForHook, setPanelNameForHook ] = useState( null ); |
| 28 |
const panelRef = useRef( null ); |
| 29 |
|
| 30 |
// Add and remove the CSS on the drop and remove of the component. |
| 31 |
useLayoutEffect( () => { |
| 32 |
styles.use(); |
| 33 |
return () => { |
| 34 |
styles.unuse(); |
| 35 |
}; |
| 36 |
}, [] ); |
| 37 |
|
| 38 |
const { getSelectedBlock } = select( 'core/block-editor' ); |
| 39 |
const blockNameForHook = getSelectedBlock()?.name.split( '/' ).pop(); // eslint-disable-line @wordpress/no-unused-vars-before-return |
| 40 |
useEffect( () => { |
| 41 |
setPanelNameForHook( getPanelIdFromRef( panelRef ) ); |
| 42 |
}, [ blockNameForHook ] ); |
| 43 |
|
| 44 |
const { withInputField, isShiftStepEnabled } = props; |
| 45 |
|
| 46 |
let max = limitMax( props.unit?.value, props ); |
| 47 |
let min = limitMin( props.unit?.value, props ); |
| 48 |
const inputValue = isNaN( props?.value ) ? '' : props?.value; |
| 49 |
|
| 50 |
let unitSizes = [ |
| 51 |
{ |
| 52 |
name: __( 'Pixel', 'sureforms' ), |
| 53 |
unitValue: 'px', |
| 54 |
}, |
| 55 |
{ |
| 56 |
name: __( 'Em', 'sureforms' ), |
| 57 |
unitValue: 'em', |
| 58 |
}, |
| 59 |
]; |
| 60 |
|
| 61 |
if ( props.units ) { |
| 62 |
unitSizes = props.units; |
| 63 |
} |
| 64 |
|
| 65 |
const handleOnChange = ( newValue ) => { |
| 66 |
const parsedValue = parseFloat( newValue ); |
| 67 |
if ( props.setAttributes ) { |
| 68 |
props.setAttributes( { |
| 69 |
[ props.data.label ]: parsedValue, |
| 70 |
} ); |
| 71 |
} |
| 72 |
if ( props?.onChange ) { |
| 73 |
props.onChange( parsedValue ); |
| 74 |
} |
| 75 |
}; |
| 76 |
|
| 77 |
const resetValues = ( defaultValues ) => { |
| 78 |
if ( props?.onChange ) { |
| 79 |
props?.onChange( defaultValues[ props?.data?.label ] ); |
| 80 |
} |
| 81 |
if ( props.displayUnit ) { |
| 82 |
onChangeUnits( defaultValues[ props?.unit?.label ] ); |
| 83 |
} |
| 84 |
}; |
| 85 |
|
| 86 |
const onChangeUnits = ( newValue ) => { |
| 87 |
props.setAttributes( { [ props.unit.label ]: newValue } ); |
| 88 |
|
| 89 |
max = limitMax( newValue, props ); |
| 90 |
min = limitMin( newValue, props ); |
| 91 |
|
| 92 |
if ( props.value > max ) { |
| 93 |
handleOnChange( max ); |
| 94 |
} |
| 95 |
if ( props.value < min ) { |
| 96 |
handleOnChange( min ); |
| 97 |
} |
| 98 |
}; |
| 99 |
|
| 100 |
const onUnitSizeClick = ( uSizes ) => { |
| 101 |
const items = []; |
| 102 |
uSizes.map( ( key ) => |
| 103 |
items.push( |
| 104 |
<Tooltip |
| 105 |
text={ sprintf( |
| 106 |
/* translators: abbreviation for units */ |
| 107 |
__( '%s units', 'sureforms' ), |
| 108 |
key.name |
| 109 |
) } |
| 110 |
key={ key.name } |
| 111 |
> |
| 112 |
<Button |
| 113 |
key={ key.unitValue } |
| 114 |
className={ 'srfm-range-control__units--' + key.name } |
| 115 |
isSmall |
| 116 |
isPrimary={ props.unit.value === key.unitValue } |
| 117 |
isSecondary={ props.unit.value !== key.unitValue } |
| 118 |
aria-pressed={ props.unit.value === key.unitValue } |
| 119 |
aria-label={ sprintf( |
| 120 |
/* translators: abbreviation for units */ |
| 121 |
__( '%s units', 'sureforms' ), |
| 122 |
key.name |
| 123 |
) } |
| 124 |
onClick={ () => onChangeUnits( key.unitValue ) } |
| 125 |
> |
| 126 |
{ key.unitValue } |
| 127 |
</Button> |
| 128 |
</Tooltip> |
| 129 |
) |
| 130 |
); |
| 131 |
|
| 132 |
return items; |
| 133 |
}; |
| 134 |
|
| 135 |
const controlName = getIdFromString( props.label ); |
| 136 |
const controlBeforeDomElement = applyFilters( |
| 137 |
`srfm.${ blockNameForHook }.${ panelNameForHook }.${ controlName }.before`, |
| 138 |
'', |
| 139 |
blockNameForHook |
| 140 |
); |
| 141 |
const controlAfterDomElement = applyFilters( |
| 142 |
`srfm.${ blockNameForHook }.${ panelNameForHook }.${ controlName }`, |
| 143 |
'', |
| 144 |
blockNameForHook |
| 145 |
); |
| 146 |
|
| 147 |
return ( |
| 148 |
<div ref={ panelRef } className="components-base-control"> |
| 149 |
{ controlBeforeDomElement } |
| 150 |
<div className="srfm-range-control srfm-size-type-field-tabs"> |
| 151 |
<div className="srfm-control__header"> |
| 152 |
<ResponsiveToggle |
| 153 |
label={ props.label } |
| 154 |
responsive={ props.responsive } |
| 155 |
/> |
| 156 |
<div className="srfm-range-control__actions srfm-control__actions"> |
| 157 |
{ props?.allowReset && ( |
| 158 |
<SRFMReset |
| 159 |
onReset={ resetValues } |
| 160 |
attributeNames={ [ |
| 161 |
props.data.label, |
| 162 |
props.displayUnit |
| 163 |
? props.unit.label |
| 164 |
: false, |
| 165 |
] } |
| 166 |
setAttributes={ props?.setAttributes } |
| 167 |
isFormSpecific={ props?.isFormSpecific } |
| 168 |
value={ props?.value } |
| 169 |
/> |
| 170 |
) } |
| 171 |
{ props.displayUnit && ( |
| 172 |
<ButtonGroup |
| 173 |
className="srfm-control__units" |
| 174 |
aria-label={ __( 'Select Units', 'sureforms' ) } |
| 175 |
> |
| 176 |
{ onUnitSizeClick( unitSizes ) } |
| 177 |
</ButtonGroup> |
| 178 |
) } |
| 179 |
</div> |
| 180 |
</div> |
| 181 |
<div className="srfm-range-control__mobile-controls"> |
| 182 |
<RangeControl |
| 183 |
value={ inputValue } |
| 184 |
onChange={ handleOnChange } |
| 185 |
withInputField={ false } |
| 186 |
allowReset={ false } |
| 187 |
max={ max } |
| 188 |
min={ min } |
| 189 |
step={ props?.step || 1 } |
| 190 |
initialPosition={ inputValue } |
| 191 |
marks={ props?.marks || false } |
| 192 |
/> |
| 193 |
{ withInputField && isNumberControlSupported && ( |
| 194 |
<NumberControl |
| 195 |
disabled={ props.disabled } |
| 196 |
isShiftStepEnabled={ isShiftStepEnabled } |
| 197 |
max={ max } |
| 198 |
min={ min } |
| 199 |
onChange={ handleOnChange } |
| 200 |
value={ inputValue } |
| 201 |
step={ props?.step || 1 } |
| 202 |
/> |
| 203 |
) } |
| 204 |
</div> |
| 205 |
<SRFMHelpText text={ props.help } /> |
| 206 |
</div> |
| 207 |
{ controlAfterDomElement } |
| 208 |
</div> |
| 209 |
); |
| 210 |
}; |
| 211 |
|
| 212 |
Range.defaultProps = { |
| 213 |
label: __( 'Margin', 'sureforms' ), |
| 214 |
className: '', |
| 215 |
allowReset: true, |
| 216 |
withInputField: true, |
| 217 |
isShiftStepEnabled: true, |
| 218 |
max: Infinity, |
| 219 |
min: -Infinity, |
| 220 |
resetFallbackValue: '', |
| 221 |
placeholder: null, |
| 222 |
unit: [ 'px', 'em' ], |
| 223 |
displayUnit: true, |
| 224 |
responsive: false, |
| 225 |
help: false, |
| 226 |
marks: false, |
| 227 |
isFormSpecific: false, |
| 228 |
}; |
| 229 |
|
| 230 |
export default Range; |
| 231 |
|