|
2 | 2 | <div ref="mountPoint"></div> |
3 | 3 | </template> |
4 | 4 |
|
5 | | -<script lang="ts"> |
6 | | -import type { StripeElementType } from '@stripe/stripe-js' |
7 | | -import type { |
8 | | - StripeElementsWithoutOverload, |
9 | | - StripeElementOptions, |
10 | | -} from '../../types/vue-stripe' |
11 | | -
|
12 | | -import { createElement } from '../stripe-elements' |
| 5 | +<script setup lang="ts"> |
| 6 | +import type { StripeElementType, StripeElements } from "@stripe/stripe-js" |
13 | 7 | import { |
14 | | - defineComponent, |
| 8 | + defineEmits, |
| 9 | + inject, |
15 | 10 | onBeforeUnmount, |
16 | 11 | onMounted, |
17 | 12 | ref, |
18 | 13 | toRefs, |
19 | 14 | watch, |
20 | | -} from 'vue' |
| 15 | + withDefaults, |
| 16 | +} from "vue" |
| 17 | +import { createElement } from "../stripe-elements" |
| 18 | +import type { StripeElementOptions } from "../stripe-elements" |
21 | 19 |
|
22 | | -export default defineComponent({ |
23 | | - name: 'StripeElement', |
| 20 | +interface Props { |
| 21 | + type?: StripeElementType |
| 22 | + elements?: StripeElements |
| 23 | + options?: StripeElementOptions |
| 24 | +} |
24 | 25 |
|
25 | | - props: { |
26 | | - // elements object |
27 | | - // https://stripe.com/docs/js/elements_object/create |
28 | | - elements: { |
29 | | - type: Object as () => StripeElementsWithoutOverload, |
30 | | - required: true, |
31 | | - }, |
32 | | - // type of the element |
33 | | - // https://stripe.com/docs/js/elements_object/create_element?type=card |
34 | | - type: { |
35 | | - type: String as () => StripeElementType, |
36 | | - default: () => 'card', |
37 | | - }, |
38 | | - // element options |
39 | | - // https://stripe.com/docs/js/elements_object/create_element?type=card#elements_create-options |
40 | | - options: { |
41 | | - type: Object as () => StripeElementOptions, |
42 | | - default: () => ({}), |
43 | | - }, |
44 | | - }, |
| 26 | +const props = withDefaults(defineProps<Props>(), { |
| 27 | + type: () => |
| 28 | + (inject("providedElements") as StripeElementType) ? "payment" : "card", |
| 29 | + elements: () => inject("providedElements") as StripeElements, |
| 30 | +}) |
| 31 | +const { type, elements, options } = toRefs(props) |
45 | 32 |
|
46 | | - setup(props, { emit }) { |
47 | | - const domElement = ref(document.createElement('div')) |
48 | | - const stripeElement = ref() |
49 | | - const mountPoint = ref() |
50 | | - const { elements, type, options } = toRefs(props) |
| 33 | +// TODO: verify it works |
| 34 | +const emit = defineEmits() |
51 | 35 |
|
52 | | - onMounted(() => { |
53 | | - const mountElement = () => { |
54 | | - stripeElement.value = createElement( |
55 | | - elements.value, |
56 | | - type.value, |
57 | | - options.value |
58 | | - ) |
59 | | - mountPoint.value.appendChild(domElement.value) |
60 | | - stripeElement.value.mount(domElement.value) |
61 | | - } |
| 36 | +const domElement = ref(document.createElement("div")) |
| 37 | +const stripeElement = ref() |
| 38 | +const mountPoint = ref() |
62 | 39 |
|
63 | | - // Handle event listeners |
64 | | - const wrapperFn = (t: string, e: Event) => { |
65 | | - emit(t, e) |
66 | | - } |
| 40 | +onMounted(() => { |
| 41 | + const mountElement = () => { |
| 42 | + stripeElement.value = createElement( |
| 43 | + elements.value, |
| 44 | + type.value, |
| 45 | + options.value, |
| 46 | + ) |
| 47 | + mountPoint.value.appendChild(domElement.value) |
| 48 | + stripeElement.value?.mount(domElement.value) |
| 49 | + } |
67 | 50 |
|
68 | | - const handleEvents = () => { |
69 | | - // See stripe element events: https://stripe.com/docs/js/element/events |
70 | | - const eventTypes = [ |
71 | | - 'change', |
72 | | - 'ready', |
73 | | - 'focus', |
74 | | - 'blur', |
75 | | - 'click', |
76 | | - 'escape', |
77 | | - ] |
78 | | - eventTypes.forEach((t) => { |
79 | | - stripeElement.value.on(t, wrapperFn.bind(null, t)) |
80 | | - }) |
81 | | - } |
| 51 | + // Handle event listeners |
| 52 | + const wrapperFn = (t: string, e: Event) => { |
| 53 | + emit(t, e) |
| 54 | + } |
82 | 55 |
|
83 | | - try { |
84 | | - mountElement() |
85 | | - handleEvents() |
86 | | - } catch (error) { |
87 | | - console.error(error) |
88 | | - } |
89 | | - }) |
| 56 | + const handleEvents = () => { |
| 57 | + // See stripe element events: https://stripe.com/docs/js/element/events |
| 58 | + const eventTypes = [ |
| 59 | + "change", |
| 60 | + "ready", |
| 61 | + "focus", |
| 62 | + "blur", |
| 63 | + "click", |
| 64 | + "escape", |
| 65 | + "loaderror", |
| 66 | + "loaderstart", |
| 67 | + ] |
90 | 68 |
|
91 | | - onBeforeUnmount(() => { |
92 | | - stripeElement.value?.unmount() |
93 | | - stripeElement.value?.destroy() |
94 | | - }) |
| 69 | + for (const eventType of eventTypes) { |
| 70 | + stripeElement.value?.on(eventType, wrapperFn.bind(null, eventType)) |
| 71 | + } |
| 72 | + } |
95 | 73 |
|
96 | | - watch(options, () => { |
97 | | - stripeElement.value?.update(props.options) |
98 | | - }) |
| 74 | + try { |
| 75 | + mountElement() |
| 76 | + handleEvents() |
| 77 | + } catch (error) { |
| 78 | + console.error(error) |
| 79 | + } |
| 80 | +}) |
99 | 81 |
|
100 | | - return { |
101 | | - stripeElement, |
102 | | - domElement, |
103 | | - mountPoint, |
104 | | - } |
105 | | - }, |
| 82 | +onBeforeUnmount(() => { |
| 83 | + stripeElement.value?.unmount() |
| 84 | + stripeElement.value?.destroy() |
| 85 | +}) |
| 86 | +
|
| 87 | +watch(options, () => { |
| 88 | + stripeElement.value?.update(props.options) |
| 89 | +}) |
| 90 | +
|
| 91 | +defineExpose({ |
| 92 | + stripeElement, |
| 93 | + domElement, |
| 94 | + mountPoint, |
106 | 95 | }) |
107 | 96 | </script> |
0 commit comments