import { Dropdown, IDropdownOption, Stack, TextField } from "@fluentui/react"; import React, { FunctionComponent, useRef, useState } from "react"; import AddIcon from "../../../../images/Add-property.svg"; import DeleteIcon from "../../../../images/delete.svg"; import { NormalizedEventKey } from "../../../Common/Constants"; import { GremlinPropertyValueType, InputPropertyValueTypeString, NewVertexData } from "../../../Contracts/ViewModels"; import { EditorNodePropertiesComponent } from "../GraphExplorerComponent/EditorNodePropertiesComponent"; import "./NewVertexComponent.less"; export interface INewVertexComponentProps { newVertexDataProp: NewVertexData; partitionKeyPropertyProp: string; onChangeProp: (labelData: NewVertexData) => void; } export const NewVertexComponent: FunctionComponent = ({ newVertexDataProp, partitionKeyPropertyProp, onChangeProp, }: INewVertexComponentProps): JSX.Element => { const DEFAULT_PROPERTY_TYPE = "string"; const [newVertexData, setNewVertexData] = useState( newVertexDataProp || { label: "", properties: [ { key: partitionKeyPropertyProp, values: [{ value: "", type: DEFAULT_PROPERTY_TYPE }], }, ], } ); const propertyTypes: string[] = EditorNodePropertiesComponent.VERTEX_PROPERTY_TYPES; const input = useRef(undefined); const onAddNewProperty = () => { addNewVertexProperty(); setTimeout(() => { input.current.focus(); }, 100); }; const onAddNewPropertyKeyPress = (event: React.KeyboardEvent) => { if (event.key === NormalizedEventKey.Space || event.key === NormalizedEventKey.Enter) { onAddNewProperty(); event.stopPropagation(); } }; const addNewVertexProperty = () => { let key: string; const ap = newVertexData.properties; if (ap.length === 0) { key = partitionKeyPropertyProp; } ap.push({ key: key || "", values: [{ value: "", type: DEFAULT_PROPERTY_TYPE }], }); setNewVertexData((prevData) => ({ ...prevData, properties: ap, })); onChangeProp(newVertexData); }; const removeNewVertexProperty = (event?: React.MouseEvent, index?: number) => { const ap = newVertexData.properties; ap.splice(index, 1); setNewVertexData((prevData) => ({ ...prevData, properties: ap, })); onChangeProp(newVertexData); document.getElementById("addProperyNewVertexBtn").focus(); }; const removeNewVertexPropertyKeyPress = (event: React.KeyboardEvent, index: number) => { if (event.key === NormalizedEventKey.Space || event.key === NormalizedEventKey.Enter) { removeNewVertexProperty(undefined, index); event.stopPropagation(); } }; const onLabelChange = (event: React.ChangeEvent) => { setNewVertexData((prevData) => ({ ...prevData, label: event.target.value, })); onChangeProp(newVertexData); }; const onKeyChange = (event: React.ChangeEvent, index: number) => { const newState = { ...newVertexData }; newState.properties[index].key = event.target.value; setNewVertexData(newState); onChangeProp(newVertexData); }; const onValueChange = (event: React.ChangeEvent, index: number) => { const newState = { ...newVertexData }; newState.properties[index].values[0].value = event.target.value as GremlinPropertyValueType; setNewVertexData(newState); onChangeProp(newVertexData); }; const onTypeChange = (option: string, index: number) => { const newState = { ...newVertexData }; if (newState.properties[index]) { newState.properties[index].values[0].type = option as InputPropertyValueTypeString; setNewVertexData(newState); onChangeProp(newVertexData); } }; return (
) => { onLabelChange(event); }} autoFocus />
{newVertexData.properties.map((data, index) => { return (
) => onKeyChange(event, index)} />
) => onValueChange(event, index)} />
({ key: type, text: type, }))} onChange={(_, options: IDropdownOption) => onTypeChange(options.key.toString(), index)} />
) => removeNewVertexProperty(event, index)} onKeyPress={(event: React.KeyboardEvent) => removeNewVertexPropertyKeyPress(event, index) } > Remove property
); })}
) => onAddNewPropertyKeyPress(event)} aria-label="Add property" > Add property Add Property
); };