forked from layer5io/sistent
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpromt-component.tsx
172 lines (154 loc) · 4.49 KB
/
promt-component.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import { forwardRef, useImperativeHandle, useRef, useState } from 'react';
import { Checkbox, FormControlLabel, Typography } from '../../base';
import { useTheme } from '../../theme';
import { Modal, ModalBody, ModalButtonPrimary, ModalButtonSecondary, ModalFooter } from '../Modal';
import { ActionComponent, Subtitle } from './style';
/* Promt variants are used to define the color of the prompt button */
export const PROMPT_VARIANTS = {
WARNING: 'warning',
DANGER: 'error',
SUCCESS: 'success',
INFO: 'info'
} as const;
type PromptVariant = (typeof PROMPT_VARIANTS)[keyof typeof PROMPT_VARIANTS];
interface PromptProps {
variant?: PromptVariant;
}
interface State {
isOpen: boolean;
title: string;
subtitle?: string;
primaryOption: string;
showInfoIcon?: string;
variant?: PromptVariant;
headerIcon?: React.ReactNode;
showCheckbox?: boolean;
isChecked?: boolean;
}
interface ShowParams {
title: string;
subtitle: string;
primaryOption: string;
variant: PromptVariant;
showInfoIcon?: string;
showCheckbox?: boolean;
headerIcon?: React.ReactNode;
}
export interface PromptRef {
show: (params: ShowParams) => Promise<string>;
getCheckboxState: () => boolean;
}
const PromptComponent = forwardRef<PromptRef, PromptProps>(({ variant }, ref) => {
const [state, setState] = useState<State>({
isOpen: false,
title: '',
subtitle: '',
primaryOption: '',
showInfoIcon: '',
variant,
headerIcon: undefined,
isChecked: false,
showCheckbox: false
});
/* This ref is used to store the resolve and reject functions of the promise returned by the show method */
const promiseInfoRef = useRef<{ resolve: (value: string) => void; reject: () => void }>({
resolve: () => {},
reject: () => {}
});
const theme = useTheme();
/* This function is used to show the prompt */
const show = (params: ShowParams) => {
return new Promise<string>((resolve, reject) => {
promiseInfoRef.current = { resolve, reject };
setState({
...params,
isOpen: true,
showInfoIcon: params.showInfoIcon || '',
showCheckbox: !!params.showCheckbox
});
});
};
/* This function is used to hide the prompt */
const hide = () => {
setState((prevState) => ({ ...prevState, isOpen: false }));
};
const handleCheckboxChange = () => {
setState((prevState) => ({ ...prevState, isChecked: !prevState.isChecked }));
};
const getCheckboxState = () => {
return !!state.isChecked;
};
useImperativeHandle(ref, () => ({
show,
getCheckboxState
}));
const { isOpen, primaryOption, title, subtitle, showInfoIcon, headerIcon, showCheckbox } = state;
const { resolve } = promiseInfoRef.current;
return (
<Modal
open={isOpen}
closeModal={hide}
title={title}
id="searchClick"
headerIcon={headerIcon}
reactNode={undefined}
>
{subtitle && (
<ModalBody>
<Subtitle id="alert-dialog-description" variant="body1" component="div">
<Typography
variant="body1"
component="div"
style={{
color: theme.palette.text.primary
}}
>
{subtitle}
</Typography>
</Subtitle>
{showCheckbox && (
<FormControlLabel
control={
<Checkbox
checked={getCheckboxState()}
onChange={handleCheckboxChange}
color="primary"
/>
}
label={<span style={{ fontSize: '1rem' }}>Do not show again</span>}
/>
)}
</ModalBody>
)}
<ModalFooter variant="filled" helpText={showInfoIcon}>
<ActionComponent>
<ModalButtonSecondary
onClick={() => {
hide();
resolve('CANCEL');
}}
>
Cancel
</ModalButtonSecondary>
<ModalButtonPrimary
onClick={() => {
hide();
resolve(primaryOption);
}}
style={
state.variant && {
backgroundColor: theme.palette.background[state.variant]?.default,
textTransform: 'capitalize'
}
}
type="submit"
>
{primaryOption}
</ModalButtonPrimary>
</ActionComponent>
</ModalFooter>
</Modal>
);
});
PromptComponent.displayName = 'PromptComponent';
export default PromptComponent;