-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathindex.js
More file actions
103 lines (90 loc) · 2.95 KB
/
index.js
File metadata and controls
103 lines (90 loc) · 2.95 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
/**
* Internal dependencies
*/
import { useInternalValue } from '~stackable/hooks'
import AdvancedControl, { extractControlProps } from '../base-control2'
import { useControlHandlers } from '../base-control2/hooks'
import { ResetButton } from '../base-control2/reset-button'
import DynamicContentControl, { useDynamicContentControlProps } from '../dynamic-content-control'
/**
* WordPress dependencies
*/
import { TextControl, TextareaControl } from '@wordpress/components'
import { memo } from '@wordpress/element'
/**
* External dependencies
*/
import classnames from 'classnames'
import { isEqual } from 'lodash'
const AdvancedTextControl = memo( props => {
const [ value, onChange ] = useControlHandlers( props.attribute, props.responsive, props.hover, props.valueCallback, props.changeCallback )
const [ propsToPass, controlProps ] = extractControlProps( props )
const {
isDynamic,
isMultiline,
changeDynamicContent: _changeDynamicContent,
allowReset,
isFormatType,
...inputProps
} = propsToPass
const changeDynamicContent = typeof _changeDynamicContent !== 'undefined'
? _changeDynamicContent
: typeof props.onChange === 'undefined' ? onChange : props.onChange
const dynamicContentProps = useDynamicContentControlProps( {
value: typeof props.value === 'undefined' ? value : props.value,
onChange: changeDynamicContent,
isFormatType,
} )
// Track the value internally, because if not the value will be updated on
// every render and will make the cursor jump to the end.
const [ internalValue, setInternalValue ] = useInternalValue( typeof props.value === 'undefined' ? value : props.value )
const _onChange = typeof props.onChange === 'undefined' ? onChange : props.onChange
const internalOnChange = value => {
setInternalValue( value )
_onChange( value )
}
const TextInput = isMultiline ? TextareaControl : TextControl
return (
<AdvancedControl
className={ props.className }
{ ...controlProps }
>
<DynamicContentControl
enable={ isDynamic }
hasPanelModifiedIndicator={ props.hasPanelModifiedIndicator }
{ ...dynamicContentProps }
>
<TextInput
{ ...inputProps }
value={ internalValue }
onChange={ internalOnChange }
className={ classnames( propsToPass.className, 'ugb-advanced-text-control' ) }
/>
</DynamicContentControl>
<ResetButton
allowReset={ allowReset && ! props.isDynamic }
value={ internalValue }
default={ props.default }
onChange={ internalOnChange }
hasPanelModifiedIndicator={ props.hasPanelModifiedIndicator }
/>
</AdvancedControl>
)
}, isEqual )
AdvancedTextControl.defaultProps = {
className: '',
isMultiline: false,
allowReset: true,
default: '',
attribute: '',
responsive: false,
hover: false,
isDynamic: false,
isFormatType: true,
value: undefined,
onChange: undefined,
// Allow custom onChange when dynamic content is changed.
changeDynamicContent: undefined,
hasPanelModifiedIndicator: true,
}
export default AdvancedTextControl