1
- import { createElement } from "./utils" ;
1
+ import { applyStyles , createElement } from "./utils" ;
2
2
import { Message } from "../chatHistory" ;
3
3
import { Modal } from "./modal" ;
4
4
@@ -11,12 +11,19 @@ type ActionButtonConfig = {
11
11
modal : Modal ;
12
12
completedTextDuration ?: number ;
13
13
onClick : ( msg : Message , modal : Modal ) => void | Promise < void > ;
14
+ disabled ?: boolean ;
14
15
}
15
16
17
+ export type ActionButton = HTMLButtonElement & {
18
+ disable ?: ( ) => void ;
19
+ enable ?: ( ) => void ;
20
+ } ;
21
+
16
22
const defaultConfig : Partial < ActionButtonConfig > = {
17
23
loadingText : 'Loading...' ,
18
24
completedText : 'Completed!' ,
19
25
completedTextDuration : 2000 ,
26
+ disabled : false ,
20
27
} ;
21
28
22
29
const styles = {
@@ -29,7 +36,8 @@ const styles = {
29
36
cursor : 'pointer' ,
30
37
display : 'flex' ,
31
38
alignItems : 'center' ,
32
- color : '#4a5568'
39
+ color : '#4a5568' ,
40
+ opacity : '1' ,
33
41
} ,
34
42
icon : {
35
43
display : 'inline-block' ,
@@ -40,6 +48,10 @@ const styles = {
40
48
backgroundRepeat : 'no-repeat' ,
41
49
backgroundPosition : 'center'
42
50
} ,
51
+ disabledButton : {
52
+ opacity : '0.5' ,
53
+ cursor : 'not-allowed'
54
+ } ,
43
55
} ;
44
56
45
57
const icons = {
@@ -53,21 +65,34 @@ export const createActionButton = (config: ActionButtonConfig) => {
53
65
...config ,
54
66
} ;
55
67
56
- const copyBtn = createElement ( 'button' , styles . actionButton ) ;
68
+ const button = createElement ( 'button' , styles . actionButton ) as ActionButton ;
69
+ button . className = 'action-button' ;
70
+ button . enable = ( ) => {
71
+ button . disabled = false ;
72
+ applyStyles ( button , styles . actionButton ) ;
73
+ }
74
+ button . disable = ( ) => {
75
+ button . disabled = true ;
76
+ applyStyles ( button , { ...styles . actionButton , ...styles . disabledButton } ) ;
77
+ }
78
+
79
+ if ( config . disabled ) {
80
+ button . disable ( ) ;
81
+ }
57
82
58
83
const copyIcon = createElement ( 'span' , {
59
84
...styles . icon ,
60
85
backgroundImage : `url("${ icons [ config . icon ] } ")`
61
86
} ) ;
62
- copyBtn . append ( copyIcon ) ;
87
+ button . append ( copyIcon ) ;
63
88
64
- copyBtn . append ( document . createTextNode ( config . label ) ) ;
65
- copyBtn . addEventListener ( 'click' , async ( ) => {
66
- const originalHTML = copyBtn . innerHTML ;
89
+ button . append ( document . createTextNode ( config . label ) ) ;
90
+ button . addEventListener ( 'click' , async ( ) => {
91
+ const originalHTML = button . innerHTML ;
67
92
const result = config . onClick ( config . message , config . modal ) ;
68
93
69
94
if ( result instanceof Promise ) {
70
- copyBtn . innerHTML = `
95
+ button . innerHTML = `
71
96
<span style="
72
97
display: inline-block;
73
98
margin-right: 5px;
@@ -134,13 +159,13 @@ export const createActionButton = (config: ActionButtonConfig) => {
134
159
document . head . removeChild ( style ) ;
135
160
}
136
161
137
- copyBtn . innerHTML = `
162
+ button . innerHTML = `
138
163
<span style="margin-right: 5px;">✓</span>
139
164
${ config . completedText }
140
165
` ;
141
166
await new Promise ( resolve => setTimeout ( resolve , 2000 ) ) ;
142
- copyBtn . innerHTML = originalHTML ;
167
+ button . innerHTML = originalHTML ;
143
168
} ) ;
144
169
145
- return copyBtn ;
170
+ return button ;
146
171
}
0 commit comments