Skip to content

Commit

Permalink
refactor: share common label code between fields
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvxd committed Oct 31, 2023
1 parent 746d896 commit 1530de7
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import getClassNameFactory from "../../../../lib/get-class-name-factory";
import inputStyles from "../../styles.module.css";
import styles from "./styles.module.css";
import { List, Plus, Trash } from "react-feather";
import { InputOrGroup, type InputProps } from "../..";
import { FieldLabelInternal, InputOrGroup, type InputProps } from "../..";
import { IconButton } from "../../../IconButton";
import { reorder, replace } from "../../../../lib";
import DroppableStrictMode from "../../../DroppableStrictMode";
Expand Down Expand Up @@ -69,13 +69,12 @@ export const ArrayField = ({
}

return (
<div className={getClassNameInput()}>
<b className={getClassNameInput("label")}>
<div className={getClassNameInput("labelIcon")}>
<List size={16} />
</div>
{label || name}
</b>
<FieldLabelInternal
label={label || name}
icon={<List size={16} />}
el="div"
readOnly={readOnly}
>
<DragDropContext
onDragEnd={(event) => {
if (event.destination) {
Expand Down Expand Up @@ -221,6 +220,6 @@ export const ArrayField = ({
}}
</DroppableStrictMode>
</DragDropContext>
</div>
</FieldLabelInternal>
);
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import getClassNameFactory from "../../../../lib/get-class-name-factory";
import styles from "../../styles.module.css";
import { Hash, Type } from "react-feather";
import type { InputProps } from "../..";
import { FieldLabelInternal, type InputProps } from "../..";

const getClassName = getClassNameFactory("Input", styles);

Expand All @@ -14,14 +14,16 @@ export const DefaultField = ({
label,
}: InputProps) => {
return (
<label className={getClassName({ readOnly })}>
<div className={getClassName("label")}>
<div className={getClassName("labelIcon")}>
<FieldLabelInternal
label={label || name}
icon={
<>
{field.type === "text" && <Type size={16} />}
{field.type === "number" && <Hash size={16} />}
</div>
{label || name}
</div>
</>
}
readOnly={readOnly}
>
<input
className={getClassName("input")}
autoComplete="off"
Expand All @@ -37,6 +39,6 @@ export const DefaultField = ({
}}
readOnly={readOnly}
/>
</label>
</FieldLabelInternal>
);
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import getClassNameFactory from "../../../../lib/get-class-name-factory";
import styles from "../../styles.module.css";
import type { InputProps } from "../..";
import { FieldLabelInternal, type InputProps } from "../..";
import { ExternalInput } from "../../../ExternalInput";
import { Link } from "react-feather";

Expand All @@ -18,15 +18,12 @@ export const ExternalField = ({
}

return (
<div className={getClassName()}>
<div className={getClassName("label")}>
<div className={getClassName("labelIcon")}>
<Link size={16} />
</div>

{label || name}
</div>
<FieldLabelInternal
label={label || name}
icon={<Link size={16} />}
el="div"
>
<ExternalInput field={field} onChange={onChange} value={value} />
</div>
</FieldLabelInternal>
);
};
78 changes: 37 additions & 41 deletions packages/core/components/InputOrGroup/fields/RadioField/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import getClassNameFactory from "../../../../lib/get-class-name-factory";
import styles from "../../styles.module.css";
import { CheckCircle } from "react-feather";
import type { InputProps } from "../..";
import { FieldLabelInternal, type InputProps } from "../..";

const getClassName = getClassNameFactory("Input", styles);

Expand All @@ -17,47 +17,43 @@ export const RadioField = ({
}

return (
<div className={getClassName({ readOnly })}>
<div className={getClassName("radioGroup")}>
<div className={getClassName("label")}>
<div className={getClassName("labelIcon")}>
<CheckCircle size={16} />
</div>
{field.label || name}
</div>
<FieldLabelInternal
icon={<CheckCircle size={16} />}
label={field.label || name}
readOnly={readOnly}
el="div"
>
<div className={getClassName("radioGroupItems")}>
{field.options.map((option) => (
<label
key={option.label + option.value}
className={getClassName("radio")}
>
<input
type="radio"
className={getClassName("radioInput")}
value={option.value as string | number}
name={name}
onChange={(e) => {
if (
e.currentTarget.value === "true" ||
e.currentTarget.value === "false"
) {
onChange(JSON.parse(e.currentTarget.value));
return;
}

<div className={getClassName("radioGroupItems")}>
{field.options.map((option) => (
<label
key={option.label + option.value}
className={getClassName("radio")}
>
<input
type="radio"
className={getClassName("radioInput")}
value={option.value as string | number}
name={name}
onChange={(e) => {
if (
e.currentTarget.value === "true" ||
e.currentTarget.value === "false"
) {
onChange(JSON.parse(e.currentTarget.value));
return;
}

onChange(e.currentTarget.value);
}}
disabled={readOnly}
defaultChecked={value === option.value}
/>
<div className={getClassName("radioInner")}>
{option.label || option.value}
</div>
</label>
))}
</div>
onChange(e.currentTarget.value);
}}
disabled={readOnly}
defaultChecked={value === option.value}
/>
<div className={getClassName("radioInner")}>
{option.label || option.value}
</div>
</label>
))}
</div>
</div>
</FieldLabelInternal>
);
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import getClassNameFactory from "../../../../lib/get-class-name-factory";
import styles from "../../styles.module.css";
import { ChevronDown } from "react-feather";
import type { InputProps } from "../..";
import { FieldLabelInternal, type InputProps } from "../..";

const getClassName = getClassNameFactory("Input", styles);

Expand All @@ -18,13 +18,11 @@ export const SelectField = ({
}

return (
<label className={getClassName({ readOnly })}>
<div className={getClassName("label")}>
<div className={getClassName("labelIcon")}>
<ChevronDown size={16} />
</div>
{label || name}
</div>
<FieldLabelInternal
label={label || name}
icon={<ChevronDown size={16} />}
readOnly={readOnly}
>
<select
className={getClassName("input")}
disabled={readOnly}
Expand All @@ -49,6 +47,6 @@ export const SelectField = ({
/>
))}
</select>
</label>
</FieldLabelInternal>
);
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import getClassNameFactory from "../../../../lib/get-class-name-factory";
import styles from "../../styles.module.css";
import { Type } from "react-feather";
import type { InputProps } from "../..";
import { FieldLabelInternal, type InputProps } from "../..";

const getClassName = getClassNameFactory("Input", styles);

Expand All @@ -13,13 +13,11 @@ export const TextareaField = ({
label,
}: InputProps) => {
return (
<label className={getClassName({ readOnly })}>
<div className={getClassName("label")}>
<div className={getClassName("labelIcon")}>
<Type size={16} />
</div>
{label || name}
</div>
<FieldLabelInternal
label={label || name}
icon={<Type size={16} />}
readOnly={readOnly}
>
<textarea
className={getClassName("input")}
autoComplete="off"
Expand All @@ -29,6 +27,6 @@ export const TextareaField = ({
readOnly={readOnly}
rows={5}
/>
</label>
</FieldLabelInternal>
);
};
38 changes: 36 additions & 2 deletions packages/core/components/InputOrGroup/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,53 @@ export const FieldLabel = ({
children,
icon,
label,
el = "label",
readOnly,
className,
}: {
children?: ReactNode;
icon?: ReactNode;
label: string;
el?: "label" | "div";
readOnly?: boolean;
className?: string;
}) => {
const El = el;
return (
<label>
<El className={className}>
<div className={getClassName("label")}>
{icon ? <div className={getClassName("labelIcon")}>{icon}</div> : <></>}
{label}
</div>
{children}
</label>
</El>
);
};

export const FieldLabelInternal = ({
children,
icon,
label,
el = "label",
readOnly,
}: {
children?: ReactNode;
icon?: ReactNode;
label: string;
el?: "label" | "div";
readOnly?: boolean;
}) => {
const El = el;
return (
<FieldLabel
label={label}
icon={icon}
className={getClassName({ readOnly })}
readOnly={readOnly}
el={el}
>
{children}
</FieldLabel>
);
};

Expand Down
4 changes: 2 additions & 2 deletions packages/core/components/InputOrGroup/styles.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@
background-color: white;
}

.Input--readOnly .Input-input,
.Input--readOnly select.Input-input {
.Input--readOnly > .Input-input,
.Input--readOnly > select.Input-input {
background-color: var(--puck-color-grey-11);
border-color: var(--puck-color-grey-8);
color: var(--puck-color-grey-5);
Expand Down

0 comments on commit 1530de7

Please sign in to comment.