1
1
import _ from "lodash" ;
2
2
import { useEffect , useState } from "react" ;
3
- import { ConfigItem , Radius } from "../pages/setting/theme/styledComponents" ;
3
+ import { ConfigItem , Radius , Margin , Padding } from "../pages/setting/theme/styledComponents" ;
4
4
import { isValidColor , toHex } from "components/colorSelect/colorUtils" ;
5
5
import { ColorSelect } from "components/colorSelect" ;
6
6
import { TacoInput } from "components/tacoInput" ;
7
7
8
+ import { ExpandIcon , CompressIcon } from "lowcoder-design" ;
9
+
8
10
export type configChangeParams = {
9
11
colorKey : string ;
10
12
color ?: string ;
11
13
radius ?: string ;
12
14
chart ?: string ;
15
+ margin ?: string ;
16
+ padding ?: string ;
13
17
} ;
14
18
15
19
type ColorConfigProps = {
@@ -21,6 +25,8 @@ type ColorConfigProps = {
21
25
radius ?: string ;
22
26
configChange : ( params : configChangeParams ) => void ;
23
27
showVarName ?: boolean ;
28
+ margin ?: string ;
29
+ padding ?: string ;
24
30
} ;
25
31
26
32
export default function ColorPicker ( props : ColorConfigProps ) {
@@ -32,10 +38,16 @@ export default function ColorPicker(props: ColorConfigProps) {
32
38
radius : defaultRadius ,
33
39
configChange,
34
40
showVarName = true ,
41
+ margin : defaultMargin ,
42
+ padding : defaultPadding ,
35
43
} = props ;
36
44
const configChangeWithDebounce = _ . debounce ( configChange , 0 ) ;
37
45
const [ color , setColor ] = useState ( defaultColor ) ;
38
46
const [ radius , setRadius ] = useState ( defaultRadius ) ;
47
+
48
+ const [ margin , setMargin ] = useState ( defaultMargin ) ;
49
+ const [ padding , setPadding ] = useState ( defaultPadding ) ;
50
+
39
51
const varName = `(${ colorKey } )` ;
40
52
41
53
const colorInputBlur = ( ) => {
@@ -62,6 +74,35 @@ export default function ColorPicker(props: ColorConfigProps) {
62
74
configChange ( { colorKey, radius : result } ) ;
63
75
} ;
64
76
77
+ const marginInputBlur = ( margin : string ) => {
78
+ let result = "" ;
79
+ if ( ! margin || Number ( margin ) === 0 ) {
80
+ result = "0" ;
81
+ } else if ( / ^ [ 0 - 9 ] + $ / . test ( margin ) ) {
82
+ result = Number ( margin ) + "px" ;
83
+ } else if ( / ^ [ 0 - 9 ] + ( p x | % ) $ / . test ( margin ) ) {
84
+ result = margin ;
85
+ } else {
86
+ result = "3px" ;
87
+ }
88
+ setMargin ( result ) ;
89
+ configChange ( { colorKey, margin : result } ) ;
90
+ } ;
91
+ const paddingInputBlur = ( padding : string ) => {
92
+ let result = "" ;
93
+ if ( ! padding || Number ( padding ) === 0 ) {
94
+ result = "0" ;
95
+ } else if ( / ^ [ 0 - 9 ] + $ / . test ( padding ) ) {
96
+ result = Number ( padding ) + "px" ;
97
+ } else if ( / ^ [ 0 - 9 ] + ( p x | % ) $ / . test ( padding ) ) {
98
+ result = padding ;
99
+ } else {
100
+ result = "3px" ;
101
+ }
102
+ setPadding ( result ) ;
103
+ configChange ( { colorKey, padding : result } ) ;
104
+ } ;
105
+
65
106
useEffect ( ( ) => {
66
107
if ( color && isValidColor ( color ) ) {
67
108
configChangeWithDebounce ( { colorKey, color } ) ;
@@ -77,6 +118,14 @@ export default function ColorPicker(props: ColorConfigProps) {
77
118
setRadius ( defaultRadius ) ;
78
119
} , [ defaultRadius ] ) ;
79
120
121
+ useEffect ( ( ) => {
122
+ setMargin ( defaultMargin ) ;
123
+ } , [ defaultMargin ] ) ;
124
+
125
+ useEffect ( ( ) => {
126
+ setPadding ( defaultPadding ) ;
127
+ } , [ defaultPadding ] ) ;
128
+
80
129
return (
81
130
< ConfigItem className = { props . className } >
82
131
< div className = "text-desc" >
@@ -85,7 +134,9 @@ export default function ColorPicker(props: ColorConfigProps) {
85
134
</ div >
86
135
< div className = "desc" > { desc } </ div >
87
136
</ div >
88
- { colorKey !== "borderRadius" ? (
137
+ { colorKey !== "borderRadius" &&
138
+ colorKey !== "margin" &&
139
+ colorKey !== "padding" && (
89
140
< div className = "config-input" >
90
141
< ColorSelect
91
142
changeColor = { _ . debounce ( setColor , 500 , {
@@ -102,7 +153,8 @@ export default function ColorPicker(props: ColorConfigProps) {
102
153
onKeyUp = { ( e ) => e . nativeEvent . key === "Enter" && colorInputBlur ( ) }
103
154
/>
104
155
</ div >
105
- ) : (
156
+ ) }
157
+ { colorKey === "borderRadius" && (
106
158
< div className = "config-input" >
107
159
< Radius radius = { defaultRadius || "0" } >
108
160
< div >
@@ -117,6 +169,42 @@ export default function ColorPicker(props: ColorConfigProps) {
117
169
/>
118
170
</ div >
119
171
) }
172
+ { colorKey === "margin" && (
173
+ < div className = "config-input" >
174
+ < Margin margin = { defaultMargin || "3px" } >
175
+ < div >
176
+ < ExpandIcon title = "" />
177
+ </ div >
178
+ </ Margin >
179
+ < TacoInput
180
+ value = { margin }
181
+ onChange = { ( e ) => setMargin ( e . target . value ) }
182
+ onBlur = { ( e ) => marginInputBlur ( e . target . value ) }
183
+ onKeyUp = { ( e ) =>
184
+ e . nativeEvent . key === "Enter" &&
185
+ marginInputBlur ( e . currentTarget . value )
186
+ }
187
+ />
188
+ </ div >
189
+ ) }
190
+ { colorKey === "padding" && (
191
+ < div className = "config-input" >
192
+ < Padding padding = { defaultPadding || "3px" } >
193
+ < div >
194
+ < CompressIcon title = "" />
195
+ </ div >
196
+ </ Padding >
197
+ < TacoInput
198
+ value = { padding }
199
+ onChange = { ( e ) => setPadding ( e . target . value ) }
200
+ onBlur = { ( e ) => paddingInputBlur ( e . target . value ) }
201
+ onKeyUp = { ( e ) =>
202
+ e . nativeEvent . key === "Enter" &&
203
+ paddingInputBlur ( e . currentTarget . value )
204
+ }
205
+ />
206
+ </ div >
207
+ ) }
120
208
</ ConfigItem >
121
209
) ;
122
210
}
0 commit comments