forked from deephaven/web-client-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIcons.tsx
More file actions
159 lines (147 loc) · 4.56 KB
/
Copy pathIcons.tsx
File metadata and controls
159 lines (147 loc) · 4.56 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
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
import React, { useState, useEffect } from 'react';
import { dh, IconDefinition } from '@deephaven/icons';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { Button } from '@deephaven/components';
import PropTypes from 'prop-types';
async function copyText(text: string): Promise<void | ErrorConstructor> {
try {
return await navigator.clipboard.writeText(text);
} catch (error) {
throw new Error(`Unable to copy: ${error}`);
}
}
const DH_PREFIX = 'dh';
const VS_PREFIX = 'vs';
// prefix, icon-name to prefixIconName
const getPrefixedName = (name: string, prefix: string) =>
prefix.toLowerCase() +
name
.split('-')
.map(str => str.charAt(0).toUpperCase() + str.slice(1))
.join('');
interface FlashProps {
message: { text: React.ReactNode };
}
const Flash = ({ message, message: { text } }: FlashProps) => {
const [show, setShow] = useState(false);
useEffect(
function setFlashMessage() {
if (text == null || text === '') return;
setShow(true);
const timeout = setTimeout(() => {
setShow(false);
}, 2000);
// eslint-disable-next-line consistent-return
return () => {
clearTimeout(timeout);
};
},
[message, text]
);
return <p className={show ? 'flash in' : 'flash out'}>{text}</p>;
};
Flash.propTypes = {
message: PropTypes.shape({
text: PropTypes.node,
}).isRequired,
};
const Icons = (): React.ReactElement => {
const [dhFilter, setDhFilter] = useState<boolean>(true);
const [vsFilter, setVsFilter] = useState<boolean>(true);
const [search, setSearch] = useState<string>('');
const [flashText, setFlashText] = useState<{ text: React.ReactNode }>({
text: '',
});
const renderIcons = Object.values(dh)
.filter((icon: IconDefinition): boolean => {
const matchesFilter =
(icon.prefix.toLowerCase() + icon.iconName.toLowerCase()).indexOf(
search.toLowerCase()
) !== -1;
const isDH = dhFilter && (icon.prefix as string) === DH_PREFIX;
const isVS = vsFilter && (icon.prefix as string) === VS_PREFIX;
return matchesFilter && (isDH || isVS);
})
.map(icon => {
const prefixedName = getPrefixedName(icon.iconName, icon.prefix);
return (
<Button
key={prefixedName}
kind="inline"
className="card"
onClick={() => {
// new object, so it always flashes even on same string
copyText(prefixedName)
.then(() => {
setFlashText({
text: (
<span>
<FontAwesomeIcon icon={dh.vsOutput} /> Copied text:{' '}
<strong>{prefixedName}</strong>
</span>
),
});
})
.catch(err => {
setFlashText({
text: <span className="text-danger">{err.message}</span>,
});
});
}}
>
<FontAwesomeIcon icon={icon} className="icon" />
<label title={prefixedName}>{prefixedName}</label>
</Button>
);
});
return (
<div>
<h2 className="ui-title">Icons</h2>
<div className="row">
<div className="col">
<div className="form-inline mb-3">
<input
type="search"
placeholder="Basic icon search..."
value={search}
className="form-control"
onChange={event => {
setSearch(event.target.value);
}}
/>
<span className="mx-2">Show: </span>
<Button
kind="inline"
active={vsFilter}
className="mr-2"
onClick={() => setVsFilter(!vsFilter)}
icon={vsFilter ? dh.vsCheck : dh.vsClose}
>
VS ICONS
</Button>
<Button
kind="inline"
active={dhFilter}
className="mr-2"
onClick={() => setDhFilter(!dhFilter)}
icon={dhFilter ? dh.vsCheck : dh.vsClose}
>
DH ICONS
</Button>
<small>
({renderIcons.length} icon{renderIcons.length === 1 ? '' : 's'})
</small>
</div>
<div className="icons">
{renderIcons}
{renderIcons.length === 0 && (
<p className="no-result">No icons found.</p>
)}
<Flash message={flashText} />
</div>
</div>
</div>
</div>
);
};
export default Icons;