mirror of
https://github.com/zhigang1992/react-jsonschema-form.git
synced 2026-01-12 09:23:40 +08:00
* #226 - Restrict accepted mimetypes in file widget * Adding a test case for accept attribute on file widget * #226 - Updated a documentation for file accept attribute * Change text for file widget accept attribute * Added grammatical things
This commit is contained in:
committed by
Ashwin Ramaswami
parent
59ab93fbff
commit
beee02f49b
1
.gitignore
vendored
1
.gitignore
vendored
@@ -124,6 +124,7 @@ yarn.lock
|
||||
|
||||
# IDE
|
||||
.vscode
|
||||
.idea
|
||||
|
||||
# Code coverage
|
||||
coverage
|
||||
|
||||
@@ -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 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((
|
||||
<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:
|
||||
|
||||
@@ -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: {},
|
||||
};
|
||||
|
||||
@@ -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>
|
||||
@@ -104,6 +104,7 @@ class FileWidget extends Component {
|
||||
defaultValue=""
|
||||
autoFocus={autofocus}
|
||||
multiple={multiple}
|
||||
accept={options.accept}
|
||||
/>
|
||||
</p>
|
||||
<FilesInfo filesInfo={filesInfo} />
|
||||
|
||||
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user