diff --git a/packages/ivy-design-wc/src/components/message-box/index.ce.vue b/packages/ivy-design-wc/src/components/message-box/index.ce.vue new file mode 100644 index 0000000..6e2eb98 --- /dev/null +++ b/packages/ivy-design-wc/src/components/message-box/index.ce.vue @@ -0,0 +1,284 @@ + + + + + + + + + + {{ props.title }} + + + + + + + + + {{ props.message }} + + + + + {{ props.inputErrorMessage }} + + + + + + + + diff --git a/packages/ivy-design-wc/src/components/message-box/index.ts b/packages/ivy-design-wc/src/components/message-box/index.ts new file mode 100644 index 0000000..cad6913 --- /dev/null +++ b/packages/ivy-design-wc/src/components/message-box/index.ts @@ -0,0 +1,115 @@ +import { defineCustomElement } from 'vue' +import { install } from '@/utils/index' +import comp from './index.ce.vue' + +const MessageBox = defineCustomElement(comp) + +const registerComponent = (prefix = 'Ivy') => { + const key = `${prefix}${comp.name}` + install(key, comp) +} + +export type MessageBoxOptionsType = 'info' | 'success' | 'warning' | 'error' + +export interface MessageBoxOptions { + title?: string + type?: MessageBoxOptionsType + message?: string + showClose?: boolean + showCancelButton?: boolean + cancelButtonText?: string + confirmButtonText?: string + callback?: (action: 'confirm' | 'cancel') => void + showInput?: boolean + inputPattern?: RegExp + inputPlaceholder?: string + inputValue?: string + inputType?: 'text' | 'number' | 'password' + inputValidator?: (value: string) => boolean | string + inputErrorMessage?: string + mode?: 'alert' | 'confirm' | 'prompt' +} + +const defaultConfig: MessageBoxOptions = { + type: 'info', + title: '', + message: '', + showClose: true, + showCancelButton: false, + cancelButtonText: '取消', + confirmButtonText: '确认', + callback: () => {}, + showInput: false, + + inputPlaceholder: '', + inputValue: '', + inputType: 'text', + inputErrorMessage: '输入的数据不合法!' +} + +const msgBox = (opts: MessageBoxOptions) => { + return new Promise(() => { + const conf = { ...defaultConfig, ...opts } + console.log(conf) + + const el: any = new MessageBox() + el.title = conf.title + el.type = conf.type + el.message = conf.message + el.showClose = conf.showClose + el.showCancelButton = conf.showCancelButton + el.cancelButtonText = conf.cancelButtonText + el.confirmButtonText = conf.confirmButtonText + el.callback = conf.callback + el.showInput = conf.showInput + el.inputPlaceholder = conf.inputPlaceholder + el.inputValue = conf.inputValue + el.inputType = conf.inputType + el.inputValidator = conf.inputValidator + el.inputErrorMessage = conf.inputErrorMessage + + console.log(el) + document.body.appendChild(el) + + el.open() + }) +} + +const $alert = (message: string, title: string = '提示', opts: MessageBoxOptions = {}) => { + const conf = { + ...opts, + title, + message, + showCancelButton: false + } + msgBox(conf) +} + +const $confirm = (message: string, title: string = '确认', opts: MessageBoxOptions = {}) => { + const conf = { + ...opts, + title, + message, + showCancelButton: true + } + return msgBox(conf) +} + +const $prompt = (message: string, title: string = '输入', opts: MessageBoxOptions = {}) => { + const conf = { + ...opts, + title, + message, + showCancelButton: true, + showInput: true + } + return msgBox(conf) +} + +msgBox.alert = $alert +msgBox.confirm = $confirm +msgBox.prompt = $prompt + +export { MessageBox, registerComponent as registerMessageBoxComponent, msgBox } + +export default registerComponent diff --git a/packages/ivy-design-wc/src/index.ts b/packages/ivy-design-wc/src/index.ts index c078823..8a67af0 100644 --- a/packages/ivy-design-wc/src/index.ts +++ b/packages/ivy-design-wc/src/index.ts @@ -61,6 +61,8 @@ import { Link } from './components/link/index' import { Text } from './components/text/index' import { Affix } from './components/affix/index' +import { MessageBox, msgBox } from './components/message-box/index' + import { createMessage } from './utils/utils' const comp: Record = { @@ -124,10 +126,11 @@ const comp: Record = { Tree, Link, Text, - Affix + Affix, + MessageBox } -export const registerComponents = async (prefix = 'Ivy') => { +const registerComponents = async (prefix = 'Ivy') => { for (const key in comp) { const name: string = `${prefix}${key}`.replace(/([A-Z])([a-z]+)/g, (val, _, p, offset) => { return offset > 0 ? `-${val.toLowerCase()}` : `${val.toLowerCase()}` @@ -137,12 +140,27 @@ export const registerComponents = async (prefix = 'Ivy') => { } } -export const message = createMessage(Message) +const message = createMessage(Message) -export const registerComponent = (name: string, component: any) => { +const registerComponent = (name: string, component: any) => { customElements.define(name, component) } +const $alert = msgBox.alert +const $confirm = msgBox.confirm +const $ivy = { + message, + msgBox, + alert: $alert, + confirm: $confirm +} + +// const $prompt = msgBox.prompt +// const $notify = msgBox.notify +// const $message = msgBox.message + +export { message, msgBox, registerComponents, registerComponent, $ivy, $alert, $confirm } + export default { registerComponents, registerComponent, @@ -212,5 +230,6 @@ declare module 'vue' { Link: typeof comp.Link Text: typeof comp.Text Affix: typeof comp.Affix + MessageBox: typeof comp.MessageBox } } diff --git a/packages/ivy-design-wc/src/utils/icons.tsx b/packages/ivy-design-wc/src/utils/icons.tsx index 8e1a3c1..2c214fe 100644 --- a/packages/ivy-design-wc/src/utils/icons.tsx +++ b/packages/ivy-design-wc/src/utils/icons.tsx @@ -103,3 +103,18 @@ export const CaretRight = (props: Record = {}) => { ) } + +export const CloseIcon = (props: Record = {}) => { + return ( + + + + ) +} diff --git a/packages/ivy-design-wc/src/utils/utils.ts b/packages/ivy-design-wc/src/utils/utils.ts index 78675e4..060a581 100644 --- a/packages/ivy-design-wc/src/utils/utils.ts +++ b/packages/ivy-design-wc/src/utils/utils.ts @@ -68,7 +68,23 @@ const createMessage = (Message: CustomElementConstructor) => { const curNotificationIndex = ref(0) -export { createMessage, curMessageIndex, curNotificationIndex } +const getType = (val: T): string => { + return Object.prototype.toString.call(val).slice(8, -1).toLowerCase() +} + +const isArray = (val: T): boolean => { + if ('isArray' in Array) { + return Array.isArray(val) + } else { + return getType(val) === 'array' + } +} + +const isFunction = (val: T): boolean => { + return getType(val) === 'function' +} + +export { createMessage, curMessageIndex, curNotificationIndex, getType, isArray, isFunction } export default { createMessage, curMessageIndex, diff --git a/packages/ivy-design-wc/vite.config.ts b/packages/ivy-design-wc/vite.config.ts index 9664f6a..aee50ef 100644 --- a/packages/ivy-design-wc/vite.config.ts +++ b/packages/ivy-design-wc/vite.config.ts @@ -5,7 +5,6 @@ import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' import vueJsx from '@vitejs/plugin-vue-jsx' import dts from 'vite-plugin-dts' -import { c } from 'vitest/dist/reporters-5f784f42.js' const genInput = () => { const target: any = {} @@ -23,7 +22,13 @@ export default defineConfig({ appType: 'custom', publicDir: false, plugins: [ - vue(), + vue({ + template: { + compilerOptions: { + isCustomElement: (tag) => tag.startsWith('ivy-') + } + } + }), vueJsx(), dts({ outDir: 'types'
{{ props.title }}
{{ props.message }}
{{ props.inputErrorMessage }}