-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make AttachmentPicker a component instead of a lib to fix Safari #721
Changes from 3 commits
fad3917
2854e9f
d2659e0
e27da2b
d79269d
b20f520
8ea88a2
b2a9203
df66da1
707c44c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import React, {createRef} from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
const propTypes = { | ||
children: PropTypes.func.isRequired, | ||
}; | ||
|
||
class AttachmentPicker extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.onChangeCallback = createRef(); | ||
} | ||
|
||
render() { | ||
return ( | ||
<> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need this here? I read the fragments doc (this is still new to me) but I'm still not sure why it'd be required in this case. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is mostly to keep the component view hierarchy clean. We could make this a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By convention we can only return a single root element or an array from any react component. Using arrays is problematic since you must then add keys to everything when they might not ever change. And wrapping things with "container" elements just makes markup heavy for no reason. Fragment is an escape hatch for that React convention. So, sort of a way of flagging things and telling react that it's OK to render this as a sibling to whatever comes after it and we don't need any keys. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah perfect, that's the context I was missing, thanks for the explanation! |
||
<input | ||
hidden | ||
type="file" | ||
ref={el => this.fileInput = el} | ||
onChange={(e) => { | ||
const file = e.target.files[0]; | ||
file.uri = URL.createObjectURL(file); | ||
this.onChangeCallback.current(file); | ||
}} | ||
/> | ||
{this.props.children({ | ||
show: (callback) => { | ||
this.onChangeCallback.current = callback; | ||
this.fileInput.click(); | ||
}, | ||
})} | ||
</> | ||
); | ||
} | ||
} | ||
|
||
AttachmentPicker.propTypes = propTypes; | ||
export default AttachmentPicker; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import React, {createRef} from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import AttachmentPickerNative from '../../libs/AttachmentPickerNative'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is breaking the file naming conventions of the repo. What I would probably suggest is that if this is the only file that imports |
||
|
||
const propTypes = { | ||
children: PropTypes.func.isRequired, | ||
}; | ||
|
||
class AttachmentPicker extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.onChangeCallback = createRef(); | ||
} | ||
|
||
render() { | ||
return this.props.children({ | ||
show: (callback) => { | ||
AttachmentPickerNative.show((response) => { | ||
callback(AttachmentPickerNative.getDataForUpload(response)); | ||
}); | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
AttachmentPicker.propTypes = propTypes; | ||
export default AttachmentPicker; |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this be switched to a functional component now? I think that
this.onPicked
is the only thing stopping it, but I think this might work:(Let me know if that doesn't make sense)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does, but we also have both a
ref
and an instance field to storeonPicked
. So, I can make this a function, but will need to use a hook calleduseRef()
and it will end up looking like this...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason why your example won't work is because
let onPicked
cannot be updated in the render cycle. Which is what hooks are for they allow you to store values (and do other junk) from one render to the next --> https://reactjs.org/docs/hooks-reference.html#userefThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, thanks for explaining and exploring that. Let's just leave it the way it is for now.