Skip to content

Commit 2df2277

Browse files
committed
feat: add highlight required fields example
1 parent c248e2d commit 2df2277

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import type {
2+
AnnotationsUnion,
3+
FormField,
4+
Instance,
5+
RendererConfiguration,
6+
} from "@nutrient-sdk/viewer";
7+
import { baseOptions } from "../../shared/base-options";
8+
9+
let requiredFormFields: ReturnType<
10+
typeof window.NutrientViewer.Immutable.List<FormField>
11+
>;
12+
13+
window.NutrientViewer.load({
14+
...baseOptions,
15+
theme: window.NutrientViewer.Theme.DARK,
16+
customRenderers: {
17+
Annotation: ({
18+
annotation,
19+
}: {
20+
annotation: AnnotationsUnion;
21+
}): RendererConfiguration | null => {
22+
if (
23+
!(
24+
annotation instanceof
25+
window.NutrientViewer.Annotations.WidgetAnnotation
26+
)
27+
) {
28+
return null;
29+
}
30+
31+
const isRequiredField =
32+
requiredFormFields &&
33+
requiredFormFields.size > 0 &&
34+
requiredFormFields.find(
35+
(formField: FormField) => formField.name === annotation.formFieldName,
36+
);
37+
38+
if (isRequiredField) {
39+
const node = document.createElement("div");
40+
node.classList.add("required-form-field");
41+
42+
return {
43+
node,
44+
append: true,
45+
};
46+
}
47+
48+
return null;
49+
},
50+
},
51+
}).then(async (instance: Instance) => {
52+
const formFields = await instance.getFormFields();
53+
54+
const changes = formFields.map((formField: FormField) =>
55+
formField.set("required", true),
56+
);
57+
58+
await instance.update(changes.toArray());
59+
60+
const updatedFormFields = await instance.getFormFields();
61+
62+
requiredFormFields = updatedFormFields.filter(
63+
(formField: FormField) => formField.required,
64+
);
65+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
category: forms
3+
title: Highlight Required Fields
4+
description: Mark form fields as required and visually highlight them with a custom renderer overlay.
5+
keywords: [forms, required, validation, custom-renderer, widget]
6+
---
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* Add your CSS here */
2+
.required-form-field:after {
3+
content: " *";
4+
color: red;
5+
font-weight: bold;
6+
position: absolute;
7+
right: -12px;
8+
top: 35%;
9+
transform: translateY(-50%);
10+
}

0 commit comments

Comments
 (0)