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

feat(form): add option to hide field labels #47

Merged
merged 1 commit into from
Jun 24, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/admin/utils/fieldTypes.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ export const common = {
type: "boolean",
},
},

dependencies: {
showAsModal: {
oneOf: [
Expand Down Expand Up @@ -89,6 +88,11 @@ export const common = {
},
},
},
"ui:label": {
title: "Show label",
type: "boolean",
default: true,
},
},
},
optionsUiSchemaUiSchema: {
Expand All @@ -109,6 +113,9 @@ export const common = {
},
"ui:order": ["showAsModal", "modal", "*"],
},
"ui:label": {
"ui:widget": "switch",
},
},
};

Expand Down Expand Up @@ -168,6 +175,7 @@ const collections = {
},
},
},
"ui:label": common.optionsUiSchema.properties["ui:label"],
},
},
optionsUiSchemaUiSchema: {
Expand Down Expand Up @@ -215,6 +223,7 @@ const collections = {
},
},
},
"ui:label": common.optionsUiSchema.properties["ui:label"],
},
},
optionsUiSchemaUiSchema: {
Expand All @@ -239,6 +248,7 @@ const collections = {
"ui:field": "codeEditor",
},
},
"ui:label": common.optionsUiSchemaUiSchema["ui:label"],
},

default: {
Expand Down Expand Up @@ -364,7 +374,6 @@ const simple = {
optionsSchemaUiSchema: {
readOnly: extra.optionsSchemaUiSchema.readOnly,
isRequired: extra.optionsSchemaUiSchema.isRequired,

pattern: {
"ui:placeholder": "^.*$",
},
Expand Down Expand Up @@ -397,6 +406,7 @@ const simple = {
},
},
},
"ui:label": common.optionsUiSchema.properties["ui:label"],
},
},
optionsUiSchemaUiSchema: {
Expand All @@ -410,6 +420,7 @@ const simple = {
},
},
},
"ui:label": common.optionsUiSchemaUiSchema["ui:label"],
},
default: {
schema: {
Expand Down Expand Up @@ -471,6 +482,7 @@ const simple = {
},
},
},
"ui:label": common.optionsUiSchema.properties["ui:label"],
},
},
optionsUiSchemaUiSchema: {
Expand Down Expand Up @@ -662,6 +674,7 @@ const simple = {
},
},
},
"ui:label": common.optionsUiSchema.properties["ui:label"],
},
},
optionsUiSchemaUiSchema: {
Expand Down Expand Up @@ -942,6 +955,7 @@ const advanced = {
},
},
},
"ui:label": common.optionsUiSchema.properties["ui:label"],
},
},
optionsUiSchemaUiSchema: {
Expand Down Expand Up @@ -1044,7 +1058,7 @@ const advanced = {
child: {},
optionsSchema: {
type: "object",
title: "ID Fetcher Field Schema",
title: "ID Fetcher Schema",
properties: {
...common.optionsSchema,
readOnly: extra.optionsSchema.readOnly,
Expand Down Expand Up @@ -1213,6 +1227,7 @@ const advanced = {
},
},
},
"ui:label": common.optionsUiSchema.properties["ui:label"],
},
},
optionsUiSchemaUiSchema: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import ArrayUtils from "./ArrayUtils";
const ArrayFieldTemplateItem = ({
children,
disabled,
formContext,
hasMoveDown,
hasMoveUp,
hasRemove,
Expand All @@ -14,12 +13,11 @@ const ArrayFieldTemplateItem = ({
onDropIndexClick,
onReorderClick,
readonly,
uiSchema,
}) => {
const { toolbarAlign = "top" } = formContext;

return (
<Row
align={toolbarAlign}
align={uiSchema && uiSchema["ui:label"] === false ? "top" : "middle"}
key={`array-item-${index}`}
style={{ margin: "10px 0px" }}
className="arrayFieldRow"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useEffect, useState } from "react";
import classNames from "classnames";

import Button from "antd/lib/button";
import { Row, Col, Modal, Space, Tag, Checkbox, Table, theme } from "antd";
Expand Down Expand Up @@ -30,7 +29,6 @@ const NormalArrayFieldTemplate = ({
items,
options,
onAddClick,
prefixCls,
readonly,
required,
schema,
Expand All @@ -39,7 +37,7 @@ const NormalArrayFieldTemplate = ({
formData,
}) => {
const { useToken } = theme;
const { labelAlign = "right", rowGutter = 24 } = formContext;
const { rowGutter = 24 } = formContext;

const [latexData, setLatexData] = useState(null);
const [showModal, setShowModal] = useState(false);
Expand All @@ -51,11 +49,6 @@ const NormalArrayFieldTemplate = ({
);
const [copy, setCopy] = useState(false);
const [importModal, setImportModal] = useState(false);
const labelClsBasic = `${prefixCls}-item-label`;
const labelColClassName = classNames(
labelClsBasic,
labelAlign === "left" && `${labelClsBasic}-left`,
);
const { token } = useToken();

let uiImport = null;
Expand Down Expand Up @@ -150,11 +143,7 @@ const NormalArrayFieldTemplate = ({
}, [emailModal]);

return (
<fieldset
style={{ marginLeft: "12px", marginRight: "12px" }}
className={className}
id={idSchema.$id}
>
<fieldset className={className} id={idSchema.$id}>
{uiLatex && (
<Modal
destroyOnClose
Expand Down Expand Up @@ -256,35 +245,31 @@ const NormalArrayFieldTemplate = ({
</Modal>
)}
<Row gutter={rowGutter}>
<div style={{ marginBottom: "8px", width: "100%" }}>
{title && (
<Col
className={labelColClassName}
span={24}
style={{ padding: "0" }}
>
<TitleField
id={`${idSchema.$id}__title`}
key={`array-field-title-${idSchema.$id}`}
required={required}
title={uiSchema["ui:title"] || title}
uiImport={uiImport}
uiLatex={uiLatex}
uiEmail={uiEmail}
readonly={readonly}
enableLatex={() => _enableLatex()}
enableImport={() => setImportModal(true)}
enableEmail={() => setEmailModal(true)}
/>
</Col>
)}
<FieldHeader
description={uiSchema["ui:description"] || schema.description}
uiSchema={uiSchema}
key={`array-field-header-${idSchema.$id}`}
idSchema={idSchema}
/>
</div>
{uiSchema["ui:label"] != false && (
<div style={{ marginBottom: "8px", width: "100%" }}>
<FieldHeader
titleField={
<TitleField
id={`${idSchema.$id}__title`}
key={`array-field-title-${idSchema.$id}`}
required={required}
title={uiSchema["ui:title"] || title}
uiImport={uiImport}
uiLatex={uiLatex}
uiEmail={uiEmail}
readonly={readonly}
enableLatex={() => _enableLatex()}
enableImport={() => setImportModal(true)}
enableEmail={() => setEmailModal(true)}
/>
}
description={uiSchema["ui:description"] || schema.description}
uiSchema={uiSchema}
key={`array-field-header-${idSchema.$id}`}
idSchema={idSchema}
/>
</div>
)}
<Col span={24} style={{ marginTop: "5px" }} className="nestedObject">
<Row>
{items && (
Expand Down
19 changes: 14 additions & 5 deletions src/forms/templates/Field/FieldHeader.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@ import { Space, Typography } from "antd";
import Markdown from "../../../partials/Markdown";
import TitleField from "../../fields/internal/TitleField";

const FieldHeader = ({ label, description, uiSchema, isObject, idSchema }) => {
const FieldHeader = ({
label,
description,
uiSchema,
isObject,
idSchema,
titleField,
}) => {
return (
<Space direction="vertical" size={0}>
{titleField && titleField}
{uiSchema["ui:title"] !== false && label && (
<TitleField
title={label}
Expand All @@ -16,8 +24,8 @@ const FieldHeader = ({ label, description, uiSchema, isObject, idSchema }) => {
id={`${idSchema.$id}-title`}
/>
)}
<Typography.Text type="secondary" id={`${idSchema.$id}-description`}>
{description && (
{description && (
<Typography.Text type="secondary" id={`${idSchema.$id}-description`}>
<Markdown
text={description}
style={{
Expand All @@ -28,8 +36,8 @@ const FieldHeader = ({ label, description, uiSchema, isObject, idSchema }) => {
uiSchema["ui:options"].descriptionIsMarkdown
}
/>
)}
</Typography.Text>
</Typography.Text>
)}
</Space>
);
};
Expand All @@ -41,6 +49,7 @@ FieldHeader.propTypes = {
description: PropTypes.node,
isObject: PropTypes.bool,
idSchema: PropTypes.object,
titleField: PropTypes.element,
};

export default FieldHeader;
4 changes: 3 additions & 1 deletion src/forms/templates/Field/FieldTemplate.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,11 @@ const FieldTemplate = ({
hasFeedback={schema.type !== "array" && schema.type !== "object"}
help={(!!rawHelp && help) || (!!rawErrors && renderFieldErrors())}
htmlFor={id}
// displayLabel is always false for custom fields, so we need the or condition
label={
!shouldShowAsModal &&
(displayLabel || uiSchema["ui:field"]) &&
(displayLabel ||
(uiSchema["ui:field"] && uiSchema["ui:label"] != false)) &&
label && (
<FieldHeader
label={label}
Expand Down
Loading