|
| 1 | +/** |
| 2 | + * @description border component core hook |
| 3 | + * @author 阿怪 |
| 4 | + * @date 2025/1/21 11:32 |
| 5 | + * @version v1.0.0 |
| 6 | + * |
| 7 | + * 江湖的业务千篇一律,复杂的代码好几百行。 |
| 8 | + */ |
| 9 | +import { BorderProps } from './props'; |
| 10 | +import { lineType } from './lineType.ts'; |
| 11 | +import { defineHook } from '../../../runtime/defineHook.ts'; |
| 12 | +import { props } from './api.ts'; |
| 13 | + |
| 14 | +const toBoolean = (value: any) => { |
| 15 | + if (value === undefined) return undefined; |
| 16 | + if (value === 'false') return false; |
| 17 | + return Boolean(value); |
| 18 | +}; |
| 19 | + |
| 20 | +const gettingBorder = (props: BorderProps): Record<lineType, boolean> => { |
| 21 | + const { |
| 22 | + border, |
| 23 | + right, left, top, bottom, |
| 24 | + } = props; |
| 25 | + // 默认right left top bottom都是undefined,但是优先级高于border |
| 26 | + // border可能是布尔值,也可能直接指定top right bottom left |
| 27 | + if (typeof border === 'boolean' || border === undefined) { |
| 28 | + const defaultBorderBoolean = border ?? true; |
| 29 | + return { |
| 30 | + top: toBoolean(top) ?? defaultBorderBoolean, |
| 31 | + left: toBoolean(left) ?? defaultBorderBoolean, |
| 32 | + right: toBoolean(right) ?? defaultBorderBoolean, |
| 33 | + bottom: toBoolean(bottom) ?? defaultBorderBoolean, |
| 34 | + }; |
| 35 | + } else { |
| 36 | + return { |
| 37 | + top: toBoolean(top) ?? border.top ?? true, |
| 38 | + left: toBoolean(left) ?? border.left ?? true, |
| 39 | + right: toBoolean(right) ?? border.right ?? true, |
| 40 | + bottom: toBoolean(bottom) ?? border.bottom ?? true, |
| 41 | + }; |
| 42 | + } |
| 43 | +}; |
| 44 | + |
| 45 | +export const borderOptions = { |
| 46 | + name:'MBorder', |
| 47 | + props, |
| 48 | +} |
| 49 | + |
| 50 | +export const useBorder = defineHook((_props, ctx) => { |
| 51 | + const props = _props as Required<BorderProps>; |
| 52 | + |
| 53 | + |
| 54 | + const renderInit = () => { |
| 55 | + const borderSetting = gettingBorder(props); |
| 56 | + |
| 57 | + const renderTypes = Object.entries(borderSetting).filter(([key, value]) => value).map(([key]) => key); |
| 58 | + |
| 59 | + |
| 60 | + return { |
| 61 | + renderTypes, |
| 62 | + }; |
| 63 | + }; |
| 64 | + |
| 65 | + return { |
| 66 | + props, |
| 67 | + renderInit, |
| 68 | + }; |
| 69 | + |
| 70 | +}); |
| 71 | + |
| 72 | + |
| 73 | +if (import.meta.vitest) { |
| 74 | + const { describe, it, expect } = import.meta.vitest; |
| 75 | + |
| 76 | + describe('gettingBorder', () => { |
| 77 | + |
| 78 | + it('border is undefined', () => { |
| 79 | + const props = {} as Required<BorderProps>; |
| 80 | + expect(gettingBorder(props)).toEqual({ top: true, right: true, bottom: true, left: true }); |
| 81 | + }); |
| 82 | + |
| 83 | + describe('boolean border', () => { |
| 84 | + |
| 85 | + it('border is true', () => { |
| 86 | + const props = { border: true } as Required<BorderProps>; |
| 87 | + expect(gettingBorder(props)).toEqual({ top: true, right: true, bottom: true, left: true }); |
| 88 | + }); |
| 89 | + |
| 90 | + it('border is false', () => { |
| 91 | + const props = { border: false } as Required<BorderProps>; |
| 92 | + expect(gettingBorder(props)).toEqual({ top: false, right: false, bottom: false, left: false }); |
| 93 | + }); |
| 94 | + |
| 95 | + it('border true,left false', () => { |
| 96 | + const props = { border: true, left: false } as Required<BorderProps>; |
| 97 | + expect(gettingBorder(props)).toEqual({ top: true, right: true, bottom: true, left: false }); |
| 98 | + }); |
| 99 | + |
| 100 | + it('border false,left true', () => { |
| 101 | + const props = { border: false, left: true } as Required<BorderProps>; |
| 102 | + expect(gettingBorder(props)).toEqual({ top: false, right: false, bottom: false, left: true }); |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + describe('object border', () => { |
| 107 | + |
| 108 | + it('border is { top: false, right: false, bottom: false, left: true }', () => { |
| 109 | + const props = { border: { top: false, right: false, bottom: false, left: true } } as Required<BorderProps>; |
| 110 | + expect(gettingBorder(props)).toEqual({ top: false, right: false, bottom: false, left: true }); |
| 111 | + }); |
| 112 | + |
| 113 | + it('border is { top: false, right: false, bottom: false, left: true }, left false', () => { |
| 114 | + const props = { border: { top: false, right: false, bottom: false, left: true }, left: false } as Required<BorderProps>; |
| 115 | + expect(gettingBorder(props)).toEqual({ top: false, right: false, bottom: false, left: false }); |
| 116 | + }); |
| 117 | + |
| 118 | + }); |
| 119 | + |
| 120 | + |
| 121 | + }); |
| 122 | + |
| 123 | +} |
0 commit comments