-
Notifications
You must be signed in to change notification settings - Fork 290
Vue DevUI 组件库规范文档
Kagol edited this page Mar 18, 2022
·
4 revisions
- 组件库整体组件的api一致性
- 组件api的数量和命名
- demo的设计和组合
- 指令命名规范
- 组件目录/文件规范
- 整体目录结构
- 组件文件
my-component.tsx
- 组件入口文件
index.ts
- 组件类型文件规范
my-component-types.ts
- Composable规范
useMyComponent.ts
- 样式文件规范
my-component.scss
- 单元测试文件规范
my-component.spec.ts
my-component
├── __tests__ // 组件单元测试
| └── my-component.spec.ts
├── index.ts // 组件入口文件
└── src // 组件源码
└── components // 子组件
├── my-sub-component.ts
└── composables // 组件的逻辑部分 composable
├── my-use-component.ts
├── my-component-types.ts // 组件类型文件
├── my-component.scss // 组件样式
└── my-component.tsx // 组件
import { defineComponent, toRefs } from 'vue';
import type { SetupContext } from 'vue';
import { myComponentProps, MyComponentProps } from './my-component-types';
import useMyComponent from './use-button';
import './my-component.scss';
export default defineComponent({
name: 'DMyComponent',
components: {},
directives: {},
props: myComponentProps,
emits: ['update:modelValue'],
setup(props: MyComponentProps, ctx: SetupContext) {
const { myProp } = toRefs(props);
const { myUseVar, myUseMethod } = useMyComponent(myUseParam);
return () => {
return (
<div class="devui-my-component">{ myProp.value }</div>
);
};
},
});
import type { App } from 'vue';
import MyComponent from './src/my-component';
export * from './src/my-component-types';
export { MyComponent };
export default {
title: 'MyComponent 我的组件',
category: '通用',
status: '100%',
install(app: App): void {
app.component(MyComponent.name, MyComponent);
},
};
import { PropType, ExtractPropTypes } from 'vue';
export const myComponentProps = {
myProp: {
type: Number,
default: 1
},
myProp2: {
type: Array as PropType<number[]>,
default: [5, 10, 20, 50]
},
} as const;
export type MyComponentProps = ExtractPropTypes<typeof myComponentProps>;
import { ref } from 'vue';
export default function useMyComponent(myUseParam) {
const myUseVar = ref(xxx);
const myUseMethod = () => {
};
return { myUseVar, myUseMethod };
}
@import '../../styles-var/devui-var.scss';
.devui-my-component {
}
import { mount, DOMWrapper } from '@vue/test-utils';
import { ref } from 'vue';
import DMyComponent from '../src/my-component';
describe('MyComponent', () => {
it('should render correctly', async () => {
const wrapper = mount({
components: { DMyComponent },
template: `
<d-my-component my-prop="xxx" />
`,
setup() {
const myVar = ref(xxx);
return { myVar };
}
});
expect(xxx).toEqual(xxx);
});
});