-
Notifications
You must be signed in to change notification settings - Fork 854
Expand file tree
/
Copy pathindex.jsx
More file actions
96 lines (90 loc) · 2.6 KB
/
index.jsx
File metadata and controls
96 lines (90 loc) · 2.6 KB
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
import { useEffect, useRef, useState } from 'react'
import PropTypes from 'prop-types'
import { useTranslation } from 'react-i18next'
import { TrashIcon } from '@primer/octicons-react'
DeleteButton.propTypes = {
onConfirm: PropTypes.func.isRequired,
size: PropTypes.number.isRequired,
text: PropTypes.string.isRequired,
}
function DeleteButton({ onConfirm, size, text }) {
const { t } = useTranslation()
const [waitConfirm, setWaitConfirm] = useState(false)
const confirmRef = useRef(null)
const [confirming, setConfirming] = useState(false)
const isMountedRef = useRef(true)
useEffect(() => {
isMountedRef.current = true
return () => {
isMountedRef.current = false
}
}, [])
useEffect(() => {
if (waitConfirm) confirmRef.current.focus()
}, [waitConfirm])
return (
<span>
<button
ref={confirmRef}
type="button"
className="normal-button"
style={{
fontSize: '10px',
...(waitConfirm ? {} : { display: 'none' }),
}}
disabled={confirming}
aria-busy={confirming ? 'true' : 'false'}
aria-hidden={waitConfirm ? undefined : 'true'}
tabIndex={waitConfirm ? 0 : -1}
onMouseDown={(e) => {
e.preventDefault()
e.stopPropagation()
}}
onBlur={() => {
if (!confirming && isMountedRef.current) setWaitConfirm(false)
}}
onClick={async (e) => {
if (confirming) return
e.preventDefault()
e.stopPropagation()
setConfirming(true)
try {
await onConfirm()
if (isMountedRef.current) setWaitConfirm(false)
} catch (err) {
// Keep confirmation visible to allow retry; optionally log
// eslint-disable-next-line no-console
console.error(err)
} finally {
if (isMountedRef.current) setConfirming(false)
}
}}
>
{t('Confirm')}
</button>
<span
title={text}
className="gpt-util-icon"
role="button"
tabIndex={0}
aria-label={text}
aria-hidden={waitConfirm ? 'true' : undefined}
style={waitConfirm ? { visibility: 'hidden' } : {}}
onKeyDown={(e) => {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault()
e.stopPropagation()
setWaitConfirm(true)
}
}}
onClick={(e) => {
e.stopPropagation()
setWaitConfirm(true)
}}
>
<TrashIcon size={size} />
</span>
</span>
)
}
export default DeleteButton