Home Reference Source

application/metaproperties/editortext-meta-property.js

  1. import React from 'react';
  2. import Codemirror from 'react-codemirror';
  3. import Subheader from 'material-ui/Subheader';
  4. require('codemirror/mode/htmlmixed/htmlmixed');
  5.  
  6.  
  7. class EditortextMetaProperty extends React.Component {
  8. constructor(props) {
  9. super(props);
  10. this.displayName = 'EditortextMetaProperty';
  11.  
  12. }
  13.  
  14. rando() {
  15. const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  16.  
  17. let str = [];
  18. for (let i=0; i<5; i++) {
  19. str.push(chars.charAt(Math.floor(Math.random() * chars.length)));
  20. }
  21.  
  22. return str.join('');
  23. }
  24.  
  25. shouldComponentUpdate(nextProps, nextState) {
  26. if (nextProps.value != this.props.value) {
  27. return true;
  28. }
  29. if (nextProps.disabled != this.props.disabled) {
  30. return true;
  31. }
  32. return false;
  33. }
  34.  
  35. handleEditorChange(val) {
  36. const propName = this.props.name;
  37.  
  38. let fakeEvent = {
  39. target: {
  40. getAttribute: function() {
  41. return propName;
  42. },
  43. value: val
  44. }
  45. };
  46. this.props.onChange(fakeEvent);
  47. }
  48.  
  49. render() {
  50. const cmOptions = {
  51. lineNumbers: true,
  52. mode: 'htmlmixed'
  53. };
  54. const name = this.props.name ? this.props.name : 'textfield_' + this.rando();
  55.  
  56. return (
  57. <div>
  58. <Subheader>{this.props.floatingLabelText}</Subheader>
  59. <Codemirror
  60. ref={name}
  61. value={this.props.value}
  62. onChange={this.handleEditorChange.bind(this)}
  63. options={cmOptions} />
  64. </div>
  65. );
  66. }
  67. }
  68.  
  69. export default EditortextMetaProperty;