mirror of
https://github.com/tusen-ai/naive-ui.git
synced 2024-12-09 04:31:35 +08:00
feat(avatar): clsPrefix
This commit is contained in:
parent
ca6dfa10c1
commit
3af0891d2c
@ -1,2 +1,2 @@
|
|||||||
/* istanbul ignore file */
|
|
||||||
export { default as NAvatar } from './src/Avatar'
|
export { default as NAvatar } from './src/Avatar'
|
||||||
|
export type { AvatarProps } from './src/Avatar'
|
||||||
|
@ -5,37 +5,44 @@ import {
|
|||||||
onUpdated,
|
onUpdated,
|
||||||
onMounted,
|
onMounted,
|
||||||
defineComponent,
|
defineComponent,
|
||||||
PropType
|
PropType,
|
||||||
|
ExtractPropTypes
|
||||||
} from 'vue'
|
} from 'vue'
|
||||||
import { useTheme } from '../../_mixins'
|
import { useConfig, useTheme } from '../../_mixins'
|
||||||
import type { ThemeProps } from '../../_mixins'
|
import type { ThemeProps } from '../../_mixins'
|
||||||
import { avatarLight } from '../styles'
|
import { avatarLight } from '../styles'
|
||||||
import type { AvatarTheme } from '../styles'
|
import type { AvatarTheme } from '../styles'
|
||||||
import { createKey } from '../../_utils'
|
import { createKey } from '../../_utils'
|
||||||
import style from './styles/index.cssr'
|
import style from './styles/index.cssr'
|
||||||
|
|
||||||
|
const avatarProps = {
|
||||||
|
...(useTheme.props as ThemeProps<AvatarTheme>),
|
||||||
|
size: {
|
||||||
|
type: [String, Number] as PropType<
|
||||||
|
number | 'tiny' | 'small' | 'medium' | 'large' | 'huge'
|
||||||
|
>,
|
||||||
|
default: 'medium'
|
||||||
|
},
|
||||||
|
src: String,
|
||||||
|
circle: {
|
||||||
|
type: Boolean,
|
||||||
|
default: false
|
||||||
|
},
|
||||||
|
round: {
|
||||||
|
type: Boolean,
|
||||||
|
default: false
|
||||||
|
},
|
||||||
|
color: String
|
||||||
|
} as const
|
||||||
|
|
||||||
|
export type AvatarProps = Partial<ExtractPropTypes<typeof avatarProps>>
|
||||||
|
|
||||||
export default defineComponent({
|
export default defineComponent({
|
||||||
name: 'Avatar',
|
name: 'Avatar',
|
||||||
props: {
|
props: avatarProps,
|
||||||
...(useTheme.props as ThemeProps<AvatarTheme>),
|
|
||||||
size: {
|
|
||||||
type: [String, Number] as PropType<
|
|
||||||
number | 'tiny' | 'small' | 'medium' | 'large' | 'huge'
|
|
||||||
>,
|
|
||||||
default: 'medium'
|
|
||||||
},
|
|
||||||
src: String,
|
|
||||||
circle: {
|
|
||||||
type: Boolean,
|
|
||||||
default: false
|
|
||||||
},
|
|
||||||
round: {
|
|
||||||
type: Boolean,
|
|
||||||
default: false
|
|
||||||
},
|
|
||||||
color: String
|
|
||||||
},
|
|
||||||
setup (props) {
|
setup (props) {
|
||||||
|
const { mergedClsPrefix } = useConfig(props)
|
||||||
|
|
||||||
let memoedTextHtml: string | null = null
|
let memoedTextHtml: string | null = null
|
||||||
const textRef = ref<HTMLElement | null>(null)
|
const textRef = ref<HTMLElement | null>(null)
|
||||||
const selfRef = ref<HTMLElement | null>(null)
|
const selfRef = ref<HTMLElement | null>(null)
|
||||||
@ -64,10 +71,18 @@ export default defineComponent({
|
|||||||
onUpdated(() => {
|
onUpdated(() => {
|
||||||
adjustText()
|
adjustText()
|
||||||
})
|
})
|
||||||
const themeRef = useTheme('Avatar', 'Avatar', style, avatarLight, props)
|
const themeRef = useTheme(
|
||||||
|
'Avatar',
|
||||||
|
'Avatar',
|
||||||
|
style,
|
||||||
|
avatarLight,
|
||||||
|
props,
|
||||||
|
mergedClsPrefix
|
||||||
|
)
|
||||||
return {
|
return {
|
||||||
textRef,
|
textRef,
|
||||||
selfRef,
|
selfRef,
|
||||||
|
cPrefix: mergedClsPrefix,
|
||||||
cssVars: computed(() => {
|
cssVars: computed(() => {
|
||||||
const { size, round, circle } = props
|
const { size, round, circle } = props
|
||||||
const {
|
const {
|
||||||
@ -91,15 +106,19 @@ export default defineComponent({
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
render () {
|
render () {
|
||||||
const { $slots, src } = this
|
const { $slots, src, cPrefix } = this
|
||||||
return (
|
return (
|
||||||
<span ref="selfRef" class="n-avatar" style={this.cssVars as any}>
|
<span
|
||||||
|
ref="selfRef"
|
||||||
|
class={`${cPrefix}-avatar`}
|
||||||
|
style={this.cssVars as any}
|
||||||
|
>
|
||||||
{!$slots.default && src ? (
|
{!$slots.default && src ? (
|
||||||
<img src={src} />
|
<img src={src} />
|
||||||
) : (
|
) : (
|
||||||
<span
|
<span
|
||||||
ref="textRef"
|
ref="textRef"
|
||||||
class="n-avatar__text"
|
class={`${cPrefix}-avatar__text`}
|
||||||
style={{ background: this.color }}
|
style={{ background: this.color }}
|
||||||
>
|
>
|
||||||
{$slots}
|
{$slots}
|
||||||
|
Loading…
Reference in New Issue
Block a user