Skip to content

Commit 567ea9f

Browse files
authored
Adding custom label for field and help text for field (#119)
1 parent f98b2d3 commit 567ea9f

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

examples/fastapi_sqlalchemy/example.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ class UserModelAdmin(SqlAlchemyModelAdmin):
2020
list_filter = ("id", "username", "is_superuser")
2121
search_fields = ("username",)
2222
formfield_overrides = { # noqa: RUF012
23-
"username": (WidgetType.SlugInput, {"required": True}),
23+
"username": (WidgetType.SlugInput, {"required": True,
24+
# Changing the field label to a custom option
25+
# "label": "Custom label",
26+
# Adding a small text below the field to better understand the purpose of the field
27+
# "help": "Detailed description of the field"
28+
}),
2429
"password": (WidgetType.PasswordInput, {"passwordModalForm": True}),
2530
"avatar_url": (
2631
WidgetType.Upload,
@@ -130,7 +135,6 @@ async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
130135

131136
app = FastAPI(lifespan=lifespan)
132137

133-
134138
app.mount("/admin", admin_app)
135139

136140
app.add_middleware(

frontend/src/components/form-container/index.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Col, Collapse, Divider, Form, Row } from "antd";
1+
import {Col, Collapse, Divider, Form, Row, Typography} from "antd";
22
import type React from "react";
33
import { useCallback, useEffect, useState } from "react";
44
import { useTranslation } from "react-i18next";
@@ -102,7 +102,10 @@ export const FormContainer: React.FC<IFormContainer> = ({
102102
<Form.Item
103103
key={field.name}
104104
name={field.name}
105-
label={getTitleFromFieldName(field.name)}
105+
label={
106+
getConf(field).form_widget_props?.label
107+
? getConf(field)?.form_widget_props?.label
108+
: getTitleFromFieldName(field.name)}
106109
rules={
107110
[
108111
...(getConf(field).required
@@ -167,6 +170,11 @@ export const FormContainer: React.FC<IFormContainer> = ({
167170
}
168171
>
169172
{getWidget(getConf(field))}
173+
{getConf(field).form_widget_props?.help &&
174+
<Typography.Text type={"secondary"} style={{marginTop: ".35rem", display: "flex"}}>
175+
{getConf(field).form_widget_props?.help}
176+
</Typography.Text>
177+
}
170178
</Form.Item>
171179
));
172180
},

0 commit comments

Comments
 (0)