Skip to content

Commit 4ac2aa3

Browse files
committed
🚀 [avatar] avatar use core hook
1 parent 1bc672d commit 4ac2aa3

File tree

13 files changed

+139
-126
lines changed

13 files changed

+139
-126
lines changed

core/components/base/avatar/api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* 江湖的业务千篇一律,复杂的代码好几百行。
88
*/
99
import { MCOPO, MPropType } from '../../types/props';
10-
import { AvatarProps } from './index';
10+
import { AvatarProps } from './props';
1111

1212
export const props: MCOPO<AvatarProps> = {
1313
variant: { type: String as MPropType<'circle' | 'square'>, default: 'circle' },

core/components/base/avatar/avatar.css

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,6 @@
2323
width: var(--m-avatar-w);
2424
height: var(--m-avatar-size);
2525

26-
& .m-avatar-mask {
27-
grid-area: avatar;
28-
width: var(--m-avatar-w);
29-
height: var(--m-avatar-size);
30-
}
31-
3226
--m-image-m-t: 0;
3327
--m-image-m-l: 0;
3428

core/components/base/avatar/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
* 江湖的业务千篇一律,复杂的代码好几百行。
88
*/
99
import { props } from './api.ts';
10+
import useAvatar from './useAvatar.tsx';
1011

1112

1213
export const AvatarCore = {
13-
props
14+
props,
15+
useAvatar
1416
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @description avatar component core hook
3+
* @author 阿怪
4+
* @date 2025/1/14 09:35
5+
* @version v1.0.0
6+
*
7+
* 江湖的业务千篇一律,复杂的代码好几百行。
8+
*/
9+
import { AvatarProps } from './props';
10+
11+
export default function useAvatar(_props: AvatarProps) {
12+
const props = _props as Required<AvatarProps>;
13+
14+
const renderInit = () => {
15+
const avatarClass = ['m-avatar', `m-avatar-${_props.variant}`, `m-avatar-${_props.size}`];
16+
const img = <img src={props.img} alt=""/>;
17+
18+
return {
19+
avatarClass,
20+
img,
21+
};
22+
};
23+
24+
return {
25+
renderInit,
26+
props,
27+
};
28+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* @description core avatar test
3+
* @author 阿怪
4+
* @date 2025/1/14 09:52
5+
* @version v1.0.0
6+
*
7+
* 江湖的业务千篇一律,复杂的代码好几百行。
8+
*/
9+
10+
11+
import { describe, expect, it } from 'vitest';
12+
import { AvatarCore } from '../../../components/base/avatar';
13+
import { getPropsDefault } from '../../test.tools.ts';
14+
15+
16+
const { props, useAvatar } = AvatarCore;
17+
const defaultProps = getPropsDefault(props);
18+
19+
describe('avatar hook test', () => {
20+
21+
describe('expect current class', () => {
22+
23+
it('default', () => {
24+
const { renderInit } = useAvatar(defaultProps);
25+
expect(renderInit().avatarClass)
26+
.toMatchObject(['m-avatar', 'm-avatar-circle', 'm-avatar-default',]);
27+
});
28+
29+
it('square and small',()=>{
30+
const { renderInit } = useAvatar({...defaultProps,variant:'square',size:'small'});
31+
expect(renderInit().avatarClass)
32+
.toMatchObject(['m-avatar', 'm-avatar-square', 'm-avatar-small',]);
33+
})
34+
35+
});
36+
37+
describe('expect current img', () => {
38+
39+
it.skip('empty img',()=>{
40+
41+
})
42+
43+
})
44+
45+
});

core/test/test.tools.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @description test tools
3+
* @author 阿怪
4+
* @date 2025/1/14 09:56
5+
* @version v1.0.0
6+
*
7+
* 江湖的业务千篇一律,复杂的代码好几百行。
8+
*/
9+
import { MCOPO } from '../components/types/props';
10+
11+
export const getPropsDefault = (props: MCOPO<any>) => {
12+
const defaultProps: Record<keyof typeof props, any> = {};
13+
for (const key in props) {
14+
defaultProps[key] = props[key].default;
15+
}
16+
return defaultProps;
17+
};

core/vitest.config.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @description
3+
* @author 阿怪
4+
* @date 2023/4/28 11:46
5+
* @version v1.0.0
6+
*
7+
* 江湖的业务千篇一律,复杂的代码好几百行。
8+
*/
9+
10+
import { defineConfig } from 'vitest/config';
11+
import Vue from '@vitejs/plugin-vue';
12+
import Jsx from '@vitejs/plugin-vue-jsx';
13+
14+
export default defineConfig({
15+
plugins: [Vue(), Jsx()],
16+
test: {
17+
environment: 'happy-dom',
18+
clearMocks: true,
19+
coverage: {
20+
provider: 'v8',
21+
all: true,
22+
include: ['components/**', 'compositions/**'],
23+
},
24+
},
25+
});

headless/components/base/avatar/MAvatar.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ import { type AvatarProps } from '@shuimo-design/ui-core/types/index';
1111
import { AvatarCore } from '@shuimo-design/ui-core';
1212
import './avatar.css';
1313

14-
const props = AvatarCore.props;
14+
const { props, useAvatar } = AvatarCore;
1515

1616
export default defineComponent((_props: AvatarProps) => {
17-
const props = _props as Required<AvatarProps>;
17+
const { renderInit } = useAvatar(_props);
1818
return () => {
19-
return <div class={['m-avatar', `m-avatar-${props.variant}`, `m-avatar-${props.size}`]}>
20-
<img src={props.img} alt=""/>
19+
const { avatarClass, img } = renderInit();
20+
return <div class={avatarClass}>
21+
{img}
2122
</div>;
2223
};
2324
}, {

lib/components/base/avatar/MAvatar.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,19 @@
77
* 江湖的业务千篇一律,复杂的代码好几百行。
88
*/
99
import { defineComponent } from 'vue';
10-
import { props } from './api.ts';
11-
import './avatar.css';
10+
import { type AvatarProps } from '@shuimo-design/ui-core/types/index';
11+
import { AvatarCore } from '@shuimo-design/ui-core';
1212
import MAvatarSvg from './MAvatarSvg.tsx';
13-
import { AvatarProps } from './index';
13+
import './avatar.css';
14+
15+
const { props, useAvatar } = AvatarCore;
1416

1517
export default defineComponent((_props: AvatarProps) => {
16-
const props = _props as Required<AvatarProps>;
18+
const { renderInit, props } = useAvatar(_props);
1719
return () => {
18-
return <div class={['m-avatar', `m-avatar-${props.variant}`, `m-avatar-${props.size}`]}>
19-
<img src={props.img} alt=""/>
20+
const { avatarClass, img } = renderInit();
21+
return <div class={avatarClass}>
22+
{img}
2023
<MAvatarSvg variant={props.variant}/>
2124
</div>;
2225
};

lib/components/base/avatar/MAvatarSvg.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@
77
* 江湖的业务千篇一律,复杂的代码好几百行。
88
*/
99
import { computed, defineComponent } from 'vue';
10-
import { AVATAR_CIRCLE_ID, AVATAR_SQUARE_ID } from '../../other/svg/MSvgSymbol.tsx';
11-
import { AvatarProps } from './index';
12-
import { props } from './api.ts';
10+
import { AvatarCore } from '@shuimo-design/ui-core';
11+
import { type AvatarProps } from '@shuimo-design/ui-core/types/index';
1312
import useSvgInject from '../../../compositions/common/useSvgInject.ts';
13+
import { AVATAR_CIRCLE_ID, AVATAR_SQUARE_ID } from '../../other/svg/MSvgSymbol.tsx';
1414

1515
type SvgInfoType = { viewBox: string; id: string };
1616
const avatarSvgInfo: Record<keyof AvatarProps['variant'], SvgInfoType> = {
1717
circle: { viewBox: '0 0 837.14 743.36', id: AVATAR_CIRCLE_ID },
1818
square: { viewBox: '0 0 832.79 828.92', id: AVATAR_SQUARE_ID },
1919
};
2020

21+
const { props } = AvatarCore;
22+
2123
export default defineComponent((props: Pick<AvatarProps, 'variant'>) => {
2224
const {
2325
svgUrl,

0 commit comments

Comments
 (0)