1
- import React , { RefObject , useRef } from 'react'
1
+ import React from 'react'
2
2
import Input from 'components/base/forms/Input'
3
+ import Icon from 'components/Icon'
4
+ import InputGroup from 'components/base/forms/InputGroup'
5
+ import Button from 'components/base/forms/Button'
6
+ import Utils from 'common/utils/utils'
7
+ import ModalHR from './ModalHR'
3
8
4
9
type RuleInputValueProps = {
5
10
'data-test' ?: string
@@ -10,7 +15,72 @@ type RuleInputValueProps = {
10
15
disabled ?: boolean
11
16
readOnly ?: boolean
12
17
isValid ?: boolean
13
- error ?: string
18
+ }
19
+
20
+ const TextAreaModal = ( {
21
+ disabled,
22
+ isValid,
23
+ onChange,
24
+ placeholder,
25
+ readOnly,
26
+ style,
27
+ value,
28
+ } : RuleInputValueProps ) => {
29
+ const [ textAreaValue , setTextAreaValue ] = React . useState ( value )
30
+
31
+ return (
32
+ < div >
33
+ < div className = 'modal-body' >
34
+ < InputGroup
35
+ id = 'rule-value-textarea'
36
+ data-test = 'rule-value-textarea'
37
+ value = { textAreaValue }
38
+ inputProps = { {
39
+ style : style ,
40
+ } }
41
+ isValid = { isValid }
42
+ onChange = { ( e : InputEvent ) => {
43
+ const value = Utils . safeParseEventValue ( e )
44
+ setTextAreaValue ( value . replace ( / \n / g, '' ) )
45
+ } }
46
+ type = 'text'
47
+ className = 'w-100'
48
+ readOnly = { readOnly }
49
+ placeholder = { placeholder }
50
+ disabled = { disabled }
51
+ textarea
52
+ />
53
+ </ div >
54
+ < ModalHR />
55
+ < div className = 'modal-footer' >
56
+ < Button
57
+ className = 'mr-2'
58
+ theme = 'secondary'
59
+ id = 'rule-value-textarea-cancel'
60
+ data-tests = 'rule-value-textarea-cancel'
61
+ onClick = { closeModal2 }
62
+ >
63
+ Cancel
64
+ </ Button >
65
+ < Button
66
+ type = 'button'
67
+ id = 'rule-value-textarea-save'
68
+ data-tests = 'rule-value-textarea-save'
69
+ onClick = { ( ) => {
70
+ const event = new InputEvent ( 'input' , { bubbles : true } )
71
+ Object . defineProperty ( event , 'target' , {
72
+ value : { value : textAreaValue } ,
73
+ writable : false ,
74
+ } )
75
+ onChange ?.( event )
76
+ closeModal2 ( )
77
+ } }
78
+ >
79
+ Apply
80
+ </ Button >
81
+ </ div >
82
+ </ div >
83
+ )
14
84
}
15
85
16
86
const RuleInputValue = ( props : RuleInputValueProps ) => {
@@ -29,8 +99,6 @@ const RuleInputValue = (props: RuleInputValueProps) => {
29
99
isOnlyWhitespace
30
100
const isLongText = String ( value ) . length >= 10
31
101
32
- const ref = useRef ( null )
33
-
34
102
const validate = ( ) => {
35
103
if ( isOnlyWhitespace ) {
36
104
return 'This value is only whitespaces'
@@ -45,31 +113,57 @@ const RuleInputValue = (props: RuleInputValueProps) => {
45
113
return 'This value ends with whitespaces'
46
114
}
47
115
if ( isLongText ) {
48
- return String ( value )
116
+ return 'Click to edit text in a larger area'
49
117
}
50
118
return ''
51
119
}
52
120
121
+ const showIcon = hasWarning || isLongText
53
122
return (
54
- < Tooltip
55
- title = {
56
- < div >
57
- < Input
58
- type = 'text'
59
- { ...props }
60
- ref = { ref }
61
- inputClassName = { hasWarning && 'border-warning' }
62
- />
123
+ < div className = 'relative' >
124
+ < Input
125
+ type = 'text'
126
+ { ...props }
127
+ inputClassName = {
128
+ showIcon ? `pr-5 ${ hasWarning ? 'border-warning' : '' } ` : ''
129
+ }
130
+ />
131
+ { showIcon && (
132
+ < div style = { { position : 'absolute' , right : 5 , top : 10 } } >
133
+ < Tooltip
134
+ title = {
135
+ < div
136
+ className = { `flex bg-white bg-opacity-10 rounded-2 p-1 ${
137
+ hasWarning ? '' : 'cursor-pointer'
138
+ } `}
139
+ onClick = { ( ) => {
140
+ if ( hasWarning ) return
141
+ openModal2 (
142
+ 'Edit Value' ,
143
+ < TextAreaModal
144
+ value = { value }
145
+ onChange = { props . onChange }
146
+ isValid = { props . isValid }
147
+ /> ,
148
+ )
149
+ } }
150
+ >
151
+ < Icon
152
+ name = { hasWarning ? 'warning' : 'unfold' }
153
+ fill = { hasWarning ? undefined : '#fff' }
154
+ width = { 18 }
155
+ height = { 18 }
156
+ />
157
+ </ div >
158
+ }
159
+ place = 'top'
160
+ effect = 'solid'
161
+ >
162
+ { validate ( ) }
163
+ </ Tooltip >
63
164
</ div >
64
- }
65
- place = 'top'
66
- effect = 'solid'
67
- afterShow = { ( ) => {
68
- ; ( ref as RefObject < HTMLElement > ) . current ?. focus ( )
69
- } }
70
- >
71
- { validate ( ) }
72
- </ Tooltip >
165
+ ) }
166
+ </ div >
73
167
)
74
168
}
75
169
0 commit comments