Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multi inits issues with initTE method #1725

Merged
merged 1 commit into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,5 @@
"src/js/plugin.js",
"src/js/plugin.cjs"
],
"extends": ["eslint:recommended", "prettier"],
"globals": {
"initiatedComponents": "writable"
}
"extends": ["eslint:recommended", "prettier"]
}
19 changes: 19 additions & 0 deletions src/js/autoinit/Register.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class InitRegister {
constructor() {
this.inits = [];
}

get initialized() {
return this.inits;
}

isInited(componentName) {
return this.inits.includes(componentName);
}

add(componentName) {
this.inits.push(componentName);
}
}

export default InitRegister;
15 changes: 8 additions & 7 deletions src/js/autoinit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import {

import { chartsCallback } from "./chartsInit";

import InitRegister from "./Register";

const register = new InitRegister();

const defaultInitSelectors = {
alert: {
name: "Alert",
Expand Down Expand Up @@ -138,10 +142,11 @@ const getComponentData = (component) => {
};

const initComponent = (component) => {
if (!component || initiatedComponents?.includes(component.NAME)) {
if (!component || register.isInited(component.NAME)) {
return;
}
initiatedComponents?.push(component.NAME);

register.add(component.NAME);

const thisComponent = getComponentData(component);
const isToggler = thisComponent?.isToggler || false;
Expand Down Expand Up @@ -178,11 +183,7 @@ const initTE = (components, checkOtherImports = false) => {
);
if (requireAutoinit) {
const component = components[defaultInitSelectors[element].name];
if (
!component &&
!initiatedComponents?.includes(element) &&
checkOtherImports
) {
if (!component && !register.isInited(element) && checkOtherImports) {
console.warn(
`Please import ${defaultInitSelectors[element].name} from "tw-elements" package and add it to a object parameter inside "initTE" function`
);
Expand Down
1 change: 0 additions & 1 deletion types/env.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export default defineConfig({
define: {
// for popper to not crash in docs
"process.env": {},
initiatedComponents: [],
},
build: {
outDir: distName,
Expand Down