| 1 |
import React, { Component, Fragment } from 'react'; |
| 2 |
import Select from 'react-select'; |
| 3 |
|
| 4 |
const thumbnailSizes = ! Array.isArray( wpupg_admin_template.thumbnail_sizes ) ? Object.values( wpupg_admin_template.thumbnail_sizes ) : wpupg_admin_template.thumbnail_sizes; |
| 5 |
|
| 6 |
export default class PropertyImageSize extends Component { |
| 7 |
constructor(props) { |
| 8 |
super(props); |
| 9 |
|
| 10 |
this.state = { |
| 11 |
width: '', |
| 12 |
height: '', |
| 13 |
} |
| 14 |
} |
| 15 |
|
| 16 |
componentDidMount() { |
| 17 |
this.checkSize(); |
| 18 |
} |
| 19 |
|
| 20 |
componentDidUpdate() { |
| 21 |
this.checkSize(); |
| 22 |
} |
| 23 |
|
| 24 |
checkSize() { |
| 25 |
const size = this.props.value; |
| 26 |
|
| 27 |
if ( '' !== size ) { |
| 28 |
const separator = size.indexOf('x'); |
| 29 |
|
| 30 |
let width = separator > 0 ? parseInt( size.substr(0, separator) ) : 0; |
| 31 |
let height = separator > 0 ? parseInt( size.substr(separator + 1) ) : 0; |
| 32 |
|
| 33 |
width = 0 < width ? width : ''; |
| 34 |
height = 0 < height ? height : ''; |
| 35 |
|
| 36 |
if ( width !== this.state.width || height !== this.state.height ) { |
| 37 |
this.setState({ |
| 38 |
width, |
| 39 |
height, |
| 40 |
}) |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
changeSize(property, value) { |
| 46 |
if ( 'width' === property || 'height' === property ) { |
| 47 |
let newState = this.state; |
| 48 |
newState[property] = parseInt( value ); |
| 49 |
|
| 50 |
this.setState(newState, () => { |
| 51 |
if ( 0 < this.state.width || 0 < this.state.height ) { |
| 52 |
this.props.onValueChange(`${this.state.width}x${this.state.height}`); |
| 53 |
} |
| 54 |
}); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
render() { |
| 59 |
let selectOptions = []; |
| 60 |
|
| 61 |
for (let thumbnail of thumbnailSizes) { |
| 62 |
selectOptions.push({ |
| 63 |
value: thumbnail, |
| 64 |
label: thumbnail, |
| 65 |
}); |
| 66 |
} |
| 67 |
|
| 68 |
return ( |
| 69 |
<Fragment> |
| 70 |
<label>Select existing thumbnail size:</label> |
| 71 |
<Select |
| 72 |
className="wpupg-template-property-input" |
| 73 |
menuPlacement="top" |
| 74 |
value={thumbnailSizes.includes(this.props.value) ? selectOptions.filter(({value}) => value === this.props.value) : ''} |
| 75 |
onChange={(option) => { |
| 76 |
if ( ! option ) { |
| 77 |
return this.props.onValueChange(''); |
| 78 |
} |
| 79 |
return this.props.onValueChange(option.value); |
| 80 |
}} |
| 81 |
options={selectOptions} |
| 82 |
clearable={true} |
| 83 |
/> |
| 84 |
<label>...or set a specific width and height:</label> |
| 85 |
<div className="wpupg-template-property-input-width-height"> |
| 86 |
<input |
| 87 |
className="wpupg-template-property-input" |
| 88 |
type="number" |
| 89 |
value={this.state.width} |
| 90 |
onChange={(e) => this.changeSize('width', e.target.value)} |
| 91 |
/> x <input |
| 92 |
className="wpupg-template-property-input" |
| 93 |
type="number" |
| 94 |
value={this.state.height} |
| 95 |
onChange={(e) => this.changeSize('height', e.target.value)} |
| 96 |
/> |
| 97 |
</div> |
| 98 |
</Fragment> |
| 99 |
); |
| 100 |
} |
| 101 |
} |