Skip to content

Commit

Permalink
feat: allow accept attribute to be configurable for file upload fields
Browse files Browse the repository at this point in the history
  • Loading branch information
jenbutongit committed Oct 21, 2024
1 parent c7100ad commit ffebdf8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
1 change: 1 addition & 0 deletions model/src/components/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ export interface FileUploadFieldComponent {
exposeToContext?: boolean;
imageQualityPlayback?: boolean;
disableChangingFromSummary?: boolean;
accept?: string;
};
schema: {};
}
Expand Down
22 changes: 16 additions & 6 deletions runner/src/server/plugins/engine/components/FileUploadField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,16 @@ import { FileUploadFieldComponent } from "@xgovformbuilder/model";
import { FormModel } from "server/plugins/engine/models";
import joi, { Schema } from "joi";

type FileUploadAttributes = {
accept: string;
multiple?: string;
};

export class FileUploadField extends FormComponent {
dataType = "file" as DataType;
attributes: FileUploadAttributes = {
accept: "image/jpeg,image/gif,image/png,application/pdf",
};

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

if (options.multiple) {
this.attributes.multiple = "multiple";
}

if (options.accept) {
this.attributes.accept = options.accept;
}

componentSchema = componentSchema.messages({
"string.empty": "Upload {{#label}}",
});
Expand All @@ -40,12 +56,6 @@ export class FileUploadField extends FormComponent {
return { [this.name]: this.schema as Schema };
}

get attributes() {
return {
accept: "image/jpeg,image/gif,image/png,application/pdf",
};
}

getViewModel(formData: FormData, errors: FormSubmissionErrors) {
const { options } = this;
const viewModel: ViewModel = {
Expand Down

0 comments on commit ffebdf8

Please sign in to comment.