feat: adapt Vue 3.x (#755)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This commit is contained in:
Zen
2023-05-05 13:57:35 +08:00
committed by GitHub
parent 84c36c5c68
commit fa9eef757a
5 changed files with 54 additions and 29 deletions
+29 -20
View File
@@ -4,12 +4,13 @@
import type { VNode } from 'vue'; import type { VNode } from 'vue';
import type { Prefix } from '@polkadot/util-crypto/address/types'; import type { Prefix } from '@polkadot/util-crypto/address/types';
import { defineComponent } from 'vue'; import { defineComponent, h } from 'vue';
import { isHex, isU8a, u8aToHex } from '@polkadot/util'; import { isHex, isU8a, u8aToHex } from '@polkadot/util';
import { decodeAddress, encodeAddress } from '@polkadot/util-crypto'; import { decodeAddress, encodeAddress } from '@polkadot/util-crypto';
import { Beachball, Empty, Jdenticon, Polkadot } from './icons/index.js'; import { Beachball, Empty, Jdenticon, Polkadot } from './icons/index.js';
import { adaptVNodeAttrs } from './util.js';
interface Account { interface Account {
address: string; address: string;
@@ -26,7 +27,7 @@ interface Data {
const DEFAULT_SIZE = 64; const DEFAULT_SIZE = 64;
function encodeAccount (value: string | Uint8Array, prefix?: Prefix): Account { export function encodeAccount (value: string | Uint8Array, prefix?: Prefix): Account {
try { try {
const address = isU8a(value) || isHex(value) const address = isU8a(value) || isHex(value)
? encodeAddress(value as string, prefix) ? encodeAddress(value as string, prefix)
@@ -81,23 +82,25 @@ export const Identicon = defineComponent({
} }
}, },
props: ['prefix', 'isAlternative', 'size', 'theme', 'value'], props: ['prefix', 'isAlternative', 'size', 'theme', 'value'],
render (h): VNode { render (): VNode {
const { address, iconSize, isAlternativeIcon, publicKey, type } = this.$data; const { address, iconSize, isAlternativeIcon, publicKey, type } = this.$data;
if (type === 'empty') { if (type === 'empty') {
return h('Empty', { return h(Empty, {
attrs: { ...adaptVNodeAttrs({
key: address, key: address,
size: iconSize size: iconSize
} })
}, []); }, []);
} else if (type === 'jdenticon') { } else if (type === 'jdenticon') {
return h('Jdenticon', { return h(Jdenticon, {
attrs: { ...adaptVNodeAttrs(
key: address, {
publicKey, key: address,
size: iconSize publicKey,
} size: iconSize
}
)
}, []); }, []);
} else if (type === 'substrate') { } else if (type === 'substrate') {
throw new Error('substrate type is not supported'); throw new Error('substrate type is not supported');
@@ -105,14 +108,20 @@ export const Identicon = defineComponent({
const cmp = type.charAt(0).toUpperCase() + type.slice(1); const cmp = type.charAt(0).toUpperCase() + type.slice(1);
return h(cmp, { if (['Beachball', 'Polkadot'].includes(cmp)) {
attrs: { const component = cmp === 'Beachball' ? Beachball : Polkadot;
address,
isAlternative: isAlternativeIcon, return h(component, {
key: address, ...adaptVNodeAttrs({
size: iconSize address,
} isAlternative: isAlternativeIcon,
}, []); key: address,
size: iconSize
})
}, []);
} else {
return h(cmp, {}, []);
}
}, },
watch: { watch: {
value: function (): void { value: function (): void {
@@ -3,7 +3,7 @@
import type { VNode } from 'vue'; import type { VNode } from 'vue';
import { defineComponent } from 'vue'; import { defineComponent, h } from 'vue';
import { beachballIcon } from '@polkadot/ui-shared'; import { beachballIcon } from '@polkadot/ui-shared';
@@ -19,7 +19,7 @@ type PropsType = {
*/ */
export const Beachball = defineComponent({ export const Beachball = defineComponent({
props: ['address', 'size', 'isAlternative'], props: ['address', 'size', 'isAlternative'],
render (h): VNode { render (): VNode {
const { address, isAlternative, size } = this.$props as PropsType; const { address, isAlternative, size } = this.$props as PropsType;
return h({ return h({
@@ -4,7 +4,7 @@
import type { VNode } from 'vue'; import type { VNode } from 'vue';
import * as jdenticon from 'jdenticon'; import * as jdenticon from 'jdenticon';
import { defineComponent } from 'vue'; import { defineComponent, h } from 'vue';
type PropsType = { type PropsType = {
publicKey: string, publicKey: string,
@@ -17,7 +17,7 @@ type PropsType = {
*/ */
export const Jdenticon = defineComponent({ export const Jdenticon = defineComponent({
props: ['publicKey', 'size'], props: ['publicKey', 'size'],
render (h): VNode { render (): VNode {
const { publicKey, size } = this.$props as PropsType; const { publicKey, size } = this.$props as PropsType;
return h({ return h({
+7 -5
View File
@@ -3,10 +3,12 @@
import type { VNode } from 'vue'; import type { VNode } from 'vue';
import { defineComponent } from 'vue'; import { defineComponent, h } from 'vue';
import { polkadotIcon } from '@polkadot/ui-shared'; import { polkadotIcon } from '@polkadot/ui-shared';
import { adaptVNodeAttrs } from '../util.js';
type propsType = { type propsType = {
address: string; address: string;
isAlternative: boolean; isAlternative: boolean;
@@ -19,18 +21,18 @@ type propsType = {
*/ */
export const Polkadot = defineComponent({ export const Polkadot = defineComponent({
props: ['address', 'isAlternative', 'size'], props: ['address', 'isAlternative', 'size'],
render (h): VNode { render (): VNode {
const { address, isAlternative, size } = this.$props as propsType; const { address, isAlternative, size } = this.$props as propsType;
const circles = polkadotIcon(address, { isAlternative }).map(({ cx, cy, fill, r }) => const circles = polkadotIcon(address, { isAlternative }).map(({ cx, cy, fill, r }) =>
h('circle', { attrs: { cx, cy, fill, r } }, []) h('circle', { ...adaptVNodeAttrs({ cx, cy, fill, r }) }, [])
); );
return h('svg', { return h('svg', {
attrs: { ...adaptVNodeAttrs({
height: size, height: size,
viewBox: '0 0 64 64', viewBox: '0 0 64 64',
width: size width: size
} })
}, circles); }, circles);
} }
}); });
+14
View File
@@ -0,0 +1,14 @@
// Copyright 2017-2023 @polkadot/vue-identicon authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { version } from 'vue';
export function adaptVNodeAttrs (data: Record<string, any>) {
if (version.startsWith('3.')) {
return data;
} else {
return {
attrs: data
};
}
}