Skip to content

Commit 0ed95a3

Browse files
authored
feature: allow accept attribute to be configurable for file upload fields (#1293)
1 parent c7100ad commit 0ed95a3

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

model/src/components/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ export interface FileUploadFieldComponent {
232232
exposeToContext?: boolean;
233233
imageQualityPlayback?: boolean;
234234
disableChangingFromSummary?: boolean;
235+
accept?: string;
235236
};
236237
schema: {};
237238
}

runner/src/server/plugins/engine/components/FileUploadField.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,16 @@ import { FileUploadFieldComponent } from "@xgovformbuilder/model";
66
import { FormModel } from "server/plugins/engine/models";
77
import joi, { Schema } from "joi";
88

9+
type FileUploadAttributes = {
10+
accept: string;
11+
multiple?: string;
12+
};
13+
914
export class FileUploadField extends FormComponent {
1015
dataType = "file" as DataType;
16+
attributes: FileUploadAttributes = {
17+
accept: "image/jpeg,image/gif,image/png,application/pdf",
18+
};
1119

1220
constructor(def: FileUploadFieldComponent, model: FormModel) {
1321
super(def, model);
@@ -20,6 +28,14 @@ export class FileUploadField extends FormComponent {
2028
componentSchema = componentSchema.allow("").allow(null);
2129
}
2230

31+
if (options.multiple) {
32+
this.attributes.multiple = "multiple";
33+
}
34+
35+
if (options.accept) {
36+
this.attributes.accept = options.accept;
37+
}
38+
2339
componentSchema = componentSchema.messages({
2440
"string.empty": "Upload {{#label}}",
2541
});
@@ -40,12 +56,6 @@ export class FileUploadField extends FormComponent {
4056
return { [this.name]: this.schema as Schema };
4157
}
4258

43-
get attributes() {
44-
return {
45-
accept: "image/jpeg,image/gif,image/png,application/pdf",
46-
};
47-
}
48-
4959
getViewModel(formData: FormData, errors: FormSubmissionErrors) {
5060
const { options } = this;
5161
const viewModel: ViewModel = {

0 commit comments

Comments
 (0)