EditorOptionsSelector.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import FormGroup from 'react-bootstrap/es/FormGroup';
  4. import FormControl from 'react-bootstrap/es/FormControl';
  5. import ControlLabel from 'react-bootstrap/es/ControlLabel';
  6. import Button from 'react-bootstrap/es/Button';
  7. export default class EditorOptionsSelector extends React.Component {
  8. constructor(props) {
  9. super(props);
  10. this.state = {
  11. options: this.props.options,
  12. }
  13. this.availableThemes = [
  14. 'elegant', 'neo', 'mdn-like', 'material', 'monokai', 'twilight'
  15. ]
  16. this.onChangeTheme = this.onChangeTheme.bind(this);
  17. this.onClickStyleActiveLine = this.onClickStyleActiveLine.bind(this);
  18. }
  19. componentDidMount() {
  20. this.init();
  21. }
  22. init() {
  23. this.themeSelectorInputEl.value = this.state.options.theme || this.availableThemes[0];
  24. }
  25. onChangeTheme() {
  26. const newValue = this.themeSelectorInputEl.value;
  27. const newOpts = Object.assign(this.state.options, {theme: newValue});
  28. this.setState({options: newOpts});
  29. // dispatch event
  30. this.dispatchOnChange();
  31. }
  32. onClickStyleActiveLine(event) {
  33. const newValue = !this.state.options.styleActiveLine;
  34. console.log(newValue);
  35. const newOpts = Object.assign(this.state.options, {styleActiveLine: newValue});
  36. this.setState({options: newOpts});
  37. // dispatch event
  38. this.dispatchOnChange();
  39. }
  40. dispatchOnChange() {
  41. if (this.props.onChange != null) {
  42. this.props.onChange(this.state.options);
  43. }
  44. }
  45. renderThemeSelector() {
  46. const optionElems = this.availableThemes.map((theme) => {
  47. return <option key={theme} value={theme}>{theme}</option>;
  48. });
  49. return (
  50. <FormGroup controlId="formControlsSelect">
  51. <ControlLabel>Theme:</ControlLabel>
  52. <FormControl componentClass="select" placeholder="select"
  53. onChange={this.onChangeTheme}
  54. inputRef={ el => this.themeSelectorInputEl=el }>
  55. {optionElems}
  56. </FormControl>
  57. </FormGroup>
  58. )
  59. }
  60. renderStyleActiveLineSelector() {
  61. const bool = this.state.options.styleActiveLine || false;
  62. return (
  63. <FormGroup controlId="formControlsSelect">
  64. <Button active={bool} className="btn-style-active-line"
  65. onClick={this.onClickStyleActiveLine}
  66. ref="styleActiveLineButton">
  67. Active Line
  68. </Button>
  69. </FormGroup>
  70. )
  71. }
  72. render() {
  73. return <span>{this.renderThemeSelector()} {this.renderStyleActiveLineSelector()}</span>
  74. }
  75. }
  76. EditorOptionsSelector.propTypes = {
  77. options: PropTypes.object,
  78. onChange: PropTypes.func,
  79. };