-
-
Notifications
You must be signed in to change notification settings - Fork 112
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: implement ShowIf component #2300
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
89da1a7
feat: implement ShowIf component
HellWolf93 6261767
test: add tests to `getElementDimensions`
HellWolf93 9ef82d3
docs: add accordion example to ShowIf
HellWolf93 18d0d84
fix: add component thumb
HellWolf93 914bcfd
docs: update examples
63b1417
fix: add horizontal slide
HellWolf93 dc4d29d
docs: add new examples to ShowIf
HellWolf93 c95ecf8
Merge branch 'master' into implement-ShowIf
TahimiLeonBravo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import React from 'react'; | ||
import { mount } from 'enzyme'; | ||
import ShowIf from '..'; | ||
|
||
describe('<ShowIf />', () => { | ||
it('should render the children in the DOM when isTrue is false', () => { | ||
const component = mount( | ||
<ShowIf> | ||
<span data-id="test" /> | ||
</ShowIf>, | ||
); | ||
expect(component.find('[data-id="test"]').exists()).toBe(true); | ||
}); | ||
}); |
64 changes: 64 additions & 0 deletions
64
src/components/ShowIf/helpers/__test__/getElementDimensions.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* eslint-disable func-names */ | ||
/* eslint-disable object-shorthand */ | ||
import getElementDimensions from '../getElementDimensions'; | ||
|
||
describe('getElementDimensions', () => { | ||
it('should return the right dimensions', () => { | ||
const dimensions = { width: 50, height: 50 }; | ||
const element = { | ||
style: { | ||
position: 'relative', | ||
display: 'none', | ||
visibility: 'hidden', | ||
}, | ||
getBoundingClientRect: () => dimensions, | ||
}; | ||
expect(getElementDimensions(element)).toEqual(dimensions); | ||
}); | ||
|
||
it('should modify the styles to get client rect correctly', () => { | ||
const dimensions = { width: 50, height: 50 }; | ||
const element = { | ||
style: { | ||
position: 'relative', | ||
display: 'none', | ||
visibility: 'hidden', | ||
}, | ||
getBoundingClientRect: function() { | ||
const { position, visibility, display } = this.style; | ||
if (position === 'absolute' && visibility === 'hidden' && display === 'block') { | ||
return dimensions; | ||
} | ||
return undefined; | ||
}, | ||
}; | ||
getElementDimensions(element); | ||
expect(element.style).toEqual({ | ||
position: 'relative', | ||
display: 'none', | ||
visibility: 'hidden', | ||
}); | ||
}); | ||
|
||
it('should leave the element styles as passed', () => { | ||
const dimensions = { width: 50, height: 50 }; | ||
const element = { | ||
style: { | ||
position: 'relative', | ||
display: 'none', | ||
visibility: 'hidden', | ||
}, | ||
getBoundingClientRect: () => dimensions, | ||
}; | ||
getElementDimensions(element); | ||
expect(element.style).toEqual({ | ||
position: 'relative', | ||
display: 'none', | ||
visibility: 'hidden', | ||
}); | ||
}); | ||
|
||
it('should return undefined when there is no element', () => { | ||
expect(getElementDimensions(null)).toBe(undefined); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* eslint-disable no-param-reassign */ | ||
const getElementBoundingClientRect = element => { | ||
if (element) { | ||
const { position, visibility, display } = element.style; | ||
element.style.position = 'absolute'; | ||
element.style.visibility = 'hidden'; | ||
element.style.display = 'block'; | ||
const rect = element.getBoundingClientRect(); | ||
element.style.position = position; | ||
element.style.visibility = visibility; | ||
element.style.display = display; | ||
return rect; | ||
} | ||
return undefined; | ||
}; | ||
|
||
export default getElementBoundingClientRect; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { ReactNode } from 'react'; | ||
import { BaseProps } from '../types'; | ||
|
||
type AnimationName = 'fade' | 'slide'; | ||
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. here we need to specify if the slide will be from right, left, top, bottom |
||
|
||
export interface ShowIfProps extends BaseProps { | ||
isTrue?: any; | ||
inAnimation?: AnimationName; | ||
outAnimation?: AnimationName; | ||
children?: ReactNode; | ||
} | ||
|
||
export default function(props: ShowIfProps): JSX.Element | null; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import React, { useLayoutEffect, useRef, useState } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import AnimatedContainer from './styled'; | ||
import getElementBoundingClientRect from './helpers/getElementDimensions'; | ||
|
||
/** | ||
* A component that shows its contents when a condition is met. Works similar to `RenderIf`, | ||
* but `ShowIf` does not remove the elements from the DOM, and animates the content in and out. | ||
*/ | ||
const ShowIf = ({ id, className, style, isTrue, inAnimation, outAnimation, children }) => { | ||
const [animation, setAnimation] = useState(); | ||
const [isVisible, setIsVisible] = useState(isTrue); | ||
const [dimensions, setDimensions] = useState(); | ||
const ref = useRef(); | ||
|
||
useLayoutEffect(() => { | ||
if (isTrue) { | ||
const rect = getElementBoundingClientRect(ref.current); | ||
setDimensions(rect); | ||
setIsVisible(true); | ||
setAnimation(`${inAnimation}In`); | ||
} else { | ||
setAnimation(`${outAnimation}Out`); | ||
} | ||
}, [isTrue, inAnimation, outAnimation]); | ||
|
||
const handleAnimationEnd = event => { | ||
if (event.animationName.includes('Out')) { | ||
setIsVisible(false); | ||
} | ||
}; | ||
|
||
return ( | ||
<AnimatedContainer | ||
id={id} | ||
className={className} | ||
style={style} | ||
animation={animation} | ||
isVisible={isVisible} | ||
dimensions={dimensions} | ||
onAnimationEnd={handleAnimationEnd} | ||
ref={ref} | ||
> | ||
{children} | ||
</AnimatedContainer> | ||
); | ||
}; | ||
|
||
ShowIf.propTypes = { | ||
/** The id of the outer element. */ | ||
id: PropTypes.string, | ||
/** A CSS class for the outer element, in addition to the component's base classes. */ | ||
className: PropTypes.string, | ||
/** An object with custom style applied for the outer element. */ | ||
style: PropTypes.object, | ||
/** Indicates whether the component content is showed or not. If is set to true, then is showed the component content. */ | ||
isTrue: PropTypes.any, | ||
inAnimation: PropTypes.oneOf(['fade', 'slide']), | ||
outAnimation: PropTypes.oneOf(['fade', 'slide']), | ||
/** The content of the component. */ | ||
children: PropTypes.oneOfType([ | ||
PropTypes.arrayOf(PropTypes.node), | ||
PropTypes.object, | ||
PropTypes.node, | ||
]), | ||
}; | ||
|
||
ShowIf.defaultProps = { | ||
id: undefined, | ||
className: undefined, | ||
style: undefined, | ||
isTrue: false, | ||
inAnimation: 'fade', | ||
outAnimation: 'fade', | ||
children: [], | ||
}; | ||
|
||
export default ShowIf; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Similar blocks of code found in 2 locations. Consider refactoring.