|
4 | 4 |
|
5 | 5 | <script lang="ts" setup> |
6 | 6 | import type { NotificationPlacement } from 'naive-ui' |
| 7 | +import type { PropType, VNode } from 'vue' |
7 | 8 | import { NButton, NSpace, useNotification } from 'naive-ui' |
8 | | -import { h, ref } from 'vue' |
| 9 | +import { defineComponent, h, ref } from 'vue' |
9 | 10 |
|
10 | | -const placementRef = ref<NotificationPlacement>('top-right') |
11 | | -
|
12 | | -function handlePlacementChange(val: NotificationPlacement) { |
13 | | - placementRef.value = val |
| 11 | +interface Item { |
| 12 | + placement: NotificationPlacement |
| 13 | + text: string |
14 | 14 | } |
15 | 15 |
|
16 | | -function PlacementButtons(props: { |
17 | | - onPlacementChange?: (placement: NotificationPlacement) => void |
18 | | -}) { |
19 | | - const notification = useNotification() |
20 | | - const placementList = [ |
21 | | - { placement: 'top-left' as const, text: 'Top left' }, |
22 | | - { placement: 'top-right' as const, text: 'Top right' }, |
23 | | - { placement: 'bottom-left' as const, text: 'Bottom left' }, |
24 | | - { placement: 'bottom-right' as const, text: 'Bottom right' }, |
25 | | - { placement: 'bottom' as const, text: 'Bottom' }, |
26 | | - { placement: 'top' as const, text: 'Top' } |
27 | | - ] |
| 16 | +const PlacementButtons = defineComponent({ |
| 17 | + props: { |
| 18 | + onPlacementChange: Function as PropType< |
| 19 | + (placement: NotificationPlacement) => void |
| 20 | + > |
| 21 | + }, |
| 22 | + setup(props) { |
| 23 | + const notification = useNotification() |
| 24 | + const placementList: Item[] = [ |
| 25 | + { placement: 'top-left', text: 'Top left' }, |
| 26 | + { placement: 'top-right', text: 'Top right' }, |
| 27 | + { placement: 'bottom-left', text: 'Bottom left' }, |
| 28 | + { placement: 'bottom-right', text: 'Bottom right' }, |
| 29 | + { placement: 'bottom', text: 'Bottom' }, |
| 30 | + { placement: 'top', text: 'Top' } |
| 31 | + ] |
| 32 | + return (): VNode => |
| 33 | + h(NSpace, null, { |
| 34 | + default: () => |
| 35 | + placementList.map((item: Item) => |
| 36 | + h( |
| 37 | + NButton, |
| 38 | + { |
| 39 | + onClick: () => { |
| 40 | + props.onPlacementChange?.(item.placement) |
| 41 | + notification.info({ |
| 42 | + title: item.placement, |
| 43 | + content: 'You can change the placement' |
| 44 | + }) |
| 45 | + } |
| 46 | + }, |
| 47 | + { default: () => item.text } |
| 48 | + ) |
| 49 | + ) |
| 50 | + }) |
| 51 | + } |
| 52 | +}) |
| 53 | +
|
| 54 | +const placementRef = ref<NotificationPlacement>('top-right') |
28 | 55 |
|
29 | | - return h(NSpace, null, { |
30 | | - default: () => |
31 | | - placementList.map(item => |
32 | | - h( |
33 | | - NButton, |
34 | | - { |
35 | | - onClick: () => { |
36 | | - props.onPlacementChange?.(item.placement) |
37 | | - notification.info({ |
38 | | - title: item.placement, |
39 | | - content: 'You can change the placement' |
40 | | - }) |
41 | | - } |
42 | | - }, |
43 | | - { default: () => item.text } |
44 | | - ) |
45 | | - ) |
46 | | - }) |
| 56 | +function handlePlacementChange(placement: NotificationPlacement) { |
| 57 | + placementRef.value = placement |
47 | 58 | } |
48 | 59 | </script> |
49 | 60 |
|
|
0 commit comments