Created
October 24, 2017 15:14
-
-
Save domtalbot/4f222a2acd2205a0b1cb61b62c2aa46d to your computer and use it in GitHub Desktop.
Control
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component, PropTypes } from 'react'; | |
import { connect } from 'react-redux'; | |
import { Map } from 'immutable'; | |
import CMS from 'netlify-cms'; | |
export default class DynamicControl extends Component { | |
static propTypes = { | |
onChange: PropTypes.func.isRequired, | |
onAddAsset: PropTypes.func.isRequired, | |
onRemoveAsset: PropTypes.func.isRequired, | |
getAsset: PropTypes.func.isRequired, | |
value: PropTypes.oneOfType([ | |
PropTypes.node, | |
PropTypes.object, | |
PropTypes.bool, | |
]), | |
field: PropTypes.object, | |
forID: PropTypes.string, | |
dynamicWidgets: PropTypes.object | |
}; | |
constructor(props) { | |
super(props); | |
const fieldValue = this.props.value && Map.isMap(this.props.value) ? | |
this.props.value.get(this.props.field.get('name')) : | |
this.props.value; | |
if (!fieldValue) { | |
this.state = { | |
widget: null, | |
}; | |
} else { | |
this.state = { | |
widget: CMS.getWidget(fieldValue), | |
}; | |
} | |
} | |
handleChange = (e) => { | |
console.log(e); | |
this.props.onChange(Map().set(e.target.id, e.target.value)); | |
if (!e.target.value) { | |
this.setState({ | |
widget: 'string', | |
}); | |
} else { | |
this.setState({ | |
widget: CMS.getWidget(e.target.value), | |
}); | |
} | |
}; | |
altChange = (target, val) => { | |
this.props.onChange(this.props.value.set(target, val)); | |
} | |
render() { | |
const { field, value, forID, onChange, onAddAsset, onRemoveAsset, getAsset, dynamicWidgets } = this.props; | |
const { widget } = this.state; | |
const name = field.get('name'); | |
const selectedName = `${ field.get('name') }_selected`; | |
const selectedNameAlt = `${ field.get('name') }_selected_alt`; | |
const fieldValue = value && Map.isMap(value) ? | |
value.get(name) : | |
value; | |
const fieldValueSelected = value && Map.isMap(value) ? | |
value.get(selectedName) : | |
value; | |
const fieldValueSelectedAlt = value && Map.isMap(value) ? | |
value.get(selectedNameAlt) : | |
value; | |
let options = field.get('dynamicWidgets').map((option) => { | |
if (typeof option === 'string') { | |
return { label: option, value: option }; | |
} | |
return option; | |
}); | |
options = options.insert(0, { | |
label: 'Please Select', | |
value: '', | |
}); | |
return ( | |
<div> | |
<div> | |
<select id={forID} value={fieldValue || ''} onChange={this.handleChange}> | |
{options.map((option, idx) => <option key={idx} value={option.value}> | |
{option.label} | |
</option>)} | |
</select> | |
</div> | |
<div> | |
{ | |
widget ? | |
<div key={selectedName}> | |
<div key={selectedName}> | |
<label htmlFor={selectedName}>{`${ field.get('label') } Data`}</label> | |
{ | |
React.createElement(widget.control, { | |
id: selectedName, | |
field, | |
value: fieldValueSelected, | |
onChange: (val, metadata) => { | |
console.log(val); | |
console.log(value); | |
onChange((value || Map()).set(selectedName, val), metadata); | |
}, | |
onAddAsset, | |
onRemoveAsset, | |
getAsset, | |
forID: selectedName, | |
}) | |
} | |
</div> | |
</div> | |
: | |
'' | |
} | |
</div> | |
<div> | |
{ | |
fieldValue === 'image' ? | |
<div key={selectedNameAlt}> | |
<div key={selectedNameAlt}> | |
<label htmlFor={selectedNameAlt}>{`${ field.get('label') } Data Alt`}</label> | |
<input type="text" id={selectedNameAlt} value={fieldValueSelectedAlt || ''} onChange={(e) => { this.altChange(selectedNameAlt, e.target.value) }}/> | |
</div> | |
</div> | |
: | |
'' | |
} | |
</div> | |
</div> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment