Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ yarn.lock

# IDE
.vscode
.idea

# Code coverage
coverage
Expand Down
22 changes: 22 additions & 0 deletions docs/form-customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,28 @@ The included `FileWidget` exposes a reference to the `<input type="file" />` 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 accept attribute to specify a filter for what file types the user can pick from the file input dialog box
Comment thread
gr8pathik marked this conversation as resolved.
Outdated

```jsx
const schema = {
type: "string",
format: "data-url"
};

const uiSchema = {
"ui:options": { accept: ".pdf" }
};

render((
<Form schema={schema}
uiSchema={uiSchema} />
), 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:
Expand Down
11 changes: 10 additions & 1 deletion playground/samples/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {},
};
3 changes: 2 additions & 1 deletion src/components/widgets/FileWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<div>
Expand All @@ -104,6 +104,7 @@ class FileWidget extends Component {
defaultValue=""
autoFocus={autofocus}
multiple={multiple}
accept={options.accept}
/>
</p>
<FilesInfo filesInfo={filesInfo} />
Expand Down
14 changes: 14 additions & 0 deletions test/StringField_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down