spee.ch/react/containers/PublishMetadataInputs.jsx

121 lines
4.4 KiB
React
Raw Normal View History

2018-01-06 03:26:57 +01:00
import React from 'react';
2018-01-10 22:10:08 +01:00
import { connect } from 'react-redux';
import { updateMetadata } from '../actions/index';
2018-01-06 03:26:57 +01:00
2018-01-11 02:41:17 +01:00
/*
const textarea = document.getElementById('publish-description');
const limit = 200;
textarea.oninput = () => {
2018-01-17 18:24:17 +01:00
textarea.style.height = '';
textarea.style.height = Math.min(textarea.scrollHeight, limit) + 'px';
}
2018-01-11 02:41:17 +01:00
*/
2018-01-06 03:26:57 +01:00
class MetadataInputs extends React.Component {
constructor (props) {
super(props);
this.state = {
2018-01-17 18:24:17 +01:00
showInputs : false,
descriptionLimit : 100,
descriptionHeight: '',
2018-01-06 03:26:57 +01:00
};
this.toggleShowInputs = this.toggleShowInputs.bind(this);
2018-01-17 18:24:17 +01:00
this.handleDescriptionInput = this.handleDescriptionInput.bind(this);
this.handleNsfwCheck = this.handleNsfwCheck.bind(this);
this.handleLicenseSelection = this.handleLicenseSelection.bind(this);
this.setDescriptionTextBoxHeight = this.setDescriptionTextBoxHeight.bind(this);
2018-01-06 03:26:57 +01:00
}
toggleShowInputs () {
2018-01-10 22:10:08 +01:00
this.setState({'showInputs': !this.state.showInputs});
2018-01-06 03:26:57 +01:00
}
2018-01-17 18:24:17 +01:00
handleDescriptionInput (event) {
2018-01-08 18:39:59 +01:00
event.preventDefault();
2018-01-10 22:10:08 +01:00
const name = event.target.name;
const value = event.target.value;
this.props.onMetadataChange(name, value);
2018-01-17 18:24:17 +01:00
this.setDescriptionTextBoxHeight(event);
}
setDescriptionTextBoxHeight (event) {
const scrollHeight = event.target.scrollHeight;
// console.log('scrollHeight:', scrollHeight);
const height = Math.min(scrollHeight, this.state.descriptionLimit) + 'px';
this.setState({descriptionHeight: height});
2018-01-10 22:10:08 +01:00
}
2018-01-17 18:24:17 +01:00
handleNsfwCheck (event) {
2018-01-10 22:10:08 +01:00
console.log('handle input', event);
event.preventDefault();
const name = event.target.name;
const value = event.target.checked;
this.props.onMetadataChange(name, value);
2018-01-08 18:39:59 +01:00
}
2018-01-17 18:24:17 +01:00
handleLicenseSelection (event) {
2018-01-10 22:10:08 +01:00
const name = event.target.name;
2018-01-09 02:06:31 +01:00
const selectedOption = event.target.selectedOptions[0].value;
2018-01-10 22:10:08 +01:00
this.props.onMetadataChange(name, selectedOption);
2018-01-09 02:06:31 +01:00
}
2018-01-06 03:26:57 +01:00
render () {
return (
<div id="publish-details" className="row row--padded row--no-top row--wide">
<a className="label link--primary" id="publish-details-toggle" href="#" onClick={this.toggleShowInputs}>{this.state.showInputs ? '[less]' : '[more]'}</a>
{this.state.showInputs && (
<div>
<div className="row row--no-top">
<div className="column column--3 column--med-10 align-content-top">
<label htmlFor="publish-license" className="label">Description:</label>
</div><div className="column column--7 column--sml-10">
2018-01-17 00:55:29 +01:00
<textarea
rows="1"
id="publish-description"
className="textarea textarea--primary textarea--full-width"
name="description"
placeholder="Optional description"
value={this.props.description}
2018-01-17 18:24:17 +01:00
style={{height: this.state.descriptionHeight}}
onChange={this.handleDescriptionInput} />
2018-01-08 18:39:59 +01:00
</div>
</div>
2018-01-08 18:39:59 +01:00
<div className="row row--no-top">
<div className="column column--3 column--med-10">
<label htmlFor="publish-license" className="label">License:</label>
</div><div className="column column--7 column--sml-10">
2018-01-17 18:24:17 +01:00
<select type="text" name="license" id="publish-license" className="select select--primary" onChange={this.handleLicenseSelection}>
<option value=" ">Unspecified</option>
<option value="Public Domain">Public Domain</option>
<option value="Creative Commons">Creative Commons</option>
</select>
2018-01-08 18:39:59 +01:00
</div>
</div>
2018-01-08 18:39:59 +01:00
<div className="row row--no-top">
<div className="column column--3">
<label htmlFor="publish-nsfw" className="label">Mature:</label>
</div><div className="column column--7">
2018-01-17 18:24:17 +01:00
<input className="input-checkbox" type="checkbox" id="publish-nsfw" name="nsfw" checked={this.props.nsfw} onChange={this.handleNsfwCheck} />
2018-01-08 18:39:59 +01:00
</div>
2018-01-06 03:26:57 +01:00
</div>
</div>
)}
2018-01-06 03:26:57 +01:00
</div>
);
}
}
2018-01-10 22:10:08 +01:00
const mapStateToProps = state => {
return {
description: state.metadata.description,
license : state.metadata.license,
nsfw : state.metadata.nsfw,
};
};
const mapDispatchToProps = dispatch => {
return {
onMetadataChange: (name, value) => {
dispatch(updateMetadata(name, value));
},
};
};
export default connect(mapStateToProps, mapDispatchToProps)(MetadataInputs);