1- import { createElement } from "./utils" ;
1+ import { applyStyles , createElement } from "./utils" ;
22import { Message } from "../chatHistory" ;
33import { Modal } from "./modal" ;
44
@@ -11,12 +11,19 @@ type ActionButtonConfig = {
1111 modal : Modal ;
1212 completedTextDuration ?: number ;
1313 onClick : ( msg : Message , modal : Modal ) => void | Promise < void > ;
14+ disabled ?: boolean ;
1415}
1516
17+ export type ActionButton = HTMLButtonElement & {
18+ disable ?: ( ) => void ;
19+ enable ?: ( ) => void ;
20+ } ;
21+
1622const defaultConfig : Partial < ActionButtonConfig > = {
1723 loadingText : 'Loading...' ,
1824 completedText : 'Completed!' ,
1925 completedTextDuration : 2000 ,
26+ disabled : false ,
2027} ;
2128
2229const styles = {
@@ -29,7 +36,8 @@ const styles = {
2936 cursor : 'pointer' ,
3037 display : 'flex' ,
3138 alignItems : 'center' ,
32- color : '#4a5568'
39+ color : '#4a5568' ,
40+ opacity : '1' ,
3341 } ,
3442 icon : {
3543 display : 'inline-block' ,
@@ -40,6 +48,10 @@ const styles = {
4048 backgroundRepeat : 'no-repeat' ,
4149 backgroundPosition : 'center'
4250 } ,
51+ disabledButton : {
52+ opacity : '0.5' ,
53+ cursor : 'not-allowed'
54+ } ,
4355} ;
4456
4557const icons = {
@@ -53,21 +65,34 @@ export const createActionButton = (config: ActionButtonConfig) => {
5365 ...config ,
5466 } ;
5567
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+ }
5782
5883 const copyIcon = createElement ( 'span' , {
5984 ...styles . icon ,
6085 backgroundImage : `url("${ icons [ config . icon ] } ")`
6186 } ) ;
62- copyBtn . append ( copyIcon ) ;
87+ button . append ( copyIcon ) ;
6388
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 ;
6792 const result = config . onClick ( config . message , config . modal ) ;
6893
6994 if ( result instanceof Promise ) {
70- copyBtn . innerHTML = `
95+ button . innerHTML = `
7196 <span style="
7297 display: inline-block;
7398 margin-right: 5px;
@@ -134,13 +159,13 @@ export const createActionButton = (config: ActionButtonConfig) => {
134159 document . head . removeChild ( style ) ;
135160 }
136161
137- copyBtn . innerHTML = `
162+ button . innerHTML = `
138163 <span style="margin-right: 5px;">✓</span>
139164 ${ config . completedText }
140165 ` ;
141166 await new Promise ( resolve => setTimeout ( resolve , 2000 ) ) ;
142- copyBtn . innerHTML = originalHTML ;
167+ button . innerHTML = originalHTML ;
143168 } ) ;
144169
145- return copyBtn ;
170+ return button ;
146171}
0 commit comments