refactor(components): refactor alert (#3365)

This commit is contained in:
三咲智子 2021-09-13 10:48:10 +08:00 committed by GitHub
parent 19752ba778
commit ad12c39ce5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 65 additions and 56 deletions

View File

@ -1,5 +1,5 @@
import { mount } from '@vue/test-utils' import { mount } from '@vue/test-utils'
import Alert from '../src/index.vue' import Alert from '../src/alert.vue'
const AXIOM = 'Rem is the best girl' const AXIOM = 'Rem is the best girl'

View File

@ -1,13 +1,8 @@
import Alert from './src/index.vue' import { withInstall } from '@element-plus/utils/with-install'
import type { App } from 'vue' import Alert from './src/alert.vue'
import type { SFCWithInstall } from '@element-plus/utils/types'
Alert.install = (app: App): void => { export const ElAlert = withInstall(Alert)
app.component(Alert.name, Alert) export default ElAlert
}
const _Alert = Alert as SFCWithInstall<typeof Alert> export * from './src/alert'
export default _Alert
export const ElAlert = _Alert

View File

@ -0,0 +1,48 @@
import type { ExtractPropTypes } from 'vue'
import { buildProp, keyOf } from '@element-plus/utils/props'
export type AlertEffect = 'light' | 'dark'
export const ALERT_TYPE_CLASSES_MAP = {
success: 'el-icon-success',
warning: 'el-icon-warning',
error: 'el-icon-error',
info: 'el-icon-info',
} as const
export const alertProps = {
title: {
type: String,
default: '',
},
description: {
type: String,
default: '',
},
type: buildProp({
type: String,
values: keyOf(ALERT_TYPE_CLASSES_MAP),
default: 'info',
}),
closable: {
type: Boolean,
default: true,
},
closeText: {
type: String,
default: '',
},
showIcon: Boolean,
center: Boolean,
effect: buildProp({
type: String,
values: ['light', 'dark'] as const,
default: 'light',
}),
} as const
export type AlertProps = ExtractPropTypes<typeof alertProps>
export const alertEmits = {
close: (evt: MouseEvent) => evt instanceof MouseEvent,
}
export type AlertEmits = typeof alertEmits

View File

@ -41,68 +41,34 @@
</template> </template>
<script lang="ts"> <script lang="ts">
import { defineComponent, computed, ref } from 'vue' import { defineComponent, computed, ref } from 'vue'
import { alertProps, alertEmits, ALERT_TYPE_CLASSES_MAP } from './alert'
import type { PropType } from 'vue'
const TYPE_CLASSES_MAP = {
success: 'el-icon-success',
warning: 'el-icon-warning',
error: 'el-icon-error',
}
export default defineComponent({ export default defineComponent({
name: 'ElAlert', name: 'ElAlert',
props: {
title: { props: alertProps,
type: String, emits: alertEmits,
default: '',
}, setup(props, { emit, slots }) {
description: {
type: String,
default: '',
},
type: {
type: String as PropType<'success' | 'info' | 'error' | 'warning'>,
default: 'info',
},
closable: {
type: Boolean,
default: true,
},
closeText: {
type: String,
default: '',
},
showIcon: Boolean,
center: Boolean,
effect: {
type: String,
default: 'light',
validator: (value: string): boolean =>
['light', 'dark'].indexOf(value) > -1,
},
},
emits: ['close'],
setup(props, ctx) {
// state // state
const visible = ref(true) const visible = ref(true)
// computed // computed
const typeClass = computed(() => `el-alert--${props.type}`) const typeClass = computed(() => `el-alert--${props.type}`)
const iconClass = computed( const iconClass = computed(
() => TYPE_CLASSES_MAP[props.type] || 'el-icon-info' () => ALERT_TYPE_CLASSES_MAP[props.type] || ALERT_TYPE_CLASSES_MAP['info']
) )
const isBigIcon = computed(() => const isBigIcon = computed(() =>
props.description || ctx.slots.default ? 'is-big' : '' props.description || slots.default ? 'is-big' : ''
) )
const isBoldTitle = computed(() => const isBoldTitle = computed(() =>
props.description || ctx.slots.default ? 'is-bold' : '' props.description || slots.default ? 'is-bold' : ''
) )
// methods // methods
const close = (evt) => { const close = (evt: MouseEvent) => {
visible.value = false visible.value = false
ctx.emit('close', evt) emit('close', evt)
} }
return { return {