Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@ ReactDOM.render(
<td></td>
<td>specific the close icon.</td>
</tr>
<tr>
<td>forceRender</td>
<td>Boolean</td>
<td>false</td>
<td>Create dialog dom node before dialog first show </td>
</tr>
</tbody>
</table>

Expand Down
18 changes: 17 additions & 1 deletion examples/ant-design.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class MyControl extends React.Component {
center: false,
mousePosition: {},
useIcon: false,
forceRender: false,
};

onClick = e => {
Expand All @@ -49,7 +50,6 @@ class MyControl extends React.Component {
}

onClose = e => {
// console.log(e);
this.setState({
visible: false,
});
Expand All @@ -61,6 +61,12 @@ class MyControl extends React.Component {
});
}

onForceRenderChange = e => {
this.setState({
forceRender: e.target.checked,
});
}

changeWidth = () => {
this.setState({
width: this.state.width === 600 ? 800 : 600,
Expand Down Expand Up @@ -98,6 +104,7 @@ class MyControl extends React.Component {
mousePosition={this.state.mousePosition}
destroyOnClose={this.state.destroyOnClose}
closeIcon={this.state.useIcon ? getSvg(clearPath, {}, true) : undefined}
forceRender={this.state.forceRender}
>
<input autoFocus />
<p>basic modal</p>
Expand Down Expand Up @@ -141,6 +148,15 @@ class MyControl extends React.Component {
onChange={this.center}
/>
</label>
&nbsp;
<label>
force render
<input
type="checkbox"
checked={this.state.forceRender}
onChange={this.onForceRenderChange}
/>
</label>
</p>
{dialog}
</div>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rc-dialog",
"version": "7.2.1",
"version": "7.2.2",
"description": "dialog ui component for react",
"keywords": [
"react",
Expand Down
8 changes: 6 additions & 2 deletions src/Dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ export default class Dialog extends React.Component<IDialogPropTypes, any> {
}
componentDidMount() {
this.componentDidUpdate({});
// if forceRender is true, set element style display to be none;
if (this.props.forceRender && this.wrap) {
this.wrap.style.display = 'none';
}
}
componentDidUpdate(prevProps: IDialogPropTypes) {
const props = this.props;
Expand Down Expand Up @@ -207,7 +211,7 @@ export default class Dialog extends React.Component<IDialogPropTypes, any> {
</button>);
}

const style = { ... props.style, ...dest };
const style = { ...props.style, ...dest };
const sentinelStyle = { width: 0, height: 0, overflow: 'hidden' };
const transitionName = this.getTransitionName();
const dialogElement = (
Expand Down Expand Up @@ -261,7 +265,7 @@ export default class Dialog extends React.Component<IDialogPropTypes, any> {
}
return style;
}
getWrapStyle = () : any => {
getWrapStyle = (): any => {
return { ...this.getZIndexStyle(), ...this.props.wrapStyle };
}
getMaskStyle = () => {
Expand Down
14 changes: 8 additions & 6 deletions src/DialogWrap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import IDialogPropTypes from './IDialogPropTypes';
const IS_REACT_16 = 'createPortal' in ReactDOM;

class DialogWrap extends React.Component<IDialogPropTypes, any> {
static defaultProps = {
static defaultProps = {
visible: false,
forceRender: false,
};

_component: React.ReactElement<any>;
Expand All @@ -18,8 +19,8 @@ class DialogWrap extends React.Component<IDialogPropTypes, any> {

removeContainer: () => void;

shouldComponentUpdate({ visible }: { visible: boolean }) {
return !!(this.props.visible || visible);
shouldComponentUpdate({ visible, forceRender }: { visible: boolean, forceRender: boolean }) {
return !!(this.props.visible || visible) || (this.props.forceRender || forceRender);
}

componentWillUnmount() {
Expand All @@ -40,7 +41,7 @@ class DialogWrap extends React.Component<IDialogPropTypes, any> {

saveDialog = (node: any) => {
this._component = node;
}
}

getComponent = (extra = {}) => {
return (
Expand Down Expand Up @@ -71,7 +72,7 @@ class DialogWrap extends React.Component<IDialogPropTypes, any> {
}

render() {
const { visible } = this.props;
const { visible, forceRender } = this.props;

let portal: any = null;

Expand All @@ -83,6 +84,7 @@ class DialogWrap extends React.Component<IDialogPropTypes, any> {
autoDestroy={false}
getComponent={this.getComponent}
getContainer={this.getContainer}
forceRender={forceRender}
>
{({ renderComponent, removeContainer }: { renderComponent: any, removeContainer: any }) => {
this.renderComponent = renderComponent;
Expand All @@ -93,7 +95,7 @@ class DialogWrap extends React.Component<IDialogPropTypes, any> {
);
}

if (visible || this._component) {
if (visible || forceRender || this._component) {
portal = (
<Portal getContainer={this.getContainer}>
{this.getComponent()}
Expand Down
1 change: 1 addition & 0 deletions src/IDialogPropTypes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ interface IDialogPropTypes {
wrapProps?: any;
getContainer?: () => HTMLElement;
closeIcon?: ReactNode;
forceRender?: boolean;
}

export default IDialogPropTypes;
11 changes: 11 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,15 @@ describe('dialog', () => {
</Dialog>), container);
expect($('.rc-dialog').css('transform-origin')).to.not.be.empty();
});

it('can get dom element before dialog first show when forceRender is set true ',()=>{
const d = ReactDOM.render((
<Dialog
forceRender
>
<div>forceRender element</div>
</Dialog>
),container)
expect($('.rc-dialog-body > div').text()).to.be('forceRender element')
})
});