Added custom theme

This commit is contained in:
MaxOvs19 2023-02-14 20:19:57 +03:00
parent 4a2e8c9b17
commit fd8fc093d0
8 changed files with 69 additions and 32 deletions

View File

@ -5,6 +5,10 @@
font-family: Arial, Helvetica, sans-serif; font-family: Arial, Helvetica, sans-serif;
} }
.testClassRed {
background-color: #ff8282;
}
.body-example { .body-example {
background: #000000c4; background: #000000c4;
} }

View File

@ -1,5 +1,6 @@
import { CGSelect } from '../src/cg-select'; import { CGSelect } from '../src/cg-select';
import './example'; import './example';
import { newCustomTheme } from './themeTest';
// ------------------------------Обычный селект-------------------- // ------------------------------Обычный селект--------------------
const dropdown = new CGSelect({ const dropdown = new CGSelect({
@ -25,11 +26,12 @@ const dropdown = new CGSelect({
width: '824px', width: '824px',
}, },
}, },
theme: newCustomTheme,
}); });
dropdown.on('clear', function (e) { // dropdown.on('clear', function (e) {
console.log(`this state: ${e}`); // console.log(`this state: ${e}`);
}); // });
// ------------------------------NativeSelect----------------------- // ------------------------------NativeSelect-----------------------
const dropdownNativeSelect = new CGSelect({ const dropdownNativeSelect = new CGSelect({

9
example/themeTest.ts Normal file
View File

@ -0,0 +1,9 @@
import { CustomTheme } from 'components/theme/theme.interface';
export const newCustomTheme: CustomTheme = {
name: 'test',
styles: {
head: 'testClassRed',
list: 'testClassRed',
},
};

View File

@ -25,6 +25,7 @@ import { ILanguage } from './interfaces/language.interface';
import './main.scss'; import './main.scss';
import { changeTheme } from './components/theme/theme'; import { changeTheme } from './components/theme/theme';
import { CustomTheme } from 'components/theme/theme.interface';
/** /**
* @class Class Description ICgSelect * @class Class Description ICgSelect
@ -36,7 +37,7 @@ export class CGSelect implements ICgSelect {
selected?: string; selected?: string;
placeholder?: string; placeholder?: string;
items?: IItems[] | string[] | any; items?: IItems[] | string[] | any;
theme?: string; theme?: string | CustomTheme;
searchMode?: boolean; searchMode?: boolean;
closeOnSelect?: boolean; closeOnSelect?: boolean;
nativeSelectMode?: boolean; nativeSelectMode?: boolean;

View File

@ -0,0 +1,12 @@
export interface CustomTheme {
name: string;
styles: {
head?: string;
list?: string;
placeholder?: string;
caret?: string;
search?: string;
chips?: string;
lable?: string;
};
}

View File

@ -1,4 +1,6 @@
export function changeTheme(element: Element, theme: string) { import { CustomTheme } from './theme.interface';
export function changeTheme(element: Element, theme: string | CustomTheme) {
const select = element!.querySelector('.cg-select'); const select = element!.querySelector('.cg-select');
const caret = element!.querySelector('.caret'); const caret = element!.querySelector('.caret');
const list = element!.querySelector('ul.list'); const list = element!.querySelector('ul.list');
@ -13,7 +15,8 @@ export function changeTheme(element: Element, theme: string) {
elem.classList.remove('pathWhite'); elem.classList.remove('pathWhite');
}); });
switch (theme.toLowerCase()) { if (typeof theme === 'string') {
switch (theme) {
case 'dark': case 'dark':
select!.classList.add('selectDark'); select!.classList.add('selectDark');
list!.classList.add('listDark'); list!.classList.add('listDark');
@ -41,4 +44,8 @@ export function changeTheme(element: Element, theme: string) {
list!.classList.add('classicList'); list!.classList.add('classicList');
break; break;
} }
} else {
select!.classList.add(`${theme.styles.head}`);
list!.classList.add(`${theme.styles.list}`);
}
} }

View File

@ -1,3 +1,4 @@
import { CustomTheme } from 'components/theme/theme.interface';
import { IItems } from 'interfaces/items.interface'; import { IItems } from 'interfaces/items.interface';
/** /**
@ -55,5 +56,5 @@ export interface ISelectedItems {
* An optional parameter that is responsible for enabling a light/dark theme by default, the dark theme is set. * An optional parameter that is responsible for enabling a light/dark theme by default, the dark theme is set.
* @type {boolean} * @type {boolean}
*/ */
theme?: string; theme?: string | CustomTheme;
} }

View File

@ -1,3 +1,4 @@
import { CustomTheme } from 'components/theme/theme.interface';
import { IItems } from './items.interface'; import { IItems } from './items.interface';
/** /**
@ -29,7 +30,7 @@ export interface ICgSelect {
* An optional parameter responsible for switching between different themes, the classic theme is set by default. * An optional parameter responsible for switching between different themes, the classic theme is set by default.
* @type {string} values: dark, white * @type {string} values: dark, white
*/ */
theme?: string; theme?: string | CustomTheme;
/** /**
* An optional parameter that adds a live search on the select elements. * An optional parameter that adds a live search on the select elements.
* @type {boolean} * @type {boolean}