1
+ import { useBentoConfig } from "../BentoConfigContext" ;
2
+ import { IconButton } from "../IconButton/IconButton" ;
1
3
import { TextField , TextFieldProps } from "../TextField/TextField" ;
4
+ import { useToast } from "../Toast/useToast" ;
5
+ import { LocalizedString } from "../util/ConfigurableTypes" ;
2
6
import { Omit } from "../util/Omit" ;
3
7
4
8
type Props = Omit <
5
9
TextFieldProps ,
6
10
"onChange" | "onBlur" | "disabled" | "isReadOnly" | "placeholder" | "issues"
7
- > ;
11
+ > &
12
+ (
13
+ | {
14
+ withCopyButton : true ;
15
+ copyButtonLabel : LocalizedString ;
16
+ copySuccessMessage : LocalizedString ;
17
+ showToastOnCopy ?: boolean ;
18
+ }
19
+ | {
20
+ withCopyButton ?: false ;
21
+ }
22
+ ) ;
8
23
9
24
const constVoid = ( ) => { } ;
10
25
@@ -13,7 +28,43 @@ const constVoid = () => {};
13
28
* and doesn't require all those props that don't make sense for a read-only field (onChange, onBlur, placeholder, ...)
14
29
*/
15
30
export function ReadOnlyField ( props : Props ) {
16
- return < TextField { ...props } onChange = { constVoid } onBlur = { constVoid } placeholder = "" isReadOnly /> ;
31
+ const { showToast } = useToast ( ) ;
32
+ const config = useBentoConfig ( ) . readOnlyField ;
33
+
34
+ const rightAccessory = props . withCopyButton ? (
35
+ < IconButton
36
+ icon = { config . copyIcon }
37
+ onPress = { async ( ) => {
38
+ if ( props . showToastOnCopy ?? true ) {
39
+ try {
40
+ await navigator . clipboard . writeText ( props . value ) ;
41
+ showToast ( {
42
+ kind : "informative" ,
43
+ message : props . copySuccessMessage ,
44
+ dismissable : true ,
45
+ } ) ;
46
+ } catch {
47
+ console . error ( "Could not copy to clipboard" ) ;
48
+ }
49
+ }
50
+ } }
51
+ kind = "transparent"
52
+ hierarchy = "secondary"
53
+ label = { props . copyButtonLabel }
54
+ size = { config . copyIconSize }
55
+ />
56
+ ) : undefined ;
57
+
58
+ return (
59
+ < TextField
60
+ { ...props }
61
+ onChange = { constVoid }
62
+ onBlur = { constVoid }
63
+ placeholder = ""
64
+ isReadOnly
65
+ rightAccessory = { rightAccessory }
66
+ />
67
+ ) ;
17
68
}
18
69
19
70
export type { Props as ReadOnlyFieldProps } ;
0 commit comments