Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tag bug #2761

Merged
merged 4 commits into from
Jun 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 30 additions & 37 deletions packages/ui-components/src/FormComponents/Tags.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,43 +33,34 @@ const FORM_EDITOR_TAG_PLUS_ICON = 'elyra-editor-tag-plusIcon';
const FORM_EDITOR_TAG_LIST = 'elyra-editor-tagList';
const FORM_EDITOR_INPUT_TAG = 'elyra-inputTag';

export const Tags: React.FC<ITagProps> = props => {
const [selectedTags, setSelectedTags] = React.useState<string[]>(
props.selectedTags ?? []
export const Tags: React.FC<ITagProps> = ({
selectedTags,
tags,
handleChange
}) => {
const [selected, setSelectedTags] = React.useState<string[]>(
selectedTags ?? []
);
const [tags, setTags] = React.useState<string[]>(props.tags ?? []);
const [allTags, setTags] = React.useState<string[]>(tags ?? []);
const [addingNewTag, setAddingNewTag] = React.useState<boolean>(false);

React.useEffect(() => {
handleChange(selected, allTags);
}, [selected, allTags, handleChange]);

const handleClick = (
event: React.MouseEvent<HTMLButtonElement, MouseEvent>
): void => {
const target = event.target as HTMLElement;
const target = event.currentTarget as HTMLElement;
const clickedTag = target.innerText;

setSelectedTags(updateTagsCss(target, selectedTags ?? [], clickedTag));
handleOnChange();
};

const handleOnChange = (): void => {
props.handleChange(selectedTags, tags);
};

const updateTagsCss = (
target: HTMLElement,
tags: string[],
clickedTag: string
): string[] => {
const currentTags = tags.slice();
if (target.classList.contains('unapplied-tag')) {
target.classList.replace('unapplied-tag', 'applied-tag');
currentTags.splice(-1, 0, clickedTag);
} else if (target.classList.contains('applied-tag')) {
target.classList.replace('applied-tag', 'unapplied-tag');

const idx = currentTags.indexOf(clickedTag);
currentTags.splice(idx, 1);
const updatedTags: string[] = Object.assign([], selected);
const tagIndex = selected.indexOf(clickedTag);
if (tagIndex === -1) {
updatedTags.push(clickedTag);
} else {
updatedTags.splice(tagIndex, 1);
}
return currentTags;
setSelectedTags(updatedTags);
};

const addTagOnClick = (event: React.MouseEvent<HTMLInputElement>): void => {
Expand All @@ -87,8 +78,8 @@ export const Tags: React.FC<ITagProps> = props => {
): Promise<void> => {
const inputElement = event.target as HTMLInputElement;

if (inputElement.value !== '' && event.keyCode === 13) {
if (tags.includes(inputElement.value)) {
if (inputElement.value.trim() !== '' && event.keyCode === 13) {
if (allTags.includes(inputElement.value)) {
event.preventDefault();
await showDialog({
title: 'A tag with this label already exists.',
Expand All @@ -100,10 +91,12 @@ export const Tags: React.FC<ITagProps> = props => {
const newTag = inputElement.value;

// update state all tag and selected tag
setSelectedTags([...selectedTags, newTag]);
setTags([...tags, newTag]);
setSelectedTags([...selected, newTag]);
setTags([...allTags, newTag]);
setAddingNewTag(false);
} else if (event.keyCode === 13) {
event.preventDefault();
setAddingNewTag(false);
handleOnChange();
}
};

Expand Down Expand Up @@ -159,9 +152,9 @@ export const Tags: React.FC<ITagProps> = props => {
return (
<li className={FORM_EDITOR_TAG_LIST}>
{hasTags
? tags.map((tag: string, index: number) =>
? allTags.map((tag: string, index: number) =>
((): JSX.Element => {
if (!selectedTags) {
if (!selected) {
return (
<button
onClick={handleClick}
Expand All @@ -174,7 +167,7 @@ export const Tags: React.FC<ITagProps> = props => {
);
}

if (selectedTags.includes(tag)) {
if (selected.includes(tag)) {
return (
<button
onClick={handleClick}
Expand Down
7 changes: 1 addition & 6 deletions packages/ui-components/src/FormEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ export const FormEditor: React.FC<IFormEditorProps> = ({
languageOptions
}) => {
const [formData, setFormData] = React.useState(originalData ?? ({} as any));
const [tags, setTags] = React.useState(allTags);

/**
* Generate the rjsf uiSchema from uihints in the elyra metadata schema.
Expand All @@ -172,10 +171,7 @@ export const FormEditor: React.FC<IFormEditorProps> = ({
formContext={{
editorServices: editorServices,
language: formData?.['Source']?.language ?? '',
allTags: tags,
updateAllTags: (updatedTags: string[]): void => {
setTags(updatedTags);
},
allTags: allTags,
languageOptions: languageOptions,
trans: translator
}}
Expand All @@ -185,7 +181,6 @@ export const FormEditor: React.FC<IFormEditorProps> = ({
uiSchema={uiSchema}
onChange={(e: IChangeEvent<any>): void => {
setFormData(e.formData);
console.log(e.formData);
onChange(e.formData, e.errors.length > 0 || false);
}}
liveValidate={true}
Expand Down
4 changes: 4 additions & 0 deletions packages/ui-components/style/formeditor.css
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
margin-top: 8px;
}

.elyra-formEditor-form-tags input {
margin: 0;
}

.rjsf > .form-group.field,
.form-group.field.tagsField,
.form-group.field.languageField {
Expand Down