This repository was archived by the owner on Oct 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathImageSelectWrapper.js
85 lines (79 loc) · 1.98 KB
/
ImageSelectWrapper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import React, { Fragment } from "react";
import { ImageSelect } from "@yoast/components";
/**
* @returns {void} Void.
*/
class ImageSelectWrapper extends React.Component {
/**
* The constructor.
*
* @param {Object} props The props.
*/
constructor( props ) {
super( props );
this.state = {
imageUrl: "",
hasPreview: true,
warnings: [],
};
this.setImageUrl = this.setStateAttribute.bind( this, "imageUrl" );
this.changePreview = this.setStateAttribute.bind( this, "hasPreview" );
this.toggleWarning = this.toggleWarning.bind( this );
}
/**
* State setting function.
*
* @param {Object} attr Attributes.
* @param {string} value Value.
*
* @returns {void} Void.
*/
setStateAttribute( attr, value ) {
this.setState( state => ( {
...state,
[ attr ]: value,
} ) );
}
/**
* Set filled array if warnings is empty. Empty array if warnings is full.
*
* @returns {void} Void.
*/
toggleWarning() {
if ( this.state.warnings.length === 0 ) {
this.setState( { warnings: [ "Oops! Something's wrong!" ] } );
return;
}
this.setState( { warnings: [] } );
}
/**
* Renders the ImageSelect.
*
* @returns {*} Void.
*/
render() {
return (
<Fragment>
<button onClick={ () => this.toggleWarning() }>Toggle warning</button>
<button onClick={ () => this.changePreview( ! this.state.hasPreview ) }>Toggle preview</button>
<div className="yoast">
<h2>ImageSelect</h2>
<ImageSelect
imageUrlInputId="test-image"
imageUrl={ this.state.imageUrl }
imageSelected={ !! this.state.imageUrl }
hasPreview={ this.state.hasPreview }
label={ "Organization" }
selectImageButtonId="bla1"
replaceImageButtonId="bla2"
removeImageButtonId="bla3"
onClick={ () => this.setImageUrl( "http://placekitten.com/g/200/300" ) }
onRemoveImageClick={ () => this.setImageUrl( "" ) }
warnings={ this.state.warnings }
/>
</div>
</Fragment>
);
}
};
export default ImageSelectWrapper;