Skip to content

Commit

Permalink
feat(Form): Extract FieldWrapper field and increase text size
Browse files Browse the repository at this point in the history
  • Loading branch information
lordrip committed Feb 5, 2025
1 parent f5034fb commit df51056
Show file tree
Hide file tree
Showing 14 changed files with 178 additions and 212 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.kaoto-form {
&__label {
--pf-v6-c-form__label--FontSize: --pf-t--global--font--size--body--md;
--pf-v6-c-form__label-required--FontSize: --pf-t--global--font--size--body--lg;
}

/* stylelint-disable-next-line selector-class-pattern */
.pf-v6-c-text-input-group__text-input::placeholder {
font-style: italic;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { FunctionComponent, useCallback, useState } from 'react';
import { KaotoSchemaDefinition } from '../../../../models';
import { isDefined, ROOT_PATH, setValue } from '../../../../utils';
import { AutoField } from './fields/AutoField';
import './KaotoForm.scss';
import { FormComponentFactoryProvider } from './providers/FormComponentFactoryProvider';
import { ModelContextProvider } from './providers/ModelProvider';
import { SchemaDefinitionsProvider } from './providers/SchemaDefinitionsProvider';
Expand Down Expand Up @@ -45,7 +46,7 @@ export const KaotoForm: FunctionComponent<FormProps> = ({ schema, onChange, mode
<SchemaDefinitionsProvider schema={schema} omitFields={omitFields}>
<SchemaProvider schema={schema}>
<ModelContextProvider model={formModel} onPropertyChange={onPropertyChange}>
<Form>
<Form className="kaoto-form kaoto-form__label">
<AutoField propName={ROOT_PATH} />
</Form>
</ModelContextProvider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useFieldValue } from '../../hooks/field-value';
import { SchemaContext, SchemaProvider } from '../../providers/SchemaProvider';
import { FieldProps } from '../../typings';
import { AutoField } from '../AutoField';
import { FieldWrapper } from '../FieldWrapper';
import { ArrayFieldWrapper } from './ArrayFieldWrapper';

export const ArrayField: FunctionComponent<FieldProps> = ({ propName }) => {
const { schema, definitions } = useContext(SchemaContext);
Expand Down Expand Up @@ -42,7 +42,7 @@ export const ArrayField: FunctionComponent<FieldProps> = ({ propName }) => {
}, [value]);

return (
<FieldWrapper
<ArrayFieldWrapper
propName={propName}
type="array"
title={schema.title}
Expand All @@ -61,6 +61,6 @@ export const ArrayField: FunctionComponent<FieldProps> = ({ propName }) => {
</SchemaProvider>
);
})}
</FieldWrapper>
</ArrayFieldWrapper>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import {
Card,
CardBody,
CardHeader,
CardHeaderActionsObject,
CardTitle,
FormGroupLabelHelp,
Popover,
} from '@patternfly/react-core';
import { FunctionComponent, PropsWithChildren, ReactNode, useMemo } from 'react';
import { FieldProps } from '../../typings';

interface FieldWrapperProps extends FieldProps {
type: 'array' | 'object';
title?: string;
description?: string;
defaultValue?: unknown;
actions?: ReactNode;
}

export const ArrayFieldWrapper: FunctionComponent<PropsWithChildren<FieldWrapperProps>> = ({
propName,
type,
title: propsTitle,
description,
defaultValue,
actions,
children,
}) => {
const title = propsTitle ?? propName;
const id = `${title}-popover`;

const cardActions: CardHeaderActionsObject = useMemo(() => ({ actions, hasNoOffset: false }), [actions]);

return (
<Card>
<CardHeader actions={cardActions}>
<CardTitle>
{title}{' '}
<Popover
id={id}
headerContent={
<p>
{title} {`<${type}>`}
</p>
}
bodyContent={<p>{description}</p>}
footerContent={<p>Default: {defaultValue?.toString() ?? 'no default value'}</p>}
triggerAction="hover"
withFocusTrap={false}
>
<FormGroupLabelHelp aria-label={`More info for ${title} field`} />
</Popover>
</CardTitle>
</CardHeader>

{children && <CardBody className="pf-v6-c-form kaoto-form__label">{children}</CardBody>}
</Card>
);
};
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Checkbox, FormGroup, FormGroupLabelHelp, Popover } from '@patternfly/react-core';
import { Checkbox } from '@patternfly/react-core';
import { FunctionComponent, useContext } from 'react';
import { useFieldValue } from '../hooks/field-value';
import { SchemaContext } from '../providers/SchemaProvider';
import { FieldProps } from '../typings';
import { FieldWrapper } from './FieldWrapper';

export const BooleanField: FunctionComponent<FieldProps> = ({ propName, required }) => {
const { schema } = useContext(SchemaContext);
Expand All @@ -18,26 +19,13 @@ export const BooleanField: FunctionComponent<FieldProps> = ({ propName, required
const id = `${propName}-popover`;

return (
<FormGroup
fieldId={propName}
label={`${schema.title} (${propName})`}
isRequired={required}
labelHelp={
<Popover
id={id}
headerContent={
<p>
{schema.title} {`<${schema.type}>`}
</p>
}
bodyContent={<p>{schema.description}</p>}
footerContent={<p>Default: {schema.default?.toString() ?? 'no default value'}</p>}
triggerAction="hover"
withFocusTrap={false}
>
<FormGroupLabelHelp aria-label={`More info for ${schema.title} field`} />
</Popover>
}
<FieldWrapper
propName={propName}
required={required}
title={schema.title}
type="boolean"
description={schema.description}
defaultValue={schema.default?.toString()}
>
<Checkbox
id={propName}
Expand All @@ -47,6 +35,6 @@ export const BooleanField: FunctionComponent<FieldProps> = ({ propName, required
checked={value}
onChange={onFieldChange}
/>
</FormGroup>
</FieldWrapper>
);
};
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { FormGroup, FormGroupLabelHelp, Popover } from '@patternfly/react-core';
import { FunctionComponent, useCallback, useContext, useMemo } from 'react';
import { Typeahead } from '../../../../typeahead/Typeahead';
import { TypeaheadItem } from '../../../../typeahead/Typeahead.types';
import { useFieldValue } from '../hooks/field-value';
import { SchemaContext } from '../providers/SchemaProvider';
import { FieldProps } from '../typings';
import { Typeahead } from '../../../../typeahead/Typeahead';
import { TypeaheadItem } from '../../../../typeahead/Typeahead.types';
import { FieldWrapper } from './FieldWrapper';

export const EnumField: FunctionComponent<FieldProps> = ({ propName, required }) => {
const { schema } = useContext(SchemaContext);
Expand Down Expand Up @@ -52,25 +52,14 @@ export const EnumField: FunctionComponent<FieldProps> = ({ propName, required })
return <div>EnumField - Schema not defined</div>;
}

const id = `${propName}-popover`;

return (
<FormGroup
fieldId={propName}
label={`${schema.title} (${propName})`}
isRequired={required}
labelHelp={
<Popover
id={id}
headerContent={<p>{schema.title}</p>}
bodyContent={<p>{schema.description}</p>}
footerContent={<p>Default: {schema.default?.toString() ?? 'no default value'}</p>}
triggerAction="hover"
withFocusTrap={false}
>
<FormGroupLabelHelp aria-label={`More info for ${schema.title} field`} />
</Popover>
}
<FieldWrapper
propName={propName}
required={required}
title={schema.title}
type="enum"
description={schema.description}
defaultValue={schema.default?.toString()}
>
<Typeahead
selectedItem={selectedItem}
Expand All @@ -80,6 +69,6 @@ export const EnumField: FunctionComponent<FieldProps> = ({ propName, required })
onChange={onItemChange}
onCleanInput={onCleanInput}
/>
</FormGroup>
</FieldWrapper>
);
};
Original file line number Diff line number Diff line change
@@ -1,60 +1,48 @@
import {
Card,
CardBody,
CardHeader,
CardHeaderActionsObject,
CardTitle,
FormGroupLabelHelp,
Popover,
} from '@patternfly/react-core';
import { FunctionComponent, PropsWithChildren, ReactNode, useMemo } from 'react';
import { FormGroup, FormGroupLabelHelp, Popover } from '@patternfly/react-core';
import { FunctionComponent, PropsWithChildren, ReactNode } from 'react';
import { FieldProps } from '../typings';

interface FieldWrapperProps extends FieldProps {
type: string;
title?: string;
title?: ReactNode;
description?: string;
defaultValue?: unknown;
actions?: ReactNode;
defaultValue?: string;
}

export const FieldWrapper: FunctionComponent<PropsWithChildren<FieldWrapperProps>> = ({
propName,
required,
title,
type,
title: propsTitle,
description,
defaultValue,
actions,
defaultValue = 'no default value',
children,
}) => {
const title = propsTitle ?? propName;
const id = `${title}-popover`;

const cardActions: CardHeaderActionsObject = useMemo(() => ({ actions, hasNoOffset: false }), [actions]);
const id = `${propName}-popover`;

return (
<Card>
<CardHeader actions={cardActions}>
<CardTitle>
{title}{' '}
<Popover
id={id}
headerContent={
<p>
{title} {`<${type}>`}
</p>
}
bodyContent={<p>{description}</p>}
footerContent={<p>Default: {defaultValue?.toString() ?? 'no default value'}</p>}
triggerAction="hover"
withFocusTrap={false}
>
<FormGroupLabelHelp aria-label={`More info for ${title} field`} />
</Popover>
</CardTitle>
</CardHeader>

{children && <CardBody className="pf-v6-c-form">{children}</CardBody>}
</Card>
<FormGroup
fieldId={propName}
label={title ?? propName}
isRequired={required}
labelHelp={
<Popover
id={id}
headerContent={
<p>
{title} {`<${type}>`}
</p>
}
bodyContent={<p>{description}</p>}
footerContent={<p>Default: {defaultValue}</p>}
triggerAction="hover"
withFocusTrap={false}
>
<FormGroupLabelHelp aria-label={`More info for ${title} field`} />
</Popover>
}
>
{children}
</FormGroup>
);
};
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { FunctionComponent } from 'react';
import { KaotoSchemaDefinition } from '../../../../../models';
import { SchemaProvider } from '../providers/SchemaProvider';
import { FieldProps } from '../typings';
import { AutoField } from './AutoField';
import { KaotoSchemaDefinition } from '../../../../../../models';
import { SchemaProvider } from '../../providers/SchemaProvider';
import { FieldProps } from '../../typings';
import { AutoField } from '../AutoField';

interface AnyOfFieldProps extends FieldProps {
anyOf: KaotoSchemaDefinition['schema']['anyOf'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { isDefined, ROOT_PATH } from '../../../../../../utils';
import { useFieldValue } from '../../hooks/field-value';
import { SchemaContext } from '../../providers/SchemaProvider';
import { FieldProps } from '../../typings';
import { FieldWrapper } from '../FieldWrapper';
import { ArrayFieldWrapper } from '../ArrayField/ArrayFieldWrapper';
import { ObjectFieldGrouping } from './ObjectFieldGrouping';

export const ObjectField: FunctionComponent<FieldProps> = ({ propName, onRemove: onRemoveProps }) => {
Expand Down Expand Up @@ -37,7 +37,7 @@ export const ObjectField: FunctionComponent<FieldProps> = ({ propName, onRemove:
}

return (
<FieldWrapper
<ArrayFieldWrapper
propName={propName}
type="object"
title={schema.title}
Expand Down Expand Up @@ -68,6 +68,6 @@ export const ObjectField: FunctionComponent<FieldProps> = ({ propName, onRemove:
}
>
{isExpanded && <ObjectFieldGrouping propName={propName} />}
</FieldWrapper>
</ArrayFieldWrapper>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getFieldGroupsV2, getFilteredProperties, isDefined } from '../../../../
import { capitalizeString } from '../../../../../../utils/capitalize-string';
import { SchemaContext, SchemaProvider } from '../../providers/SchemaProvider';
import { FieldProps } from '../../typings';
import { AnyOfField } from '../AnyOfField';
import { AnyOfField } from './AnyOfField';
import { ObjectFieldInner } from './ObjectFieldInner';

const SPACE_REGEX = /\s/g;
Expand Down
Loading

0 comments on commit df51056

Please sign in to comment.