SubmissionCommentForm.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (C) 2017 - present Instructure, Inc.
  3. *
  4. * This file is part of Canvas.
  5. *
  6. * Canvas is free software: you can redistribute it and/or modify it under
  7. * the terms of the GNU Affero General Public License as published by the Free
  8. * Software Foundation, version 3 of the License.
  9. *
  10. * Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
  11. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. * A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. * details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License along
  16. * with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. import $ from 'jquery';
  19. import 'compiled/jquery.rails_flash_notifications';
  20. import React from 'react';
  21. import { bool, func, string } from 'prop-types';
  22. import I18n from 'i18n!gradebook';
  23. import TextArea from 'instructure-ui/lib/components/TextArea';
  24. import Button from 'instructure-ui/lib/components/Button';
  25. import ScreenReaderContent from 'instructure-ui/lib/components/ScreenReaderContent';
  26. export default class SubmissionCommentForm extends React.Component {
  27. static propTypes = {
  28. cancelCommenting: func.isRequired,
  29. comment: string,
  30. processing: bool.isRequired,
  31. setProcessing: func.isRequired
  32. };
  33. static defaultProps = {
  34. comment: ''
  35. };
  36. constructor (props) {
  37. super(props);
  38. const methodsToBind = [
  39. 'bindTextarea', 'handleCancel', 'handleCommentChange', 'handlePublishComment', 'focusTextarea'
  40. ];
  41. methodsToBind.forEach((method) => { this[method] = this[method].bind(this); });
  42. this.state = { comment: props.comment };
  43. }
  44. focusTextarea () {
  45. this.textarea.focus();
  46. };
  47. handleCancel (event, callback) {
  48. event.preventDefault();
  49. this.setState({ comment: this.props.comment }, () => {
  50. this.props.cancelCommenting();
  51. if (callback) {
  52. callback();
  53. }
  54. });
  55. }
  56. handleCommentChange (event) {
  57. this.setState({ comment: event.target.value });
  58. }
  59. handlePublishComment (event) {
  60. event.preventDefault();
  61. this.props.setProcessing(true);
  62. this.publishComment().catch(() => this.props.setProcessing(false));
  63. }
  64. commentIsValid () {
  65. const comment = this.state.comment.trim();
  66. return comment.length > 0;
  67. }
  68. bindTextarea (ref) {
  69. this.textarea = ref;
  70. }
  71. render () {
  72. const { cancelButtonLabel, submitButtonLabel } = this.buttonLabels();
  73. return (
  74. <div>
  75. <div>
  76. <TextArea
  77. label={<ScreenReaderContent>{I18n.t('Leave a comment')}</ScreenReaderContent>}
  78. placeholder={I18n.t("Leave a comment")}
  79. onChange={this.handleCommentChange}
  80. value={this.state.comment}
  81. textareaRef={this.bindTextarea}
  82. />
  83. </div>
  84. {
  85. this.showButtons() &&
  86. <div
  87. style={{ textAlign: 'right', marginTop: '0rem', border: 'none', padding: '0rem', background: 'transparent' }}
  88. >
  89. <Button
  90. disabled={this.props.processing}
  91. label={cancelButtonLabel}
  92. margin="small small small 0"
  93. onClick={this.handleCancel}
  94. >
  95. {I18n.t("Cancel")}
  96. </Button>
  97. <Button
  98. disabled={this.props.processing || !this.commentIsValid()}
  99. label={submitButtonLabel}
  100. margin="small 0"
  101. onClick={this.handlePublishComment}
  102. variant="primary"
  103. >
  104. {I18n.t("Submit")}
  105. </Button>
  106. </div>
  107. }
  108. </div>
  109. );
  110. }
  111. }