-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathindex.js
More file actions
164 lines (145 loc) · 4.96 KB
/
index.js
File metadata and controls
164 lines (145 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
* Internal dependencies
*/
import RangeControl from './range-control'
import { useControlHandlers } from '../base-control2/hooks'
import AdvancedControl, { extractControlProps } from '../base-control2'
import DynamicContentControl, { useDynamicContentControlProps } from '../dynamic-content-control'
import { ResetButton } from '../base-control2/reset-button'
import {
useAttributeName,
useBlockAttributesContext,
useBlockHoverState,
useDeviceType,
} from '~stackable/hooks'
/**
* External dependencies
*/
import { isEqual } from 'lodash'
/**
* WordPress dependencies
*/
import { memo } from '@wordpress/element'
const AdvancedRangeControl = props => {
const [ value, onChange ] = useControlHandlers( props.attribute, props.responsive, props.hover, props.valueCallback, props.changeCallback )
const [ propsToPass, controlProps ] = extractControlProps( props )
const deviceType = useDeviceType()
const [ currentHoverState ] = useBlockHoverState()
const hasUnits = !! props.units?.length
const unitAttrName = useAttributeName( `${ props.attribute }Unit`, props.responsive, props.hover )
const {
unitAttribute,
_valueDesktop,
_valueTablet,
_unitDesktop,
_unitTablet,
} = useBlockAttributesContext( attributes => {
return {
unitAttribute: attributes[ unitAttrName ],
_valueDesktop: attributes[ `${ props.attribute }` ],
_valueTablet: attributes[ `${ props.attribute }Tablet` ],
_unitDesktop: attributes[ `${ props.attribute }Unit` ],
_unitTablet: attributes[ `${ props.attribute }UnitTablet` ],
}
} )
const unit = typeof props.unit === 'string'
? ( props.unit || props.units?.[ 0 ] || 'px' )
: ( unitAttribute || '' )
// Change the min, max & step values depending on the unit used.
if ( hasUnits ) {
const i = props.units.indexOf( unit ) < 0 ? 0 : props.units.indexOf( unit )
if ( Array.isArray( props.min ) ) {
propsToPass.min = props.min[ i ]
}
if ( Array.isArray( props.max ) ) {
propsToPass.max = props.max[ i ]
}
if ( Array.isArray( props.sliderMin ) ) {
propsToPass.sliderMin = props.sliderMin[ i ]
}
if ( Array.isArray( props.sliderMax ) ) {
propsToPass.sliderMax = props.sliderMax[ i ]
}
if ( Array.isArray( props.step ) ) {
propsToPass.step = props.step[ i ]
}
propsToPass.initialPosition = props.initialPosition !== '' ? props.initialPosition : props.placeholder
// If the unit was not the default, remove the placeholder.
if ( i !== 0 ) {
propsToPass.initialPosition = ''
propsToPass.placeholder = ''
}
}
// Change placeholder based on inherited value
if ( deviceType === 'Mobile' && _valueTablet && _valueTablet !== '' ) {
propsToPass.initialPosition = unitAttribute === _unitTablet ? _valueTablet : ''
propsToPass.placeholder = unitAttribute === _unitTablet ? _valueTablet : ''
} else if ( ( deviceType === 'Mobile' || deviceType === 'Tablet' ) && _valueDesktop && _valueDesktop !== '' ) {
propsToPass.initialPosition = unitAttribute === _unitDesktop ? _valueDesktop : ''
propsToPass.placeholder = unitAttribute === _unitDesktop ? _valueDesktop : ''
}
// Remove the placeholder.
if ( ! props.forcePlaceholder && currentHoverState !== 'normal' ) {
propsToPass.initialPosition = ''
propsToPass.placeholder = ''
}
let placeholderRender = props.placeholderRender
if ( currentHoverState !== 'normal' || ( hasUnits && unit !== props.units[ 0 ] ) ) {
placeholderRender = null
}
// If this supports dynamic content, then the value should be saved as a String.
// Important, the attribute type for this option should be a string.
const _onChange = value => {
const onChangeFunc = typeof props.onChange === 'undefined' ? onChange : props.onChange
let newValue = props.isDynamic ? value.toString() : value
// On reset, allow overriding the value.
if ( newValue === '' ) {
const overrideValue = props.onOverrideReset?.()
if ( typeof overrideValue !== 'undefined' ) {
newValue = overrideValue
}
}
onChangeFunc( newValue )
}
const derivedValue = typeof props.value === 'undefined' ? value : props.value
const dynamicContentProps = useDynamicContentControlProps( {
value: derivedValue,
onChange: _onChange,
} )
return (
<AdvancedControl { ...controlProps }>
<DynamicContentControl
enable={ propsToPass.isDynamic }
controlHasTooltip
{ ...dynamicContentProps }
>
<RangeControl
{ ...propsToPass }
value={ propsToPass.isDynamic ? parseFloat( derivedValue ) : derivedValue }
onChange={ _onChange }
allowReset={ false }
placeholderRender={ placeholderRender }
/>
</DynamicContentControl>
<ResetButton
allowReset={ props.allowReset }
value={ derivedValue }
default={ props.default }
onChange={ _onChange }
/>
</AdvancedControl>
)
}
AdvancedRangeControl.defaultProps = {
allowReset: true,
isDynamic: false,
default: '',
attribute: '',
responsive: false,
hover: false,
value: undefined,
onChange: undefined,
onOverrideReset: undefined,
forcePlaceholder: false,
}
export default memo( AdvancedRangeControl, isEqual )