11import _ from "lodash" ;
22import { useEffect , useState } from "react" ;
3- import { ConfigItem , Radius } from "../pages/setting/theme/styledComponents" ;
3+ import { ConfigItem , Radius , Margin , Padding } from "../pages/setting/theme/styledComponents" ;
44import { isValidColor , toHex } from "components/colorSelect/colorUtils" ;
55import { ColorSelect } from "components/colorSelect" ;
66import { TacoInput } from "components/tacoInput" ;
77
8+ import { ExpandIcon , CompressIcon } from "lowcoder-design" ;
9+
810export type configChangeParams = {
911 colorKey : string ;
1012 color ?: string ;
1113 radius ?: string ;
1214 chart ?: string ;
15+ margin ?: string ;
16+ padding ?: string ;
1317} ;
1418
1519type ColorConfigProps = {
@@ -21,6 +25,8 @@ type ColorConfigProps = {
2125 radius ?: string ;
2226 configChange : ( params : configChangeParams ) => void ;
2327 showVarName ?: boolean ;
28+ margin ?: string ;
29+ padding ?: string ;
2430} ;
2531
2632export default function ColorPicker ( props : ColorConfigProps ) {
@@ -32,10 +38,16 @@ export default function ColorPicker(props: ColorConfigProps) {
3238 radius : defaultRadius ,
3339 configChange,
3440 showVarName = true ,
41+ margin : defaultMargin ,
42+ padding : defaultPadding ,
3543 } = props ;
3644 const configChangeWithDebounce = _ . debounce ( configChange , 0 ) ;
3745 const [ color , setColor ] = useState ( defaultColor ) ;
3846 const [ radius , setRadius ] = useState ( defaultRadius ) ;
47+
48+ const [ margin , setMargin ] = useState ( defaultMargin ) ;
49+ const [ padding , setPadding ] = useState ( defaultPadding ) ;
50+
3951 const varName = `(${ colorKey } )` ;
4052
4153 const colorInputBlur = ( ) => {
@@ -62,6 +74,35 @@ export default function ColorPicker(props: ColorConfigProps) {
6274 configChange ( { colorKey, radius : result } ) ;
6375 } ;
6476
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+
65106 useEffect ( ( ) => {
66107 if ( color && isValidColor ( color ) ) {
67108 configChangeWithDebounce ( { colorKey, color } ) ;
@@ -77,6 +118,14 @@ export default function ColorPicker(props: ColorConfigProps) {
77118 setRadius ( defaultRadius ) ;
78119 } , [ defaultRadius ] ) ;
79120
121+ useEffect ( ( ) => {
122+ setMargin ( defaultMargin ) ;
123+ } , [ defaultMargin ] ) ;
124+
125+ useEffect ( ( ) => {
126+ setPadding ( defaultPadding ) ;
127+ } , [ defaultPadding ] ) ;
128+
80129 return (
81130 < ConfigItem className = { props . className } >
82131 < div className = "text-desc" >
@@ -85,7 +134,9 @@ export default function ColorPicker(props: ColorConfigProps) {
85134 </ div >
86135 < div className = "desc" > { desc } </ div >
87136 </ div >
88- { colorKey !== "borderRadius" ? (
137+ { colorKey !== "borderRadius" &&
138+ colorKey !== "margin" &&
139+ colorKey !== "padding" && (
89140 < div className = "config-input" >
90141 < ColorSelect
91142 changeColor = { _ . debounce ( setColor , 500 , {
@@ -102,7 +153,8 @@ export default function ColorPicker(props: ColorConfigProps) {
102153 onKeyUp = { ( e ) => e . nativeEvent . key === "Enter" && colorInputBlur ( ) }
103154 />
104155 </ div >
105- ) : (
156+ ) }
157+ { colorKey === "borderRadius" && (
106158 < div className = "config-input" >
107159 < Radius radius = { defaultRadius || "0" } >
108160 < div >
@@ -117,6 +169,42 @@ export default function ColorPicker(props: ColorConfigProps) {
117169 />
118170 </ div >
119171 ) }
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+ ) }
120208 </ ConfigItem >
121209 ) ;
122210}
0 commit comments