Skip to content
This repository was archived by the owner on Aug 23, 2022. It is now read-only.

Commit 3fe8721

Browse files
committed
Adding support for reading file value in event. Fixes #250
1 parent 5e699ec commit 3fe8721

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

src/utils/index.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,26 @@ function isEvent(event) {
3333
}
3434

3535
function getEventValue(event) {
36-
if (!event.target) {
36+
const { target } = event;
37+
38+
if (!target) {
3739
if (!event.nativeEvent) {
3840
return undefined;
3941
}
4042

4143
return event.nativeEvent.text;
4244
}
4345

44-
if (event.target.multiple) {
45-
return [...event.target.selectedOptions].map(option => option.value);
46+
if (target.type === 'file') {
47+
return [...target.files]
48+
|| (target.dataTransfer && [...target.dataTransfer.files]);
49+
}
50+
51+
if (target.multiple) {
52+
return [...target.selectedOptions].map(option => option.value);
4653
}
4754

48-
return event.target.value;
55+
return target.value;
4956
}
5057

5158
function getValue(value) {

test/field-component-spec.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,40 @@ describe('<Field /> component', () => {
454454
});
455455
});
456456

457+
describe('with <input type="file" />', () => {
458+
const store = applyMiddleware(thunk)(createStore)(combineReducers({
459+
testForm: formReducer('test'),
460+
test: modelReducer('test', { foo: [] }),
461+
}));
462+
463+
const field = TestUtils.renderIntoDocument(
464+
<Provider store={store}>
465+
<Field model="test.foo">
466+
<input type="file" />
467+
</Field>
468+
</Provider>
469+
);
470+
471+
const input = TestUtils.findRenderedDOMComponentWithTag(field, 'input');
472+
473+
TestUtils.Simulate.change(input, {
474+
target: {
475+
type: 'file',
476+
files: [
477+
{ name: 'first.jpg' },
478+
{ name: 'second.jpg' },
479+
],
480+
},
481+
});
482+
483+
assert.deepEqual(
484+
store.getState().test.foo,
485+
[
486+
{ name: 'first.jpg' },
487+
{ name: 'second.jpg' },
488+
]);
489+
});
490+
457491
describe('with <select>', () => {
458492
const store = applyMiddleware(thunk)(createStore)(combineReducers({
459493
testForm: formReducer('test'),

0 commit comments

Comments
 (0)