@@ -3,6 +3,7 @@ import { usePrefix } from '@designable/react'
33import { camelCase } from '@formily/shared'
44import { Select } from '@formily/antd'
55import { observable } from '@formily/reactive'
6+ import { Field as FieldType } from '@formily/core'
67import { useField , Field , observer } from '@formily/react'
78import { FoldItem } from '../FoldItem'
89import { ColorInput } from '../ColorInput'
@@ -32,23 +33,44 @@ const BorderStyleOptions = [
3233 } ,
3334]
3435
36+ const createBorderProp = ( position : string , key : string ) => {
37+ const insert = position === 'center' ? '' : `-${ position } `
38+ return camelCase ( `border${ insert } -${ key } ` )
39+ }
40+
41+ const parseInitPosition = ( field : FieldType ) => {
42+ const basePath = field . address . parent ( )
43+ for ( let i = 0 ; i < Positions . length ; i ++ ) {
44+ const position = Positions [ i ]
45+ const stylePath = `${ basePath } .${ createBorderProp ( position , 'style' ) } `
46+ const widthPath = `${ basePath } .${ createBorderProp ( position , 'width' ) } `
47+ const colorPath = `${ basePath } .${ createBorderProp ( position , 'color' ) } `
48+ if (
49+ field . query ( stylePath ) . value ( ) ||
50+ field . query ( widthPath ) . value ( ) ||
51+ field . query ( colorPath ) . value ( )
52+ ) {
53+ return position
54+ }
55+ }
56+ return 'center'
57+ }
3558export interface IBorderStyleSetterProps {
3659 className ?: string
3760 style ?: React . CSSProperties
3861}
3962
4063export const BorderStyleSetter : React . FC < IBorderStyleSetterProps > = observer (
4164 ( { className, style } ) => {
65+ const field = useField < FieldType > ( )
4266 const currentPosition = useMemo (
4367 ( ) =>
4468 observable ( {
45- value : 'center' ,
69+ value : parseInitPosition ( field ) ,
4670 } ) ,
47- [ ]
71+ [ field . value ]
4872 )
49- const field = useField ( )
5073 const prefix = usePrefix ( 'border-style-setter' )
51-
5274 const createReaction =
5375 ( position : string ) => ( field : Formily . Core . Models . Field ) => {
5476 field . display =
@@ -80,32 +102,20 @@ export const BorderStyleSetter: React.FC<IBorderStyleSetterProps> = observer(
80102 return (
81103 < Fragment key = { key } >
82104 < Field
83- name = { camelCase (
84- `border${
85- position === 'center' ? '' : `-${ position } `
86- } Style`
87- ) }
105+ name = { createBorderProp ( position , 'style' ) }
88106 basePath = { field . address . parent ( ) }
89107 dataSource = { BorderStyleOptions }
90108 reactions = { createReaction ( position ) }
91109 component = { [ Select , { placeholder : 'Please Select' } ] }
92110 />
93111 < Field
94- name = { camelCase (
95- `border${
96- position === 'center' ? '' : `-${ position } `
97- } Width`
98- ) }
112+ name = { createBorderProp ( position , 'width' ) }
99113 basePath = { field . address . parent ( ) }
100114 reactions = { createReaction ( position ) }
101115 component = { [ SizeInput , { exclude : [ 'auto' ] } ] }
102116 />
103117 < Field
104- name = { camelCase (
105- `border${
106- position === 'center' ? '' : `-${ position } `
107- } Color`
108- ) }
118+ name = { createBorderProp ( position , 'color' ) }
109119 basePath = { field . address . parent ( ) }
110120 reactions = { createReaction ( position ) }
111121 component = { [ ColorInput ] }
0 commit comments