|
108 | 108 | ref,
|
109 | 109 | watch,
|
110 | 110 | Teleport,
|
111 |
| - type RendererElement, |
| 111 | + getCurrentInstance, |
112 | 112 | } from "vue";
|
113 | 113 | import _ from "lodash";
|
114 | 114 |
|
115 | 115 | import { useI18n, useSwal } from "@open-xamu-co/ui-common-helpers";
|
| 116 | + import { eColors } from "@open-xamu-co/ui-common-enums"; |
116 | 117 |
|
117 | 118 | import BaseErrorBoundary from "./base/ErrorBoundary.vue";
|
118 | 119 | import BaseWrapper from "./base/Wrapper.vue";
|
|
122 | 123 | import ActionButtonToggle from "./action/ButtonToggle.vue";
|
123 | 124 | import LoaderSimple from "./loader/Simple.vue";
|
124 | 125 |
|
125 |
| - import type { iUseThemeProps } from "../types/props"; |
| 126 | + import type { iModalButtonConfig, iModalProps } from "../types/props"; |
126 | 127 | import useTheme from "../composables/theme";
|
127 | 128 | import useUUID from "../composables/uuid";
|
128 | 129 | import { useHelpers } from "../composables/utils";
|
129 | 130 |
|
130 |
| - interface iButtonConfig { |
131 |
| - title?: string; |
132 |
| - visible?: boolean; |
133 |
| - btnClass?: string; |
134 |
| - } |
135 |
| -
|
136 |
| - interface iModalProps extends iUseThemeProps { |
137 |
| - /** |
138 |
| - * Modal is loading |
139 |
| - * Some of the modal contents could be still loading |
140 |
| - */ |
141 |
| - loading?: boolean; |
142 |
| - /** |
143 |
| - * The title of the modal shown in .x-modal-header div. If empty title is not rendered |
144 |
| - */ |
145 |
| - title?: string; |
146 |
| - subtitle?: string; |
147 |
| - /** |
148 |
| - * :class object which is attached to the modal modal element |
149 |
| - */ |
150 |
| - modalClass?: string | string[] | Record<string, boolean>; |
151 |
| - /** |
152 |
| - * Save button config |
153 |
| - */ |
154 |
| - saveButton?: iButtonConfig & { disabled?: boolean }; |
155 |
| - /** |
156 |
| - * Cancel button config |
157 |
| - */ |
158 |
| - cancelButton?: iButtonConfig; |
159 |
| - /** |
160 |
| - * Are modal requirement meet? |
161 |
| - * This is intended to prevent the usage of certain modals |
162 |
| - * |
163 |
| - * Ex: user does not have enough permissions |
164 |
| - */ |
165 |
| - hide?: boolean; |
166 |
| - hideMessage?: string; |
167 |
| - hideFooter?: boolean; |
168 |
| - /** |
169 |
| - * disables modal |
170 |
| - */ |
171 |
| - disabled?: boolean; |
172 |
| - // PRIVATE |
173 |
| - /** |
174 |
| - * Shows/hides the modal |
175 |
| - * @private |
176 |
| - */ |
177 |
| - modelValue?: boolean; |
178 |
| - target?: string | RendererElement; |
179 |
| - } |
180 |
| -
|
181 | 131 | /**
|
182 | 132 | * Based on @innologica/vue-stackable-modal
|
183 | 133 | * Modified to support vue3
|
|
188 | 138 |
|
189 | 139 | defineOptions({ name: "ModalSimple", inheritAttrs: false });
|
190 | 140 |
|
191 |
| - const props = defineProps<iModalProps>(); |
| 141 | + const props = withDefaults(defineProps<iModalProps>(), { |
| 142 | + theme: eColors.SECONDARY, |
| 143 | + }); |
192 | 144 | const emit = defineEmits(["save", "close", "update:model-value"]);
|
193 | 145 |
|
194 | 146 | const { t } = useHelpers(useI18n);
|
195 | 147 | const Swal = useHelpers(useSwal);
|
196 | 148 | const { themeClasses, invertedThemeValues } = useTheme(props, true);
|
| 149 | + const router = getCurrentInstance()?.appContext.config.globalProperties.$router; |
197 | 150 | const { uuid } = useUUID();
|
198 | 151 |
|
199 | 152 | const resolver = ref<(r?: boolean) => void>();
|
|
208 | 161 |
|
209 | 162 | return `modal_${seed.replaceAll(" ", "") || randomId}`;
|
210 | 163 | });
|
211 |
| - const saveButtonOptions = computed<iButtonConfig & { disabled?: boolean }>(() => ({ |
| 164 | + const saveButtonOptions = computed<iModalButtonConfig & { disabled?: boolean }>(() => ({ |
212 | 165 | title: t("ok"),
|
213 | 166 | visible: !!props.saveButton?.title,
|
214 | 167 | btnClass: "",
|
215 | 168 | ...(!!props.saveButton && props.saveButton),
|
216 | 169 | }));
|
217 |
| - const cancelButtonOptions = computed<iButtonConfig>(() => ({ |
| 170 | + const cancelButtonOptions = computed<iModalButtonConfig>(() => ({ |
218 | 171 | title: t("close"),
|
219 | 172 | visible: true,
|
220 | 173 | btnClass: "",
|
|
283 | 236 | },
|
284 | 237 | { immediate: true }
|
285 | 238 | );
|
| 239 | +
|
| 240 | + if (!router?.currentRoute) return; |
| 241 | +
|
| 242 | + // close on route change |
| 243 | + watch(router.currentRoute, () => closeModal(), { immediate: false }); |
286 | 244 | });
|
287 | 245 | onUnmounted(closeModal);
|
288 | 246 | </script>
|
0 commit comments