Skip to content

Commit 773845c

Browse files
authored
Merge pull request #235 from sf1tzp/click-to-edit
Allow editing `tag:value` in TagSelector
2 parents 4db738f + 442acc1 commit 773845c

3 files changed

Lines changed: 24 additions & 12 deletions

File tree

ui/src/common/TagChip.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const useStyles = makeStyles((theme: Theme) => ({
1818
interface TagChipProps {
1919
label: string;
2020
color: string;
21-
onClick?: () => void;
21+
onClick?: (e: React.MouseEvent) => void;
2222
}
2323

2424
export const TagChip: React.FC<TagChipProps> = ({color, label, onClick}) => {

ui/src/dashboard/Entry/DashboardEntryForm.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@ export const DashboardEntryForm: React.FC<EditPopupProps> = ({entry, onChange: s
206206
}}
207207
createTags={false}
208208
onlySelectKeys
209-
removeWhenClicked
210209
/>
211210

212211
<FormTagSelector

ui/src/tag/TagSelector.tsx

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ export interface TagSelectorProps {
3737
createTags?: boolean;
3838
allowDuplicateKeys?: boolean;
3939
onlySelectKeys?: boolean;
40-
removeWhenClicked?: boolean;
4140
}
4241

4342
export const TagSelector: React.FC<TagSelectorProps> = ({
@@ -48,7 +47,6 @@ export const TagSelector: React.FC<TagSelectorProps> = ({
4847
createTags = true,
4948
allowDuplicateKeys = false,
5049
onlySelectKeys = false,
51-
removeWhenClicked = false,
5250
}) => {
5351
const classes = useStyles();
5452
const [tooltipErrorActive, tooltipError, showTooltipError] = useError(4000);
@@ -127,14 +125,21 @@ export const TagSelector: React.FC<TagSelectorProps> = ({
127125
return;
128126
};
129127

130-
const onTagClicked = (entry: TagSelectorEntry) => {
131-
if (!removeWhenClicked) {
128+
const onTagClicked = (entry: TagSelectorEntry, edit: boolean) => {
129+
// Prevent overwriting text that's already being edited
130+
if (currentValue && edit) {
131+
showTooltipError('Input is not empty. Use ctrl+click to delete tag without editing.');
132132
return;
133133
}
134-
const tagIndex = selectedEntries.indexOf(entry);
135-
selectedEntries.splice(tagIndex, 1);
136134

137-
setSelectedEntries(selectedEntries);
135+
setSelectedEntries(selectedEntries.filter((selected) => selected !== entry));
136+
137+
if (edit) {
138+
setCurrentValueInternal(itemLabel(entry, onlySelectKeys));
139+
setOpen(true);
140+
}
141+
142+
focusInput();
138143
};
139144

140145
const onKeyDown = (event: React.KeyboardEvent) => {
@@ -177,6 +182,7 @@ export const TagSelector: React.FC<TagSelectorProps> = ({
177182
disableHoverListener
178183
disableTouchListener
179184
open={tooltipErrorActive}
185+
PopperProps={{style: {zIndex: 100000}}}
180186
placement={'top'}
181187
title={
182188
<Typography color="inherit" style={{whiteSpace: 'pre-line'}}>
@@ -217,7 +223,10 @@ export const TagSelector: React.FC<TagSelectorProps> = ({
217223
) : null}
218224
{addDialogOpen && (
219225
<AddTagDialog
220-
onAdded={(tag) => trySubmit({tag, value: ''})}
226+
onAdded={(tag) => {
227+
const valuePart = currentValue.split(':')[1] || '';
228+
trySubmit({tag, value: valuePart});
229+
}}
221230
open={true}
222231
initialName={currentValue.split(':')[0]}
223232
close={() => {
@@ -254,13 +263,17 @@ const Item: React.FC<ItemProps> = ({entry, selected, onlySelectKeys, onClick}) =
254263
);
255264
};
256265

257-
const toChips = (entries: TagSelectorEntry[], onlySelectKeys: boolean, onClick: (entry: TagSelectorEntry) => void) => {
266+
const toChips = (
267+
entries: TagSelectorEntry[],
268+
onlySelectKeys: boolean,
269+
onClick: (entry: TagSelectorEntry, edit: boolean) => void
270+
) => {
258271
return entries.map((entry) => (
259272
<TagChip
260273
key={itemLabel(entry, onlySelectKeys)}
261274
label={itemLabel(entry, onlySelectKeys)}
262275
color={entry.tag.color}
263-
onClick={() => onClick(entry)}
276+
onClick={(e) => onClick(entry, !e.ctrlKey)}
264277
/>
265278
));
266279
};

0 commit comments

Comments
 (0)