-
Notifications
You must be signed in to change notification settings - Fork 20
Refactor/class to func component #44
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
Open
Vitao18
wants to merge
16
commits into
a-x-:master
Choose a base branch
from
Vitao18:refactor/class-to-func-component
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
1865f5a
refactor: NoPrint component
Vitao18 0a39b4c
refactor: PrintProvider component
Vitao18 3da9e0e
fix: export default PrintProvider
Vitao18 2162f84
fix: add missing export defaults
Vitao18 c9f6153
feat: split state into multiple useStates
Vitao18 19b46c0
fix: replace Object.assign with spread operator
Vitao18 888db31
fix: add current to 'printableRegistry' mutations
Vitao18 1656f05
NoPrint: keep component name in devtools by using named function
a-x- 0275379
Print: keep component name in devtools by using named function
a-x- c58fb54
PrintProviderContext: eof
a-x- 7aec3f1
PrintProvider: keep component name in devtools by using named function
a-x- b82b58d
PrintProvider: use uniq id 😳
a-x- 3f6e270
Print: add empy array to useEffect deps
Vitao18 bf1ccc2
PrintProvider: move useEffects to top and fix hasSingle validation
Vitao18 dc5c164
PrintProvider: update deps on useEffects
Vitao18 21555d9
PrintProvider: move back to Object.assign to avoid additional complexity
Vitao18 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
---|---|---|
@@ -1,125 +1,129 @@ | ||
import React from 'react'; | ||
import React, { useState, useEffect, useRef } from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import PropTypes from 'prop-types'; | ||
import PrintProviderContext from './PrintProviderContext'; | ||
import s from './PrintProvider.css'; | ||
import { spliced, debug } from './common'; | ||
|
||
export const propTypes = { | ||
loose: PropTypes.bool, | ||
children: PropTypes.node.isRequired, | ||
invert: PropTypes.bool, | ||
invert: PropTypes.bool | ||
}; | ||
|
||
export const childContextTypes = { | ||
printProvider: PropTypes.shape({ | ||
regPrintable: PropTypes.func.isRequired, | ||
unregPrintable: PropTypes.func.isRequired, | ||
isPrint: PropTypes.bool.isRequired, | ||
}).isRequired, | ||
const createRender = children => { | ||
const el = document.createElement('div'); | ||
el.id = 'render'; | ||
document.body.appendChild(el); | ||
ReactDOM.render(<div className={s.printRender}>{children}</div>, el); | ||
}; | ||
|
||
export default class PrintProvider extends React.PureComponent { | ||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
isInPrintPreview: false, | ||
printableNodes: [], | ||
}; | ||
this.printableRegistry = {}; | ||
window.matchMedia('print').onchange = () => { | ||
debug('toggle print mode', window.matchMedia('print').matches); | ||
this.setState({ isInPrintPreview: window.matchMedia('print').matches }); | ||
}; | ||
const deleteRender = () => { | ||
const el = document.getElementById('render'); | ||
el && document.body.removeChild(el); | ||
}; | ||
|
||
this.hasSingle = false; | ||
} | ||
const PrintProvider = props => { | ||
|
||
getChildContext() { | ||
return { | ||
printProvider: { | ||
isPrint: window.matchMedia('print').matches, | ||
regPrintable: this.regPrintable.bind(this), | ||
unregPrintable: this.unregPrintable.bind(this), | ||
}, | ||
}; | ||
} | ||
const [isInPrintPreview, setIsInPrintPreview] = useState(false); | ||
const [printableNodes, setPrintableNodes ] = useState([]); | ||
|
||
const printableRegistry = useRef({}); | ||
const hasSingle = useRef(false); | ||
|
||
// hideAll - is being used to cover all of React Portals, popups and modals and etc. | ||
hideAll() { | ||
const hideAll = () => { | ||
document.body.classList.add(s.hiddenAll); | ||
this.hasSingle = true; | ||
} | ||
hasSingle.current = true; | ||
}; | ||
|
||
unhideAll() { | ||
const unhideAll = () => { | ||
document.body.classList.remove(s.hiddenAll); | ||
this.hasSingle = false; | ||
} | ||
hasSingle.current = false; | ||
}; | ||
|
||
regPrintable(key, node) { | ||
const loose = this.props.loose || this.props.invert; | ||
const regPrintable = (key, node) => { | ||
const loose = props.loose || props.invert; | ||
const isSingle = node.props.single || node.props.main; | ||
const { hasSingle } = this; | ||
|
||
debug('reg printable', key, node); | ||
|
||
if (this.printableRegistry[key] !== undefined || loose) return; | ||
setTimeout(() => this.setState({ | ||
printableNodes: this.state.printableNodes.concat(node), | ||
}), 0); | ||
this.printableRegistry[key] = this.state.printableNodes.length; | ||
if (printableRegistry.current[key] !== undefined || loose) return; | ||
setTimeout( | ||
() => | ||
setPrintableNodes(printableNodes.concat(node)), | ||
0 | ||
); | ||
printableRegistry.current[key] = printableNodes.length; | ||
|
||
if (isSingle && !hasSingle) { | ||
this.hideAll(); | ||
hideAll(); | ||
} else if (isSingle) { | ||
console.warn(new Error('react-easy-print warning \n\t you\'re using more than one `single` Print component')); | ||
console.warn( | ||
new Error( | ||
'react-easy-print warning \n\t you\'re using more than one `single` Print component' | ||
) | ||
); | ||
} | ||
} | ||
|
||
unregPrintable(key, isSingle) { | ||
const loose = this.props.loose || this.props.invert; | ||
if (this.printableRegistry[key] === undefined || this.state.isInPrintPreview || loose) return; | ||
this.setState({ | ||
printableNodes: spliced(this.state.printableNodes, this.printableRegistry[key]), | ||
}); | ||
this.printableRegistry = Object.assign({}, this.printableRegistry, { [key]: undefined }); | ||
}; | ||
|
||
const unregPrintable = (key, isSingle) => { | ||
const loose = props.loose || props.invert; | ||
if ( | ||
printableRegistry[key] === undefined || | ||
isInPrintPreview || | ||
loose | ||
) | ||
return; | ||
setPrintableNodes( | ||
spliced( | ||
printableNodes, | ||
printableRegistry[key] | ||
) | ||
); | ||
printableRegistry.current = { | ||
...printableRegistry, | ||
a-x- marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[key]: undefined | ||
}; | ||
if (isSingle) { | ||
this.unhideAll(); | ||
unhideAll(); | ||
} | ||
} | ||
|
||
createRender(children) { | ||
const el = document.createElement('div'); | ||
el.id = 'render'; | ||
document.body.appendChild(el); | ||
|
||
ReactDOM.render(<div className={s.printRender} >{children}</div>, el); | ||
} | ||
}; | ||
|
||
deleteRender() { | ||
const el = document.getElementById('render'); | ||
el && document.body.removeChild(el); | ||
} | ||
|
||
render() { | ||
const { isInPrintPreview, printableNodes } = this.state; | ||
const loose = this.props.loose || this.props.invert; | ||
const { hasSingle } = this; | ||
useEffect(() => { | ||
window.matchMedia('print').onchange = () => { | ||
a-x- marked this conversation as resolved.
Show resolved
Hide resolved
|
||
debug('toggle print mode', window.matchMedia('print').matches); | ||
setIsInPrintPreview(window.matchMedia('print').matches); | ||
}; | ||
}); | ||
|
||
useEffect(() => { | ||
if (isInPrintPreview && printableNodes.length && !loose && !hasSingle) { | ||
debug('render printable only', printableNodes); | ||
const children = React.Children.map(printableNodes, (child, key) => { | ||
return React.cloneElement(child, { key }); | ||
}); | ||
|
||
this.createRender(<div>{children}</div>); | ||
createRender(<div>{children}</div>); | ||
} | ||
setTimeout(() => this.deleteRender(), 0); | ||
|
||
setTimeout(() => deleteRender(), 0); | ||
}); | ||
a-x- marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
useEffect(() => { | ||
debug('render everything', isInPrintPreview, printableNodes.length, !loose); | ||
const loose_ = loose ? s.loose : ''; | ||
const invert_ = this.props.invert ? s.invert : ''; | ||
return <div className={`${s.wrap} ${loose_} ${invert_} `}>{this.props.children}</div>; | ||
} | ||
} | ||
}); | ||
|
||
const loose = props.loose || props.invert; | ||
const loose_ = loose ? s.loose : ''; | ||
const invert_ = props.invert ? s.invert : ''; | ||
return ( | ||
<PrintProviderContext.Provider unregPrintable={unregPrintable} regPrintable={regPrintable}> | ||
<div className={`${s.wrap} ${loose_} ${invert_} `}> | ||
{props.children} | ||
</div> | ||
</PrintProviderContext.Provider> | ||
); | ||
}; | ||
|
||
export default PrintProvider; | ||
|
||
PrintProvider.propTypes = propTypes; | ||
PrintProvider.childContextTypes = childContextTypes; |
This file contains hidden or 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,15 @@ | ||
|
||
import React, { useContext } from 'react'; | ||
|
||
const PrintProviderContext = React.createContext(); | ||
|
||
export const usePrintProvider = () => { | ||
const { regPrintable, unregPrintable } = useContext(PrintProviderContext); | ||
|
||
return { | ||
regPrintable, | ||
unregPrintable | ||
}; | ||
}; | ||
|
||
export default PrintProviderContext; |
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.
looks like this useEffect should be componentDidMount equivalent, because, e.g. window.matchMedia('print').onchange must be called at once,
so, as I understand, we need add empty list of dependencies (
[]
) here:useEffect(() => {...}, []);
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.
also, we need to verify all the
useEffect
s' dependencies