Skip to content

Commit

Permalink
Prevent undefined inputs; delete entry when deleting node
Browse files Browse the repository at this point in the history
Signed-off-by: Tyler Ohlsen <[email protected]>
  • Loading branch information
ohltyler committed Oct 17, 2023
1 parent 54f65f1 commit 541a2f8
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ interface JsonFieldProps {
* An input field for a component where users manually enter
* in some custom JSON
*/
// TODO: integrate with formik
export function JsonField(props: JsonFieldProps) {
return (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
EuiText,
} from '@elastic/eui';
import { Field, FieldProps } from 'formik';
import { IComponentField } from '../../../../../common';
import { IComponentField, getInitialValue } from '../../../../../common';

// TODO: Should be fetched from global state.
// Need to have a way to determine where to fetch this dynamic data.
Expand Down Expand Up @@ -50,7 +50,7 @@ export function SelectField(props: SelectFieldProps) {
<EuiFormRow label={props.field.label}>
<EuiSuperSelect
options={options}
valueOfSelected={field.value}
valueOfSelected={field.value || getInitialValue(props.field.type)}
onChange={(option) => {
field.onChange(option);
form.setFieldValue(formField, option);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import React from 'react';
import { Field, FieldProps } from 'formik';
import { EuiFieldText, EuiFormRow } from '@elastic/eui';
import { IComponentField } from '../../../../../common';
import { IComponentField, getInitialValue } from '../../../../../common';

interface TextFieldProps {
field: IComponentField;
Expand All @@ -23,9 +23,10 @@ export function TextField(props: TextFieldProps) {
return (
<EuiFormRow label={props.field.label}>
<EuiFieldText
{...field}
placeholder={props.field.placeholder || ''}
compressed={false}
{...field}
value={field.value || getInitialValue(props.field.type)}
/>
</EuiFormRow>
);
Expand Down
4 changes: 2 additions & 2 deletions public/pages/workflow_detail/workspace/workspace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ export function Workspace(props: WorkspaceProps) {
},
};

// TODO: on node addition, need to update the formik initial state to include this
// new component.
// TODO: on node addition, need to update the formik values to include
// a new property with this id.
setNodes((nds) => nds.concat(newNode));
dispatch(setDirty());
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

import React, { useContext } from 'react';
import { useFormikContext } from 'formik';
import {
EuiFlexGroup,
EuiFlexItem,
Expand All @@ -13,7 +14,7 @@ import {
EuiButtonIcon,
} from '@elastic/eui';
import { rfContext } from '../../../store';
import { IComponentData } from '../../../component_types';
import { IComponentData, WorkspaceFormValues } from '../../../component_types';
import { InputHandle } from './input_handle';
import { OutputHandle } from './output_handle';

Expand All @@ -29,6 +30,7 @@ interface WorkspaceComponentProps {
export function WorkspaceComponent(props: WorkspaceComponentProps) {
const component = props.data;
const { deleteNode } = useContext(rfContext);
const { values } = useFormikContext<WorkspaceFormValues>();

return (
<EuiCard
Expand All @@ -45,8 +47,7 @@ export function WorkspaceComponent(props: WorkspaceComponentProps) {
iconType="trash"
onClick={() => {
deleteNode(component.id);
// TODO: update the form to remove this node based on node ID.
// Can fetch & update the values using useFormikContext().
delete values[`${component.id}`];
}}
aria-label="Delete"
/>
Expand Down
2 changes: 1 addition & 1 deletion public/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export function formikToComponentData(
}

// Helper fn to get an initial value based on the field type
function getInitialValue(fieldType: FieldType): FieldValue {
export function getInitialValue(fieldType: FieldType): FieldValue {
switch (fieldType) {
case 'string': {
return '';
Expand Down

0 comments on commit 541a2f8

Please sign in to comment.