| 1 |
import React, { Component, Fragment } from 'react'; |
| 2 |
import SVG from 'react-inlinesvg'; |
| 3 |
|
| 4 |
export default class PropertyIcon extends Component { |
| 5 |
constructor(props) { |
| 6 |
super(props); |
| 7 |
|
| 8 |
this.state = { |
| 9 |
selectingIcon: false, |
| 10 |
} |
| 11 |
} |
| 12 |
|
| 13 |
render() { |
| 14 |
const customSelected = wpupg_admin_template.icons.hasOwnProperty(this.props.value); |
| 15 |
const iconUrl = customSelected ? wpupg_admin_template.icons[this.props.value].url : this.props.value; |
| 16 |
|
| 17 |
return ( |
| 18 |
<Fragment> |
| 19 |
{ |
| 20 |
! this.state.selectingIcon |
| 21 |
? |
| 22 |
<span className="wpupg-template-property-icon-selected-container"> |
| 23 |
{ |
| 24 |
iconUrl |
| 25 |
&& |
| 26 |
<SVG |
| 27 |
src={iconUrl} |
| 28 |
className="wpupg-template-property-icon-select" |
| 29 |
/> |
| 30 |
} |
| 31 |
<a href="#" onClick={(e) => { |
| 32 |
e.preventDefault(); |
| 33 |
this.setState({ |
| 34 |
selectingIcon: true, |
| 35 |
}); |
| 36 |
}}>{ iconUrl ? 'Change...' : 'Select...' }</a> |
| 37 |
</span> |
| 38 |
: |
| 39 |
<div className="wpupg-template-property-icon-select-container"> |
| 40 |
<a href="#" onClick={(e) => { |
| 41 |
e.preventDefault(); |
| 42 |
this.setState({ |
| 43 |
selectingIcon: false, |
| 44 |
}); |
| 45 |
|
| 46 |
return this.props.onValueChange(''); |
| 47 |
}}>Clear icon</a> | <a href="#" onClick={(e) => { |
| 48 |
e.preventDefault(); |
| 49 |
|
| 50 |
const url = prompt('Set a custom URL for the icon'); |
| 51 |
|
| 52 |
if ( url ) { |
| 53 |
this.setState({ |
| 54 |
selectingIcon: false, |
| 55 |
}); |
| 56 |
|
| 57 |
return this.props.onValueChange(url); |
| 58 |
} |
| 59 |
}}>Set custom URL</a> | Select: |
| 60 |
<div className="wpupg-template-property-icon-select-container-icons"> |
| 61 |
{ |
| 62 |
Object.keys(wpupg_admin_template.icons).map((id, index) => { |
| 63 |
let icon = wpupg_admin_template.icons[id]; |
| 64 |
return ( |
| 65 |
<span href="#" |
| 66 |
onClick={() => { |
| 67 |
this.setState({ |
| 68 |
selectingIcon: false, |
| 69 |
}); |
| 70 |
|
| 71 |
if ( icon.id !== this.props.value ) { |
| 72 |
return this.props.onValueChange(icon.id); |
| 73 |
} |
| 74 |
}} |
| 75 |
key={index} |
| 76 |
> |
| 77 |
<SVG |
| 78 |
src={icon.url} |
| 79 |
className={ icon.id === this.props.value ? 'wpupg-template-property-icon-select wpupg-template-property-icon-selected' : 'wpupg-template-property-icon-select' } |
| 80 |
/> |
| 81 |
</span> |
| 82 |
); |
| 83 |
}) |
| 84 |
} |
| 85 |
</div> |
| 86 |
</div> |
| 87 |
} |
| 88 |
</Fragment> |
| 89 |
); |
| 90 |
} |
| 91 |
} |