Skip to content

Commit e5b4a75

Browse files
author
ziyingmai
committed
feat:add get image ref support
1 parent 1d4e8ce commit e5b4a75

File tree

7 files changed

+1350
-153
lines changed

7 files changed

+1350
-153
lines changed

Docs/Api.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,3 +469,18 @@ a callback on process finished
469469
}}
470470
/>
471471
```
472+
473+
### getImageRef
474+
475+
**Type** - `function`
476+
477+
**Example** -
478+
479+
get image ref
480+
481+
```jsx
482+
<ProcessImage
483+
image={src}
484+
getImageRef={image=>this.image=image}
485+
/>
486+
```

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,21 @@
1919
**Example**
2020

2121
```jsx
22+
23+
const mix = {
24+
color: 'mistyrose',
25+
amount: 10
26+
}
27+
2228
class App extends React.Component {
2329
state = { src: '', err: null }
24-
2530
render() {
26-
const params = {
27-
mix: {
28-
color: 'mistyrose',
29-
amount: 10
30-
}
31-
}
3231
return (
3332
<ProcessImage
3433
image='http://365.unsplash.com/assets/paul-jarvis-9530891001e7f4ccfcef9f3d7a2afecd.jpg'
35-
colors={params}
34+
colors={{
35+
mix
36+
}}
3637
resize={{ width: 500, height: 500, mode: 'bilinear' }}
3738
processedImage={(src, err) => this.setState({ src, err, })}
3839
/>

public/App.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class App extends Component {
3333
amount: this.state.mixAmount
3434
}
3535
}}
36+
getImageRef={element => (this.image = element)}
3637
/>
3738
<button
3839
disabled={this.state.isProcessing}
@@ -57,6 +58,13 @@ class App extends Component {
5758
>
5859
test2
5960
</button>
61+
<button
62+
onClick={() => {
63+
console.log(this.image);
64+
}}
65+
>
66+
log ImageRef
67+
</button>
6068
</div>
6169
);
6270
}

src/components/ProcessImage.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { Component } from 'react';
1+
import React, { Component, forwardRef } from 'react';
22
import ProgressiveImage from 'react-progressive-image';
33
import size from 'browser-image-size';
44
import work from 'webworkify-webpack';
@@ -146,8 +146,8 @@ class ProcessImage extends Component {
146146

147147
worker = null;
148148

149-
processedImage = (image, restProps, style) => (
150-
<img src={image} {...restProps} style={style} />
149+
processedImage = (image, restProps, style, getImageRef) => (
150+
<img src={image} {...restProps} style={style} ref={getImageRef} />
151151
);
152152

153153
placeholderImage = image =>
@@ -161,7 +161,12 @@ class ProcessImage extends Component {
161161
placeholder={this.placeholderImage(props.image)}
162162
>
163163
{image =>
164-
this.processedImage(image, restProps, this.getDefaultImageSize(props))
164+
this.processedImage(
165+
image,
166+
restProps,
167+
this.getDefaultImageSize(props),
168+
props.getImageRef
169+
)
165170
}
166171
</ProgressiveImage>
167172
);

src/utils/propsFactory.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ const getImageProps = props => {
8282
disableRerender,
8383
customCdn,
8484
onProcessFinish,
85+
getImageRef,
8586
...rest
8687
} = props;
8788

src/validators/props.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ const MainPropTypes = {
9494
scaleToFit: scaleToFitPropType,
9595
disableRerender: PropTypes.bool,
9696
customCdn: PropTypes.string,
97-
onProcessFinish: PropTypes.func
97+
onProcessFinish: PropTypes.func,
98+
getImageRef: PropTypes.func
9899
};
99100

100101
export default MainPropTypes;

0 commit comments

Comments
 (0)