Skip to content

Commit 6f73b8b

Browse files
nirmaozNir Maoz
authored andcommitted
Fix image component responsive prop
1 parent fe7a5e0 commit 6f73b8b

File tree

5 files changed

+20
-8
lines changed

5 files changed

+20
-8
lines changed

src/components/CloudinaryComponent/CloudinaryComponent.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ const camelCase = Util.camelCase;
1212
* @returns {object} an object with copied values
1313
*/
1414
function only(source, keys = []) {
15-
if(!source) return source
15+
if(!source){
16+
return source;
17+
}
18+
1619
return keys.reduce((tr, key) => {
1720
if (key in source) {
1821
tr[key] = source[key]
@@ -28,12 +31,17 @@ function only(source, keys = []) {
2831
class CloudinaryComponent extends PureComponent {
2932
constructor(props, context) {
3033
super(props, context);
34+
this.getContext = this.getContext.bind(this);
3135
}
3236

3337
render() {
3438
return null;
3539
}
3640

41+
getContext(){
42+
return this.context || {};
43+
}
44+
3745
getChildTransformations(children) {
3846
if(children === undefined || children === null) return null;
3947
let mapped = React.Children.map(children, child =>{

src/components/CloudinaryContext/CloudinaryContext.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ class CloudinaryContext extends CloudinaryComponent {
2020
}
2121

2222
render() {
23-
const context = this.context || {};
24-
const props = {...context, ...this.props};
23+
const props = {...this.getContext(), ...this.props};
2524

2625
const {children, cloudinaryProps, nonCloudinaryProps, cloudinaryReactProps} = extractCloudinaryProps(props);
2726

src/components/Image/Image.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Image extends CloudinaryComponent {
3434
return (this.element && this.element.ownerDocument) ? (this.element.ownerDocument.defaultView || windowRef) : windowRef;
3535
}
3636

37-
prepareState(props = this.props, context = this.context) {
37+
prepareState(props = this.props, context = this.getContext()) {
3838
let extendedProps = CloudinaryComponent.normalizeOptions(context, props);
3939
let url = this.getUrl(extendedProps);
4040
let state = {};
@@ -88,7 +88,7 @@ class Image extends CloudinaryComponent {
8888
componentDidUpdate(prevProps) {
8989
this.setState(this.prepareState());
9090
if (this.state.responsive) {
91-
const wait = firstDefined(this.props.responsiveDebounce, this.context.responsiveDebounce, 100);
91+
const wait = firstDefined(this.props.responsiveDebounce, this.getContext().responsiveDebounce, 100);
9292
if (this.listener) {
9393
this.window && this.window.removeEventListener('resize', this.listener);
9494
}
@@ -99,7 +99,7 @@ class Image extends CloudinaryComponent {
9999

100100
render() {
101101
const {publicId, responsive, responsiveDebounce, children, ...options} =
102-
CloudinaryComponent.normalizeOptions(this.props, this.context);
102+
CloudinaryComponent.normalizeOptions(this.props, this.getContext());
103103
const attributes = cloudinary.Transformation.new(options).toHtmlAttributes();
104104
const {url} = this.state;
105105
return (
@@ -125,7 +125,7 @@ class Image extends CloudinaryComponent {
125125
};
126126

127127
applyBreakpoints(width, steps, options) {
128-
options = CloudinaryComponent.normalizeOptions(this.context, this.props, options);
128+
options = CloudinaryComponent.normalizeOptions(this.getContext(), this.props, options);
129129
let responsiveUseBreakpoints = options.responsiveUseBreakpoints;
130130
if ((!responsiveUseBreakpoints) || (responsiveUseBreakpoints === 'resize' && !options.resizing)) {
131131
return width;

src/components/Video/Video.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Video extends CloudinaryComponent {
1919

2020
render() {
2121
let {publicId, poster, sourceTypes, fallback, sourceTransformation: sourceTransformations, ...options} = Object.assign({},
22-
this.context,
22+
this.getContext(),
2323
this.props);
2424
sourceTransformations = sourceTransformations || {};
2525
sourceTypes = sourceTypes || Cloudinary.DEFAULT_VIDEO_PARAMS.source_types;

test/ImageTest.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,9 @@ describe('Image', () => {
125125

126126
expect(tag.find('img').prop('src')).to.match(/fn_wasm:blur.wasm\/sample/);
127127
});
128+
it('should support responsive prop', () => {
129+
let tag = mount(<Image publicId="sample" cloudName="demo"/>);
130+
tag.setProps({responsive:true});
131+
expect(tag.find('img').prop('src')).to.equal('http://res.cloudinary.com/demo/image/upload/sample');
132+
});
128133
});

0 commit comments

Comments
 (0)