-
Notifications
You must be signed in to change notification settings - Fork 4k
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
feat(Ref): implement innerRef
on all components
#1879
Conversation
Very cool, thanks for getting this going. It has been a long time running and will solve many issues. ❤️ I assume this PR will also apply this to all the problem areas noted in the original issue? |
Yep, however there is an unresolvable problem with a HOC. const RefsSupported = Ref(RefsNotSupported)
<RefsSupported ref={node => console.log(node)} /> There is no way to make this working because |
Agreed, in my example on react.run I think I used |
Codecov Report
@@ Coverage Diff @@
## master #1879 +/- ##
===========================================
- Coverage 99.73% 82.82% -16.92%
===========================================
Files 152 147 -5
Lines 2656 2492 -164
===========================================
- Hits 2649 2064 -585
- Misses 7 428 +421
Continue to review full report at Codecov.
|
574158a
to
f1e6df7
Compare
test/utils/shallow.js
Outdated
} | ||
} | ||
|
||
export default (...args) => dive(new Wrapper(...args)) |
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.
@levithomason I need you there.
After I injected withRef
HOC to getElementType
I received many broken tests. Half of then fixed with the code above, its idea to dive
automatically when shallow
is called.
Another half shoud be fixed with overriding of the shallow
method on ShallowWrapper
, but the inheritance doesn't work. WTF? Can you explain what I'm doing wrong? I think that I'm missing something basic, but at now it destroys my mind. phantomjs
doesn't support neither Proxy
, nor __noSuchMethod__
😢
Example test:
shallow(<Confirm cancelButton='foo' />)
.find('Button')
.first()
.shallow()
.should.have.text('foo')
expected <Button /> to have text 'foo', but it has '<refHOC />'
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.
const shallowMethod = ShallowWrapper.prototype.shallow
ShallowWrapper.prototype.shallow = function (...args) {
return dive(shallowMethod.apply(this, ...args))
}
I found way to solve this, looks ugly, but works. Open for better solution
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.
I returned to this PR and want to finish it before this weekend because I want to get initial review on it. I fixed all elements
tests, will back to all other tomorrow.
@levithomason I didn't finished this PR, but I want to get your review. There are many things that don't make me happy. getElementTypeNow it returns an withRefThis hoc contains magic conditions. I'm not sure that it's the best idea, but calls of TestsMost of test are broken because now we get
@levithomason I can fix all left tests, but I need comments and signal from you, that I've chosen the right way 😄 |
src/hoc/withRef.js
Outdated
|
||
import Ref from '../addons/Ref' | ||
|
||
const withRef = Child => class refHOC extends Component { |
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.
Capitalized classes, please.
nit: Could also just be WithRef
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.
Nice catch 👍
src/lib/getElementType.js
Outdated
@@ -19,7 +21,7 @@ function getElementType(Component, props, getDefault) { | |||
// computed default element type | |||
|
|||
if (getDefault) { | |||
const computedDefault = getDefault() | |||
const computedDefault = typeof getDefault === 'function' ? getDefault() : getDefault |
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.
If this change is made, the signature needs to be updated as well.
* @param {function} [getDefault] A function that returns a default element type.
That said, I'm not sure there is too much advantage to allowing a conditional here.
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.
Yes, I used original
as you wrote below, so this change was reverted.
As for the image example, this is actually a bug :) The 3rd argument is a function: -const ElementType = getElementType(Image, props, wrappedImage && 'div')
+const ElementType = getElementType(Image, props, () => wrappedImage && 'div') getElementType.js * @param {function} [getDefault] A function that returns a default element type. As for the list item example, I'm not entirely sure right now. One idea, but feels hacky :( is to have <Button as='div' />
const Button (props) => {
const ElementType = getElementType(Button, props)
// given the above invocation:
// ElementType.originalComponent === 'div'
return <ElementType />
} I'm not super fond of this, but it seems like one of the only possibilities for "unwrapping" the HOC and accessing the original component. |
I will have to come back and address the others another time! |
Note, |
Requires #2142. |
Unblocked, has some conflicts. |
…-Org/Semantic-UI-React into feat/ref # Conflicts: # src/addons/Ref/Ref.d.ts # src/addons/Ref/Ref.js # src/collections/Breadcrumb/BreadcrumbSection.js # src/index.js # test/specs/addons/Ref/Ref-test.js # test/specs/addons/Ref/fixtures.js
2317aa1
to
11fef64
Compare
innerRef
on all our componentsinnerRef
on all components
Needs #2144. |
#2144 is now unblocked. |
Note, #2144 was merged and this PR should be clear to move forward. |
fc6cee3
to
20bbbfa
Compare
WIP
Fixes #1321.
Why?
ref
isn't a prop, it has a same behaviour askey
- it never passed to a component. However, there were numerous issues about refs (#1740, #1602, #1849, #1732 and more) and it's time to make something in this way.document.querySelector
is a workaround, but it's not a solution.Problem is a well known in React's world, there is a long-live issue facebook/react#4213, there was also the RFC facebook/react#6974 from Dan.
Variants
componentRef
&innerRef
refs.md
componentRef
connect
HOC,withRef
innerRef
Link.md
TODO
ref
andtrigger
usagescontext
andscrollContext
usages