Skip to content

Commit

Permalink
Merge pull request #29 from Yuyz0112/feat/form-windlike
Browse files Browse the repository at this point in the history
Feat/form windlike
  • Loading branch information
Yuyz0112 committed Sep 13, 2022
2 parents ed26d62 + 6dbca75 commit a18a2d7
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 36 deletions.
54 changes: 35 additions & 19 deletions src/_internal/atoms/themes/CloudTower/components/SummaryList.tsx
Expand Up @@ -141,20 +141,28 @@ export type Item = {
removable?: boolean;
};

export type Label = {
type: "Label";
label: string;
icon?: IconTypes;
};

export type Group = {
title: string;
children: (Item | SubHeading | Object)[];
children: (Item | SubHeading | Object | Label)[];
};

function SummaryItem(props: Item) {
return (
<ItemDiv>
<ItemContent title={`${props.label} : ${props.value}`}>
<Label className={Typo.Label.l4_regular}>
{props.label}&nbsp;:&nbsp;
</Label>
<Value className={Typo.Label.l4_regular}>{props.value || ""}</Value>
</ItemContent>
{props.label ? (
<ItemContent title={`${props.label} : ${props.value}`}>
<Label className={Typo.Label.l4_regular}>
{props.label}&nbsp;:&nbsp;
</Label>
<Value className={Typo.Label.l4_regular}>{props.value || ""}</Value>
</ItemContent>
) : null}
{props.removable ? (
<CloseIcon>
<Icon type="1-xmark-remove-16-secondary" />
Expand All @@ -164,7 +172,22 @@ function SummaryItem(props: Item) {
);
}

function Field(props: Item | Object | SubHeading) {
function SummaryLabel(props: Omit<Label, "type">) {
return (
<ItemDiv>
<ItemContent title={props.label}>
{props.icon ? (
<ObjectIcon>
<Icon type={props.icon}></Icon>
</ObjectIcon>
) : null}
<Label className={Typo.Label.l4_regular}>{props.label}</Label>
</ItemContent>
</ItemDiv>
);
}

function Field(props: Item | Object | SubHeading | Label) {
if (props.type === "Item") {
return <SummaryItem {...props}></SummaryItem>;
} else if (props.type === "SubHeading") {
Expand All @@ -174,21 +197,14 @@ function Field(props: Item | Object | SubHeading) {
} else if (props.type === "Object") {
return (
<>
<ItemDiv>
<ItemContent title={props.label}>
{props.icon ? (
<ObjectIcon>
<Icon type={props.icon}></Icon>
</ObjectIcon>
) : null}
<Label className={Typo.Label.l4_regular}>{props.label}</Label>
</ItemContent>
</ItemDiv>
{props.label ? <SummaryLabel {...props}></SummaryLabel> : null}
{props.items.map((item) => (
<SummaryItem key={item.label} {...item}></SummaryItem>
))}
</>
);
} else if (props.type === "Label") {
return <SummaryLabel {...props}></SummaryLabel>;
}

return null;
Expand All @@ -198,7 +214,7 @@ type Props = {
title: string;
defaultWidth?: string;
groups?: Group[];
items?: (Item | Object | SubHeading)[];
items?: (Item | Object | SubHeading | Label)[];
};

function SummaryList(props: Props) {
Expand Down
5 changes: 5 additions & 0 deletions src/_internal/molecules/AutoForm/SpecField.tsx
Expand Up @@ -76,6 +76,11 @@ const FormItemStyle = css`
.dovetail-ant-form-item-extra {
margin-top: 5px;
}
&.dovetail-ant-form-item-has-error .ant-select-selector,
&.dovetail-ant-form-item-has-error .ant-input {
border-color: #f0483e !important;
}
`;

const FormItemLabelStyle = css`
Expand Down
2 changes: 1 addition & 1 deletion src/_internal/molecules/InputNumber.tsx
Expand Up @@ -28,7 +28,7 @@ const InputNumber = (props: Props) => {
`${props.subKey ? `${props.subKey}${props.field?.key ? '-' : ''}` : ""}${props.field?.key || ""}`
);
}
}, [props.onChange]);
}, [props.onChange, stringValue]);

return (
<AntdInput
Expand Down
26 changes: 19 additions & 7 deletions src/_internal/organisms/KubectlApplyForm/useSummary.tsx
Expand Up @@ -3,6 +3,7 @@ import type {
Item,
Object,
SubHeading,
Label,
} from "../../atoms/themes/CloudTower/components/SummaryList";
import { Layout, Field } from "./KubectlApplyForm";
import { get } from "lodash";
Expand All @@ -24,10 +25,10 @@ function getValueByPath(formData: Record<string, any>, path: string) {
function getListItems(
fields: Field[],
formData: Record<string, any>
): (Item | Object | SubHeading)[] {
): (Item | Object | SubHeading | Label)[] {
return fields
.map((field) => {
const items: (Item | Object | SubHeading)[] = [];
const items: (Item | Object | SubHeading | Label)[] = [];

if (field.sectionTitle) {
items.push({
Expand All @@ -36,9 +37,16 @@ function getListItems(
});
}

const value = getValueByPath(formData, field.path);
const path = field.path.replace(/(.\$add)|(.\$i)/g, (substring)=> {
if (substring === '.$add') {
return '';
} else {
return '.0';
}
});
const value = getValueByPath(formData, path);

if (value instanceof Array && field.label) {
if (value instanceof Array) {
if (typeof value[0] !== "object") {
items.push({
type: "Item" as const,
Expand All @@ -47,8 +55,12 @@ function getListItems(
});
}

value.forEach((item) => {
value.forEach((item, index) => {
if (item && typeof item === "object") {
items.push({
type: "Label" as const,
label: field.widgetOptions?.title + ' ' + (index + 1),
});
items.push({
type: "Object" as const,
icon: field.widgetOptions?.icon,
Expand All @@ -69,10 +81,10 @@ function getListItems(
});
}
});
} else if (value && typeof value === "object" && field.label) {
} else if (value && typeof value === "object") {
items.push({
type: "Object" as const,
label: field.label,
label: field.label || field.widgetOptions?.title,
icon: field.widgetOptions?.icon,
items: field.fields?.length
? (getListItems(field.fields, value) as Item[])
Expand Down
16 changes: 7 additions & 9 deletions src/sunmao/components/KubectlApplyForm.tsx
Expand Up @@ -275,15 +275,13 @@ export const KubectlApplyForm = implementRuntimeComponent({
});
useEffect(() => {
subscribeMethods({
setField({ fieldPath, value }) {
setValues((prevValues) => {
set(prevValues, fieldPath, value);
const newValues = [...prevValues];
mergeState({
value: newValues,
});
return newValues;
setField({ fieldPath, value: fieldValue }) {
const newValues = set(values, fieldPath, fieldValue);

mergeState({
value: newValues,
});
setValues(newValues);
},
nextStep() {
mergeState({
Expand All @@ -292,7 +290,7 @@ export const KubectlApplyForm = implementRuntimeComponent({
setStep(step + 1);
}
});
}, [step]);
}, [step, subscribeMethods, mergeState]);

return (
<_KubectlApplyForm
Expand Down

0 comments on commit a18a2d7

Please sign in to comment.