|
| 1 | +import React, { Fragment, useMemo } from 'react' |
| 2 | +import { usePrefix } from '@designable/react' |
| 3 | +import { camelCase } from '@formily/shared' |
| 4 | +import { Select } from '@formily/antd' |
| 5 | +import { observable } from '@formily/reactive' |
| 6 | +import { useField, Field, observer } from '@formily/react' |
| 7 | +import { Space } from 'antd' |
| 8 | +import { FoldItem } from '../FoldItem' |
| 9 | +import { ColorInput } from '../ColorInput' |
| 10 | +import { SizeInput } from '../SizeInput' |
| 11 | +import { PositionInput } from '../PositionInput' |
| 12 | +import cls from 'classnames' |
| 13 | +import './styles.less' |
| 14 | + |
| 15 | +const Positions = ['center', 'top', 'right', 'bottom', 'left'] |
| 16 | + |
| 17 | +const BorderStyleOptions = [ |
| 18 | + { |
| 19 | + label: 'None', |
| 20 | + value: 'none', |
| 21 | + }, |
| 22 | + { |
| 23 | + label: <span className="border-style-solid-line" />, |
| 24 | + value: 'solid', |
| 25 | + }, |
| 26 | + { |
| 27 | + label: <span className="border-style-dashed-line" />, |
| 28 | + value: 'dashed', |
| 29 | + }, |
| 30 | + { |
| 31 | + label: <span className="border-style-dotted-line" />, |
| 32 | + value: 'dotted', |
| 33 | + }, |
| 34 | +] |
| 35 | + |
| 36 | +export interface IBorderStyleSetterProps { |
| 37 | + className?: string |
| 38 | + style?: React.CSSProperties |
| 39 | +} |
| 40 | + |
| 41 | +export const BorderStyleSetter: React.FC<IBorderStyleSetterProps> = observer( |
| 42 | + ({ className, style }) => { |
| 43 | + const currentPosition = useMemo( |
| 44 | + () => |
| 45 | + observable({ |
| 46 | + value: 'center', |
| 47 | + }), |
| 48 | + [] |
| 49 | + ) |
| 50 | + const field = useField() |
| 51 | + const prefix = usePrefix('border-style-setter') |
| 52 | + return ( |
| 53 | + <FoldItem label={field.title}> |
| 54 | + <FoldItem.Extra> |
| 55 | + <div className={cls(prefix, className)} style={style}> |
| 56 | + <div className={prefix + '-position'}> |
| 57 | + <PositionInput |
| 58 | + value={currentPosition.value} |
| 59 | + onChange={(value) => { |
| 60 | + currentPosition.value = value |
| 61 | + }} |
| 62 | + /> |
| 63 | + </div> |
| 64 | + <div className={prefix + '-input'}> |
| 65 | + {Positions.map((position, key) => { |
| 66 | + const reaction = (field: Formily.Core.Models.Field) => { |
| 67 | + field.display = |
| 68 | + currentPosition.value === position ? 'visible' : 'hidden' |
| 69 | + if (position !== 'center') { |
| 70 | + const borderStyle = field.query('.borderStyle').value() |
| 71 | + const borderWidth = field.query('.borderWidth').value() |
| 72 | + const borderColor = field.query('.borderColor').value() |
| 73 | + if (borderStyle || borderWidth || borderColor) { |
| 74 | + field.value = undefined |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + return ( |
| 79 | + <Fragment key={key}> |
| 80 | + <Field |
| 81 | + name={camelCase( |
| 82 | + `border${ |
| 83 | + position === 'center' ? '' : `-${position}` |
| 84 | + }Style` |
| 85 | + )} |
| 86 | + basePath={field.address.parent()} |
| 87 | + dataSource={BorderStyleOptions} |
| 88 | + reactions={reaction} |
| 89 | + component={[Select]} |
| 90 | + /> |
| 91 | + <Field |
| 92 | + name={camelCase( |
| 93 | + `border${ |
| 94 | + position === 'center' ? '' : `-${position}` |
| 95 | + }Width` |
| 96 | + )} |
| 97 | + basePath={field.address.parent()} |
| 98 | + reactions={reaction} |
| 99 | + component={[SizeInput]} |
| 100 | + /> |
| 101 | + <Field |
| 102 | + name={camelCase( |
| 103 | + `border${ |
| 104 | + position === 'center' ? '' : `-${position}` |
| 105 | + }Color` |
| 106 | + )} |
| 107 | + basePath={field.address.parent()} |
| 108 | + reactions={reaction} |
| 109 | + component={[ColorInput]} |
| 110 | + /> |
| 111 | + </Fragment> |
| 112 | + ) |
| 113 | + })} |
| 114 | + </div> |
| 115 | + </div> |
| 116 | + </FoldItem.Extra> |
| 117 | + </FoldItem> |
| 118 | + ) |
| 119 | + } |
| 120 | +) |
0 commit comments