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

Move Formik fields and form helpers from @console/dev-console to @console/shared #3516

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as ResourceDropdown } from './ResourceDropdown';
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import { FormFooterProps } from './form-utils-types';
const FormFooter: React.FC<FormFooterProps> = ({
handleSubmit,
handleReset,
handleCancel,
submitLabel = 'Save',
resetLabel = 'Reload',
cancelLabel = 'Cancel',
isSubmitting,
errorMessage,
successMessage,
Expand All @@ -15,7 +19,7 @@ const FormFooter: React.FC<FormFooterProps> = ({
<ButtonBar inProgress={isSubmitting} errorMessage={errorMessage} successMessage={successMessage}>
{showAlert && (
<Alert isInline className="co-alert" variant="info" title="You made changes to this page.">
Click Save to save changes or Reload to cancel.
{`Click ${submitLabel} to save changes or ${resetLabel} to cancel changes.`}
</Alert>
)}
<ActionGroup className="pf-c-form">
Expand All @@ -25,11 +29,18 @@ const FormFooter: React.FC<FormFooterProps> = ({
variant={ButtonVariant.primary}
isDisabled={disableSubmit}
>
Save
</Button>
<Button type="button" variant={ButtonVariant.secondary} onClick={handleReset}>
Reload
{submitLabel}
</Button>
{handleReset && (
<Button type="button" variant={ButtonVariant.secondary} onClick={handleReset}>
{resetLabel}
</Button>
)}
{handleCancel && (
<Button type="button" variant={ButtonVariant.secondary} onClick={handleCancel}>
{cancelLabel}
</Button>
)}
</ActionGroup>
</ButtonBar>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
export interface FormFooterProps {
handleSubmit?: () => void;
handleReset: () => void;
handleReset?: () => void;
handleCancel?: () => void;
submitLabel?: string;
resetLabel?: string;
cancelLabel?: string;
isSubmitting: boolean;
errorMessage: string;
successMessage: string;
successMessage?: string;
disableSubmit: boolean;
showAlert?: boolean;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/* eslint-disable no-unused-vars, no-undef */
import * as React from 'react';
import { useField } from 'formik';
import { FormGroup, Switch } from '@patternfly/react-core';
import { getFieldId } from '@console/dev-console/src/components/formik-fields/field-utils';
import { CheckboxFieldProps } from '@console/dev-console/src/components/formik-fields/field-types';
import { getFieldId } from './field-utils';
import { CheckboxFieldProps } from './field-types';

const SwitchField: React.FC<CheckboxFieldProps> = ({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yikes, had no idea SwitchField was so similar to CheckboxField... we need to deduplicate this code using some sort of HOC or something. The logic of both of these components is near identical.

@christianvogt @rohitkrai03 I recommend we look to clean this up after this PR as to keep everything from crossing wires.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, this didn't come from dev-console. I just assumed all fields were in our package. Either way, this should probably be cleaned up, it's near identical code.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andrewballantyne I think we can merge both of these components and control the component it renders internally using a prop. Something like

<CheckboxField as="switch" {...prop} />

This is inline with how formik handles rendering form fields with different components. We could make checkbox as default variant as well.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like keeping SwitchField as a separate component as it is easier for devs to know it exists. How about this? jtomasek@9f2a976
I've used render props pattern and refactored common code into CheckBoxFieldBase component. It would be IMHO better to keep that change in a separate PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely agree, it should be another PR, this one should track the refactor (it's a big enough change to be done on it's own).

FWIW, I lean the render prop way that @jtomasek suggested. I had envisioned more of a function composition where the Component would be passed in and invoked with JSX inside a nested function -- but it seems our hook lint doesn't like that idea. The render prop is a decent alternative imo.

label,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
export { default as InputField } from './InputField';
export { default as TextAreaField } from './TextAreaField';
export { default as DropdownField } from './DropdownField';
export { default as NSDropdownField } from './NSDropdownField';
export { default as CheckboxField } from './CheckboxField';
export { default as NumberSpinnerField } from './NumberSpinnerField';
export { default as EnvironmentField } from './EnvironmentField';
export { default as DropdownField } from './DropdownField';
export { default as DroppableFileInputField } from './DroppableFileInputField';
export { default as EnvironmentField } from './EnvironmentField';
export { default as InputField } from './InputField';
export { default as InputSearchField } from './InputSearchField';
export { default as MultiColumnField } from './multi-column-field/MultiColumnField';
export { default as ResourceLimitField } from './ResourceLimitField';
export { default as NSDropdownField } from './NSDropdownField';
export { default as NumberSpinnerField } from './NumberSpinnerField';
export { default as RadioButtonField } from './RadioButtonField';
export { default as ResourceDropdownField } from './ResourceDropdownField';
export { default as ResourceLimitField } from './ResourceLimitField';
export { default as SwitchField } from './SwitchField';
export { default as TextAreaField } from './TextAreaField';
export * from './field-utils';
export * from './field-types';
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am slightly concerned if we really want to re-export these. It allows to use '@console/shared' for imports but we could hit naming collisions/confusion as the @console/shared package grows. Should we re-export everything which is used outside the shared package? Should we re-export only selected components and utils? Should we split shared package into multiple packages (e.g. @console/shared-forms) to avoid potential naming conflicts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could definitely create more packages for isolated use, but we need to be very clear with who is dependant on who otherwise we run the risk of circular references. I wonder if we could just export at a deeper level... something like @console/shared/src/formik or something...

@christianvogt @spadgett thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we circle back to this - you may have a growing issue with rebases as time goes on. Recommend we get this in and circle back to address the export.

3 changes: 3 additions & 0 deletions frontend/packages/console-shared/src/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
export * from './badges';
export * from './contextMenu';
export * from './dropdown';
export * from './form-utils';
export * from './formik-fields';
export * from './status';
export * from './pod';
export * from './popper';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';
import { Firehose } from '@console/internal/components/utils';
import { referenceForModel } from '@console/internal/module/k8s';
import { ServiceModel } from '@console/knative-plugin';
import ResourceDropdown from './ResourceDropdown';
import { ResourceDropdown } from '@console/shared';

interface ApplicationDropdownProps {
id?: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as React from 'react';
import * as _ from 'lodash';
import { ResourceDropdown } from '@console/shared';
import { Firehose } from '@console/internal/components/utils';
import { referenceForModel } from '@console/internal/module/k8s';
import { PipelineResourceModel } from '../../models';
import ResourceDropdown from './ResourceDropdown';

export interface PipelineResourceDropdownProps {
dropDownClassName?: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import { SecretModel } from '@console/internal/models';
import { Firehose } from '@console/internal/components/utils';
import ResourceDropdown from './ResourceDropdown';
import { ResourceDropdown } from '@console/shared';

interface SourceSecretDropdownProps {
dropDownClassName?: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { CheckboxField, EnvironmentField } from '../../formik-fields';
import { CheckboxField, EnvironmentField } from '@console/shared';
import FormSection from '../section/FormSection';

export interface BuildConfigSectionProps {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { CheckboxField, EnvironmentField } from '../../formik-fields';
import { CheckboxField, EnvironmentField } from '@console/shared';
import FormSection from '../section/FormSection';

export interface DeploymentConfigSectionProps {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import { ResourceLimitField } from '@console/shared';
import FormSection from '../section/FormSection';
import { ResourceLimitField } from '../../formik-fields';
import { MemoryUnits, CPUUnits } from '../import-types';

const ResourceLimitSection: React.FC = () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { NumberSpinnerField } from '../../formik-fields';
import { NumberSpinnerField } from '@console/shared';
import FormSection from '../section/FormSection';

const ScalingSection: React.FC = () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { NumberSpinnerField } from '../../formik-fields';
import { NumberSpinnerField } from '@console/shared';
import FormSection from '../section/FormSection';

const ServerlessScalingSection: React.FC = () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import { useField } from 'formik';
import { TextInputTypes } from '@patternfly/react-core';
import { InputField, TextAreaField } from '../../formik-fields';
import { InputField, TextAreaField } from '@console/shared';
import { ProjectData } from '../import-types';
import FormSection from '../section/FormSection';
import ApplicationSelector from './ApplicationSelector';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import * as React from 'react';
import * as _ from 'lodash';
import { useFormikContext, FormikValues, useField } from 'formik';
import { FormGroup, TextInputTypes } from '@patternfly/react-core';
import { InputField } from '../../formik-fields';
import { InputField, getFieldId } from '@console/shared';
import ApplicationDropdown from '../../dropdown/ApplicationDropdown';
import { getFieldId } from '../../formik-fields/field-utils';

export const CREATE_APPLICATION_KEY = '#CREATE_APPLICATION_KEY#';
export const UNASSIGNED_KEY = '#UNASSIGNED_KEY#';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { useField, useFormikContext, FormikValues } from 'formik';
import { LoadingInline } from '@console/internal/components/utils';
import { FormGroup, Alert } from '@patternfly/react-core';
import { StarIcon } from '@patternfly/react-icons';
import { getFieldId } from '@console/shared';
import { NormalizedBuilderImages } from '../../../utils/imagestream-utils';
import { getFieldId } from '../../formik-fields/field-utils';
import BuilderImageCard from './BuilderImageCard';
import './BuilderImageSelector.scss';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { useFormikContext, FormikValues } from 'formik';
import { ResourceName } from '@console/internal/components/utils';
import { K8sResourceKind } from '@console/internal/module/k8s';
import { ImageStreamTagModel } from '@console/internal/models';
import { DropdownField } from '@console/shared';
import {
BuilderImage,
getTagDataWithDisplayName,
getPorts,
} from '../../../utils/imagestream-utils';
import { DropdownField } from '../../formik-fields';
import { useSafeK8s } from '../../../utils/safe-k8s-hook';
import ImageStreamInfo from './ImageStreamInfo';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import { ExpandCollapse } from '@console/internal/components/utils';
import { TextInputTypes } from '@patternfly/react-core';
import { InputField } from '../../formik-fields';
import { InputField } from '@console/shared';
import FormSection from '../section/FormSection';
import SourceSecretSelector from './SourceSecretSelector';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import { TextInputTypes } from '@patternfly/react-core';
import { InputField } from '../../formik-fields';
import { InputField } from '@console/shared';
import FormSection from '../section/FormSection';

export interface DockerSectionProps {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Alert, TextInputTypes } from '@patternfly/react-core';
import { getGitService, GitProvider } from '@console/git-service';
import { LoadingInline } from '@console/internal/components/utils';
import { CheckCircleIcon } from '@patternfly/react-icons';
import { InputField, DropdownField } from '../../formik-fields';
import { InputField, DropdownField } from '@console/shared';
import { GitReadableTypes, GitTypes } from '../import-types';
import { detectGitType, detectGitRepoName } from '../import-validation-utils';
import { getSampleRepo, getSampleRef, getSampleContextDir } from '../../../utils/imagestream-utils';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import * as React from 'react';
import { useFormikContext, FormikValues, useField } from 'formik';
import { FormGroup } from '@patternfly/react-core';
import { SecretTypeAbstraction } from '@console/internal/components/secrets/create-secret';
import { getFieldId } from '@console/shared';
import SourceSecretDropdown from '../../dropdown/SourceSecretDropdown';
import { getFieldId } from '../../formik-fields/field-utils';
import { secretModalLauncher } from '../CreateSecretModal';

const CREATE_SOURCE_SECRET = 'create-source-secret';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { ImageStreamImportsModel } from '@console/internal/models';
import { useFormikContext, FormikValues } from 'formik';
import { TextInputTypes, Alert, AlertActionCloseButton, Button } from '@patternfly/react-core';
import { SecretTypeAbstraction } from '@console/internal/components/secrets/create-secret';
import { InputSearchField } from '@console/shared';
import { getSuggestedName, getPorts, makePortName } from '../../../utils/imagestream-utils';
import { InputSearchField } from '../../formik-fields';
import { secretModalLauncher } from '../CreateSecretModal';

const ImageSearch: React.FC = () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import { useFormikContext, FormikValues } from 'formik';
import { K8sResourceKind } from '@console/internal/module/k8s';
import { RadioButtonField } from '../../formik-fields';
import { RadioButtonField } from '@console/shared';
import FormSection from '../section/FormSection';
import { imageRegistryType } from '../../../utils/imagestream-utils';
import ImageStream from './ImageStream';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useFormikContext, FormikValues } from 'formik';
import { k8sGet } from '@console/internal/module/k8s';
import { ImageStreamTagModel, RoleBindingModel } from '@console/internal/models';
import { useAccessReview } from '@console/internal/components/utils';
import { DropdownField, CheckboxField } from '../../formik-fields';
import { DropdownField, ResourceDropdownField, CheckboxField } from '@console/shared';
import { ImageStreamProps } from '../import-types';
import {
getSuggestedName,
Expand All @@ -18,7 +18,6 @@ import {
getImageStreamTags,
} from '../../../utils/imagestream-utils';
import './ImageStream.scss';
import ResourceDropdownField from '../../formik-fields/ResourceDropdownField';

const ImageStream: React.FC<ImageStreamProps> = ({ imageStreams }) => {
const resources = getProjectResource();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { K8sResourceKind, ContainerPort } from '@console/internal/module/k8s';
import { LazyLoader } from '@console/plugin-sdk';
import { NameValuePair, NameValueFromPair } from '../formik-fields/field-types';
import { NameValuePair, NameValueFromPair } from '@console/shared';
import { NormalizedBuilderImages } from '../../utils/imagestream-utils';

export interface DeployImageFormProps {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { LoadingInline } from '@console/internal/components/utils';
import { k8sList } from '@console/internal/module/k8s';
import { useFormikContext, FormikValues } from 'formik';
import { Alert, Expandable } from '@patternfly/react-core';
import { CheckboxField } from '@console/shared';
import { PipelineModel } from '../../../models';
import { CheckboxField } from '../../formik-fields';
import { PipelineVisualization } from '../../pipelines/detail-page-tabs/pipeline-details/PipelineVisualization';

const MISSING_DOCKERFILE_LABEL_TEXT =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';
import * as _ from 'lodash';
import { useFormikContext, FormikValues } from 'formik';
import { TextInputTypes } from '@patternfly/react-core';
import { InputField, DropdownField } from '../../formik-fields';
import { InputField, DropdownField } from '@console/shared';
import { makePortName } from '../../../utils/imagestream-utils';

const CreateRoute: React.FC = () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { CheckboxField } from '../../formik-fields';
import { CheckboxField } from '@console/shared';

const RouteCheckbox: React.FC = () => {
return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import { useFormikContext, FormikValues } from 'formik';
import { FormHelperText } from '@patternfly/react-core';
import { DropdownField, DroppableFileInputField, CheckboxField } from '../../formik-fields';
import { DropdownField, DroppableFileInputField, CheckboxField } from '@console/shared';
import {
TerminationTypes,
PassthroughInsecureTrafficTypes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ import {
ServiceModel,
KnativeServingModel,
} from '@console/knative-plugin';
import { getBadgeFromType } from '@console/shared';
import { getBadgeFromType, RadioButtonField, RadioOption } from '@console/shared';
import { useAccessReview } from '@console/internal/components/utils';
import { getActiveNamespace } from '@console/internal/actions/ui';
import { Resources } from '../import-types';
import { RadioOption } from '../../formik-fields/field-types';
import { RadioButtonField } from '../../formik-fields';
import FormSection from './FormSection';
import './ResourceSection.scss';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as React from 'react';
import { TextInputTypes } from '@patternfly/react-core';
import { InputField } from '@console/shared';
import FormSection from '../section/FormSection';
import { RouteData } from '../import-types';
import { InputField } from '../../formik-fields';

export interface ServerlessRouteSectionProps {
route: RouteData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import {
ModalSubmitFooter,
} from '@console/internal/components/factory/modal';
import { Formik, FormikProps, FormikValues } from 'formik';
import { YellowExclamationTriangleIcon } from '@console/shared';
import { YellowExclamationTriangleIcon, InputField } from '@console/shared';
import { K8sResourceKind } from '@console/internal/module/k8s';
import { InputField } from '../formik-fields';

type DeleteApplicationModalProps = {
initialApplication: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { Form, TextInputTypes } from '@patternfly/react-core';
import { FormikProps, FormikValues } from 'formik';
import { useAccessReview } from '@console/internal/components/utils';
import { getActiveNamespace } from '@console/internal/actions/ui';
import { MultiColumnField, InputField } from '../../formik-fields';
import { FormFooter } from '../../form-utils';
import { MultiColumnField, InputField, FormFooter } from '@console/shared';

const PipelineParameters: React.FC<FormikProps<FormikValues>> = ({
handleSubmit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { Form, TextInputTypes } from '@patternfly/react-core';
import { FormikProps, FormikValues } from 'formik';
import { useAccessReview } from '@console/internal/components/utils';
import { getActiveNamespace } from '@console/internal/actions/ui';
import { MultiColumnField, InputField, DropdownField } from '../../formik-fields';
import { FormFooter } from '../../form-utils';
import { MultiColumnField, InputField, DropdownField, FormFooter } from '@console/shared';

enum resourceTypes {
'' = 'Select resource type',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as React from 'react';
import { FieldArray } from 'formik';
import { TextInputTypes } from '@patternfly/react-core';
import { InputField } from '@console/shared';
import FormSection from '../../import/section/FormSection';
import { InputField } from '../../formik-fields';

export interface ParamertersSectionProps {
parameters: {
Expand Down