diff --git a/.gitignore b/.gitignore index b5009d0cbe..14a7909c8b 100644 --- a/.gitignore +++ b/.gitignore @@ -124,6 +124,7 @@ yarn.lock # IDE .vscode +.idea # Code coverage coverage diff --git a/docs/form-customization.md b/docs/form-customization.md index 8aeb64634b..a1f15e6e5b 100644 --- a/docs/form-customization.md +++ b/docs/form-customization.md @@ -231,6 +231,28 @@ The included `FileWidget` exposes a reference to the `` ele This allows you to programmatically trigger the browser's file selector, which can be used in a custom file widget. +#### File widget options + +##### `accept` option + +You can use the accept attribute to specify a filter for what file types the user can upload: + +```jsx +const schema = { + type: "string", + format: "data-url" +}; + +const uiSchema = { + "ui:options": { accept: ".pdf" } +}; + +render(( +
+), document.getElementById("app")); +``` + ### Object fields ordering Since the order of object properties in Javascript and JSON is not guaranteed, the `uiSchema` object spec allows you to define the order in which properties are rendered using the `ui:order` property: diff --git a/playground/samples/files.js b/playground/samples/files.js index 7775ef2cd7..613a3cdf61 100644 --- a/playground/samples/files.js +++ b/playground/samples/files.js @@ -16,8 +16,17 @@ module.exports = { format: "data-url", }, }, + filesAccept: { + type: "string", + format: "data-url", + title: "Single File with Accept attribute", + }, + }, + }, + uiSchema: { + filesAccept: { + "ui:options": { accept: ".pdf" }, }, }, - uiSchema: {}, formData: {}, }; diff --git a/src/components/widgets/FileWidget.js b/src/components/widgets/FileWidget.js index 836c3c635d..fa6d78427a 100644 --- a/src/components/widgets/FileWidget.js +++ b/src/components/widgets/FileWidget.js @@ -90,7 +90,7 @@ class FileWidget extends Component { }; render() { - const { multiple, id, readonly, disabled, autofocus } = this.props; + const { multiple, id, readonly, disabled, autofocus, options } = this.props; const { filesInfo } = this.state; return (
@@ -104,6 +104,7 @@ class FileWidget extends Component { defaultValue="" autoFocus={autofocus} multiple={multiple} + accept={options.accept} />

diff --git a/test/StringField_test.js b/test/StringField_test.js index b1a7437c49..97189b26bb 100644 --- a/test/StringField_test.js +++ b/test/StringField_test.js @@ -1648,6 +1648,20 @@ describe("StringField", () => { }); it("should render the widget with the expected id", () => { + const { node } = createFormComponent({ + schema: { + type: "string", + format: "data-url", + }, + uiSchema: { + "ui:options": { accept: ".pdf" }, + }, + }); + + expect(node.querySelector("[type=file]").accept).eql(".pdf"); + }); + + it("should render the file widget with accept attribute", () => { const { node } = createFormComponent({ schema: { type: "string",