new-marketplace
130 строк · 6.6 Кб
1'use client';
2
3import { PrimeReactContext } from 'primereact/api';
4import { Button } from 'primereact/button';
5import { InputSwitch, InputSwitchChangeEvent } from 'primereact/inputswitch';
6import { RadioButton, RadioButtonChangeEvent } from 'primereact/radiobutton';
7import { Sidebar } from 'primereact/sidebar';
8import { classNames } from 'primereact/utils';
9import React, { useContext, useEffect, useState } from 'react';
10import { AppConfigProps, LayoutConfig, LayoutState } from '@/types';
11import { LayoutContext } from './context/layoutcontext';
12
13const AppConfig = (props: AppConfigProps) => {
14const [scales] = useState([12, 13, 14, 15, 16]);
15const { layoutConfig, setLayoutConfig, layoutState, setLayoutState } = useContext(LayoutContext);
16const { setRipple, changeTheme } = useContext(PrimeReactContext);
17
18const onConfigButtonClick = () => {
19setLayoutState((prevState: LayoutState) => ({ ...prevState, configSidebarVisible: true }));
20};
21
22const onConfigSidebarHide = () => {
23setLayoutState((prevState: LayoutState) => ({ ...prevState, configSidebarVisible: false }));
24};
25
26const changeInputStyle = (e: RadioButtonChangeEvent) => {
27setLayoutConfig((prevState: LayoutConfig) => ({ ...prevState, inputStyle: e.value }));
28};
29
30const changeRipple = (e: InputSwitchChangeEvent) => {
31setRipple?.(e.value as boolean);
32setLayoutConfig((prevState: LayoutConfig) => ({ ...prevState, ripple: e.value as boolean }));
33};
34
35const changeMenuMode = (e: RadioButtonChangeEvent) => {
36setLayoutConfig((prevState: LayoutConfig) => ({ ...prevState, menuMode: e.value }));
37};
38
39const _changeTheme = (theme: string, colorScheme: string) => {
40changeTheme?.(layoutConfig.theme, theme, 'theme-css', () => {
41setLayoutConfig((prevState: LayoutConfig) => ({ ...prevState, theme, colorScheme }));
42});
43};
44
45const decrementScale = () => {
46setLayoutConfig((prevState: LayoutConfig) => ({ ...prevState, scale: prevState.scale - 1 }));
47};
48
49const incrementScale = () => {
50setLayoutConfig((prevState: LayoutConfig) => ({ ...prevState, scale: prevState.scale + 1 }));
51};
52
53const applyScale = () => {
54document.documentElement.style.fontSize = layoutConfig.scale + 'px';
55};
56
57useEffect(() => {
58applyScale();
59// eslint-disable-next-line react-hooks/exhaustive-deps
60}, [layoutConfig.scale]);
61
62return (
63<>
64<button className="layout-config-button config-link" type="button" onClick={onConfigButtonClick}>
65<i className="pi pi-cog"></i>
66</button>
67
68<Sidebar visible={layoutState.configSidebarVisible} onHide={onConfigSidebarHide} position="right" className="layout-config-sidebar w-20rem">
69{!props.simple && (
70<>
71<h5>Scale</h5>
72<div className="flex align-items-center">
73<Button icon="pi pi-minus" type="button" onClick={decrementScale} rounded text className="w-2rem h-2rem mr-2" disabled={layoutConfig.scale === scales[0]}></Button>
74<div className="flex gap-2 align-items-center">
75{scales.map((item) => {
76return <i className={classNames('pi pi-circle-fill', { 'text-primary-500': item === layoutConfig.scale, 'text-300': item !== layoutConfig.scale })} key={item}></i>;
77})}
78</div>
79<Button icon="pi pi-plus" type="button" onClick={incrementScale} rounded text className="w-2rem h-2rem ml-2" disabled={layoutConfig.scale === scales[scales.length - 1]}></Button>
80</div>
81
82<h5>Menu Type</h5>
83<div className="flex">
84<div className="field-radiobutton flex-1">
85<RadioButton name="menuMode" value={'static'} checked={layoutConfig.menuMode === 'static'} onChange={(e) => changeMenuMode(e)} inputId="mode1"></RadioButton>
86<label htmlFor="mode1">Static</label>
87</div>
88<div className="field-radiobutton flex-1">
89<RadioButton name="menuMode" value={'overlay'} checked={layoutConfig.menuMode === 'overlay'} onChange={(e) => changeMenuMode(e)} inputId="mode2"></RadioButton>
90<label htmlFor="mode2">Overlay</label>
91</div>
92</div>
93
94<h5>Input Style</h5>
95<div className="flex">
96<div className="field-radiobutton flex-1">
97<RadioButton name="inputStyle" value={'outlined'} checked={layoutConfig.inputStyle === 'outlined'} onChange={(e) => changeInputStyle(e)} inputId="outlined_input"></RadioButton>
98<label htmlFor="outlined_input">Outlined</label>
99</div>
100<div className="field-radiobutton flex-1">
101<RadioButton name="inputStyle" value={'filled'} checked={layoutConfig.inputStyle === 'filled'} onChange={(e) => changeInputStyle(e)} inputId="filled_input"></RadioButton>
102<label htmlFor="filled_input">Filled</label>
103</div>
104</div>
105
106<h5>Ripple Effect</h5>
107<InputSwitch checked={layoutConfig.ripple as boolean} onChange={(e) => changeRipple(e)}></InputSwitch>
108</>
109)}
110<h5>PrimeOne Design</h5>
111<div className="grid">
112<div className="col-3">
113<button className="p-link w-2rem h-2rem" onClick={() => _changeTheme('lara-light-indigo', 'light')}>
114<img src="/layout/images/themes/lara-light-indigo.png" className="w-2rem h-2rem" alt="Lara Light Indigo" />
115</button>
116</div>
117
118<div className="col-3">
119<button className="p-link w-2rem h-2rem" onClick={() => _changeTheme('lara-dark-indigo', 'dark')}>
120<img src="/layout/images/themes/lara-dark-indigo.png" className="w-2rem h-2rem" alt="Lara Dark Indigo" />
121</button>
122</div>
123
124</div>
125</Sidebar>
126</>
127);
128};
129
130export default AppConfig;
131