|
| 1 | +### 结构 |
| 2 | + |
| 3 | +``` |
| 4 | +button |
| 5 | +├── __test__ // button组件测试用例 |
| 6 | +│ └── button.test.tsx |
| 7 | +├── button-group.ts // button-group的配置文件 |
| 8 | +├── button-group.vue // button-group组件 |
| 9 | +├── button.vue // button 组件 |
| 10 | +├── constant.ts // button组件的常量 |
| 11 | +├── index.ts // 注册组件入口 |
| 12 | +└── styles // button组件样式 |
| 13 | + ├── button-group.scss |
| 14 | + └── index.scss |
| 15 | +``` |
| 16 | + |
| 17 | +### 样式变量的使用 |
| 18 | + |
| 19 | +- `button`组件存在不同状态,不同样式的展示形式,在样式存在差异但大部分都有公共的地方 |
| 20 | +- 利用 `CSS `的变量,通过 `var`获取具体的值,用于减少大量重复代码的书写,同时也能保证不同类名下对于不同的样式 |
| 21 | + |
| 22 | +```scss |
| 23 | +--ued-button-text-color: var(--ued-text-color-primary); |
| 24 | +--ued-button-hover-text-color: var(--ued-color-primary); |
| 25 | +--ued-button-hover-bg-color: var(--ued-color-primary-light-9); |
| 26 | +--ued-button-hover-border-color: var(--ued-color-primary-light-8); |
| 27 | +--ued-button-active-text-color: var(--ued-color-primary); |
| 28 | +--ued-button-active-bg-color: var(--ued-color-primary-light-9); |
| 29 | +--ued-button-active-border-color: var(--ued-color-primary); |
| 30 | + |
| 31 | +.ued-button { |
| 32 | + color: var(--ued-button-text-color, var(--ued-text-color-regular)); |
| 33 | + background: var(--ued-button-bg-color, var(--ued-fill-color-blank)); |
| 34 | + border-radius: var(--ued-border-radius-base); |
| 35 | + |
| 36 | + // 基础按钮 |
| 37 | + &--primary, |
| 38 | + &--success, |
| 39 | + &--warning, |
| 40 | + &--danger, |
| 41 | + &--error, |
| 42 | + &--info { |
| 43 | + --ued-button-text-color: var(--ued-color-white); |
| 44 | + --ued-button-hover-text-color: var(--ued-color-white); |
| 45 | + --ued-button-active-text-color: var(--ued-button-hover-text-color); |
| 46 | + } |
| 47 | + |
| 48 | + &--primary { |
| 49 | + --ued-button-bg-color: var(--ued-color-primary); |
| 50 | + --ued-button-border-color: var(--ued-color-primary); |
| 51 | + --ued-button-hover-bg-color: var(--ued-color-primary-light-3); |
| 52 | + --ued-button-hover-border-color: var(--ued-color-primary-light-3); |
| 53 | + --ued-button-active-bg-color: var(--ued-color-primary-dark-2); |
| 54 | + --ued-button-active-border-color: var(--ued-color-primary-dark-2); |
| 55 | + } |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +### provide inject 变成响应式 |
| 60 | + |
| 61 | +- 使用 `button-group`组件时,`button`组件需要接收通过`button-group`传入的组件,完成不同按钮的展示 |
| 62 | +- 使用的方法是 `provide inject`的方式,但是不能直接传,因为直接传入时,到`button`组件接收时就不是响应式变量,此时改动`butong-group`传入的值时,`button`组件不会变化 |
| 63 | + |
| 64 | +- `constant.ts` |
| 65 | + |
| 66 | +```ts |
| 67 | +// 创建一个constant.ts文件保证 provide的key是唯一的,避免重复,并从button-group.ts中拿到传入的类型,完成传入的类型声明限制 |
| 68 | +import type { InjectionKey } from 'vue' |
| 69 | +import { ButtonGroupPropsType } from './button-group' |
| 70 | +export const buttonGroupKey: InjectionKey<ButtonGroupPropsType> = |
| 71 | + Symbol('ButtonGroupKey') |
| 72 | +``` |
| 73 | + |
| 74 | +- `button-group.ts` |
| 75 | + |
| 76 | +```ts |
| 77 | +// 定义传入的字段以及类型,并将相关的类型暴露 |
| 78 | +import { ExtractPropTypes } from 'vue' |
| 79 | + |
| 80 | +export const ButtonGroupProps = { |
| 81 | + type: { |
| 82 | + type: String, |
| 83 | + default: undefined, |
| 84 | + }, |
| 85 | + size: { |
| 86 | + type: String, |
| 87 | + default: undefined, |
| 88 | + }, |
| 89 | +} |
| 90 | + |
| 91 | +export type ButtonGroupPropsType = ExtractPropTypes<typeof ButtonGroupProps> |
| 92 | +``` |
| 93 | +
|
| 94 | +- `button-group.vue` |
| 95 | +
|
| 96 | +```vue |
| 97 | +<script lang="ts" setup> |
| 98 | +import { provide, reactive, toRefs } from 'vue' |
| 99 | +import { buttonGroupKey } from './constant' |
| 100 | +import { ButtonGroupProps } from './button-group' |
| 101 | + |
| 102 | +const buttonGroupProps = defineProps(ButtonGroupProps) |
| 103 | + |
| 104 | +// 关键:为了保证是一个响应式的变量,传入的是一个使用reactive包裹的变量 |
| 105 | +provide( |
| 106 | + buttonGroupKey, |
| 107 | + reactive({ |
| 108 | + ...toRefs(buttonGroupProps), |
| 109 | + }) |
| 110 | +) |
| 111 | +</script> |
| 112 | +``` |
0 commit comments