| 1 |
import { |
| 2 |
ButtonGroup, |
| 3 |
Button, |
| 4 |
Tooltip, |
| 5 |
__experimentalNumberControl as NumberControl, |
| 6 |
} from '@wordpress/components'; |
| 7 |
import ResponsiveToggle from '../responsive-toggle'; |
| 8 |
import { __, sprintf } from '@wordpress/i18n'; |
| 9 |
import styles from './editor.lazy.scss'; |
| 10 |
import React, { useLayoutEffect } from 'react'; |
| 11 |
import { limitMax, limitMin } from '@Controls/unitWiseMinMaxOption'; |
| 12 |
import UAGReset from '../reset'; |
| 13 |
|
| 14 |
const UAGNumberControl = ( props ) => { |
| 15 |
// Add and remove the CSS on the drop and remove of the component. |
| 16 |
useLayoutEffect( () => { |
| 17 |
styles.use(); |
| 18 |
return () => { |
| 19 |
styles.unuse(); |
| 20 |
}; |
| 21 |
}, [] ); |
| 22 |
|
| 23 |
const { isShiftStepEnabled } = props; |
| 24 |
|
| 25 |
let max = limitMax( props.unit?.value, props ); |
| 26 |
let min = limitMin( props.unit?.value, props ); |
| 27 |
const inputValue = isNaN( props?.value ) ? '' : props?.value; |
| 28 |
|
| 29 |
let unitSizes = [ |
| 30 |
{ |
| 31 |
name: __( 'Pixel', 'ultimate-addons-for-gutenberg' ), |
| 32 |
unitValue: 'px', |
| 33 |
}, |
| 34 |
{ |
| 35 |
name: __( 'Em', 'ultimate-addons-for-gutenberg' ), |
| 36 |
unitValue: 'em', |
| 37 |
}, |
| 38 |
]; |
| 39 |
|
| 40 |
if ( props.units ) { |
| 41 |
unitSizes = props.units; |
| 42 |
} |
| 43 |
|
| 44 |
const handleOnChange = ( newValue ) => { |
| 45 |
const parsedValue = parseFloat( newValue ); |
| 46 |
if ( props.setAttributes ) { |
| 47 |
props.setAttributes( { |
| 48 |
[ props.data.label ]: parsedValue, |
| 49 |
} ) |
| 50 |
} |
| 51 |
if ( props?.onChange ) { |
| 52 |
props.onChange( parsedValue ); |
| 53 |
} |
| 54 |
}; |
| 55 |
|
| 56 |
const resetValues = ( defaultValues ) => { |
| 57 |
|
| 58 |
if ( props?.onChange ) { |
| 59 |
props?.onChange( defaultValues[props?.data?.label] ) |
| 60 |
} |
| 61 |
if ( props.displayUnit ) { |
| 62 |
onChangeUnits( defaultValues[props?.unit?.label] ) |
| 63 |
} |
| 64 |
}; |
| 65 |
|
| 66 |
const onChangeUnits = ( newValue ) => { |
| 67 |
|
| 68 |
props.setAttributes( { [ props.unit.label ]: newValue } ); |
| 69 |
|
| 70 |
max = limitMax( newValue, props ); |
| 71 |
min = limitMin( newValue, props ); |
| 72 |
|
| 73 |
if ( props.value > max ) { |
| 74 |
handleOnChange( max ); |
| 75 |
} |
| 76 |
if ( props.value < min ) { |
| 77 |
handleOnChange( min ); |
| 78 |
} |
| 79 |
|
| 80 |
}; |
| 81 |
|
| 82 |
const onUnitSizeClick = ( uSizes ) => { |
| 83 |
const items = []; |
| 84 |
uSizes.map( ( key ) => |
| 85 |
items.push( |
| 86 |
<Tooltip |
| 87 |
text={ sprintf( |
| 88 |
/* translators: abbreviation for units */ |
| 89 |
__( '%s units', 'ultimate-addons-for-gutenberg' ), |
| 90 |
key.name |
| 91 |
) } |
| 92 |
key={key.name} |
| 93 |
> |
| 94 |
<Button |
| 95 |
key={ key.unitValue } |
| 96 |
className={ 'uagb-number-control__units--' + key.name } |
| 97 |
isSmall |
| 98 |
isPrimary={ props.unit.value === key.unitValue } |
| 99 |
isSecondary={ props.unit.value !== key.unitValue } |
| 100 |
aria-pressed={ props.unit.value === key.unitValue } |
| 101 |
aria-label={ sprintf( |
| 102 |
/* translators: abbreviation for units */ |
| 103 |
__( '%s units', 'ultimate-addons-for-gutenberg' ), |
| 104 |
key.name |
| 105 |
) } |
| 106 |
onClick={ () => onChangeUnits( key.unitValue ) } |
| 107 |
> |
| 108 |
{ key.unitValue } |
| 109 |
</Button> |
| 110 |
</Tooltip> |
| 111 |
) |
| 112 |
); |
| 113 |
|
| 114 |
return items; |
| 115 |
}; |
| 116 |
|
| 117 |
return ( |
| 118 |
<div className="components-base-control uag-number-control uagb-size-type-field-tabs"> |
| 119 |
<div className="uagb-control__header"> |
| 120 |
<div className="uagb-number-control__actions uagb-control__actions"> |
| 121 |
<UAGReset |
| 122 |
onReset={resetValues} |
| 123 |
attributeNames = {[ |
| 124 |
props.data.label, |
| 125 |
props.displayUnit ? props.unit.label : false |
| 126 |
]} |
| 127 |
setAttributes={ props.setAttributes } |
| 128 |
/> |
| 129 |
{ props.displayUnit && ( |
| 130 |
<ButtonGroup |
| 131 |
className="uagb-control__units" |
| 132 |
aria-label={ __( |
| 133 |
'Select Units', |
| 134 |
'ultimate-addons-for-gutenberg' |
| 135 |
) } |
| 136 |
> |
| 137 |
{ onUnitSizeClick( unitSizes ) } |
| 138 |
</ButtonGroup> |
| 139 |
) } |
| 140 |
</div> |
| 141 |
</div> |
| 142 |
<div className="uagb-number-control__mobile-controls"> |
| 143 |
<ResponsiveToggle |
| 144 |
label= { props.label } |
| 145 |
responsive= { props.responsive } |
| 146 |
/> |
| 147 |
<NumberControl |
| 148 |
labelPosition="edge" |
| 149 |
disabled={ props.disabled } |
| 150 |
isShiftStepEnabled={ isShiftStepEnabled } |
| 151 |
max={ max } |
| 152 |
min={ min } |
| 153 |
onChange={ handleOnChange } |
| 154 |
value={ inputValue } |
| 155 |
step={ props?.step || 1 } |
| 156 |
/> |
| 157 |
</div> |
| 158 |
{ props.help && ( |
| 159 |
<p className="uag-control-help-notice">{ props.help }</p> |
| 160 |
) } |
| 161 |
</div> |
| 162 |
); |
| 163 |
}; |
| 164 |
|
| 165 |
UAGNumberControl.defaultProps = { |
| 166 |
label: __( 'Margin', 'ultimate-addons-for-gutenberg' ), |
| 167 |
className: '', |
| 168 |
allowReset: true, |
| 169 |
isShiftStepEnabled: true, |
| 170 |
max: Infinity, |
| 171 |
min: -Infinity, |
| 172 |
resetFallbackValue: '', |
| 173 |
placeholder: null, |
| 174 |
unit: [ 'px', 'em' ], |
| 175 |
displayUnit: true, |
| 176 |
responsive: false, |
| 177 |
}; |
| 178 |
|
| 179 |
export default UAGNumberControl; |
| 180 |
|