SubmissionTrayRadioInput.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 React from 'react';
  19. import { func, number, shape, string, bool } from 'prop-types';
  20. import I18n from 'i18n!gradebook';
  21. import Container from 'instructure-ui/lib/components/Container';
  22. import NumberInput from 'instructure-ui/lib/components/NumberInput';
  23. import PresentationContent from 'instructure-ui/lib/components/PresentationContent';
  24. import Typography from 'instructure-ui/lib/components/Typography';
  25. import RadioInput from 'instructure-ui/lib/components/RadioInput';
  26. import ScreenReaderContent from 'instructure-ui/lib/components/ScreenReaderContent';
  27. import classnames from 'classnames';
  28. import round from 'coffeescripts/util/round';
  29. function defaultDurationLate (interval, secondsLate) {
  30. let durationLate = secondsLate / 3600;
  31. if (interval === 'day') {
  32. durationLate /= 24;
  33. }
  34. return round(durationLate, 2).toString();
  35. }
  36. export default function SubmissionTrayRadioInput (props) {
  37. const showNumberInput = props.value === 'late' && props.checked;
  38. const interval = props.latePolicy.lateSubmissionInterval;
  39. const numberInputLabel = interval === 'day' ? I18n.t('Days late') : I18n.t('Hours late');
  40. const numberInputText = interval === 'day' ? I18n.t('Day(s)') : I18n.t('Hour(s)');
  41. const styles = {
  42. backgroundColor: props.color,
  43. height: showNumberInput ? '5rem' : '2rem'
  44. };
  45. const radioInputClasses = classnames(
  46. 'SubmissionTray__RadioInput',
  47. { 'SubmissionTray__RadioInput-WithBackground': props.color !== 'transparent' }
  48. );
  49. return (
  50. <div className={radioInputClasses} style={styles}>
  51. <RadioInput
  52. checked={props.checked}
  53. name={props.value}
  54. label={props.text}
  55. onChange={props.onChange}
  56. value={props.value}
  57. />
  58. {
  59. showNumberInput &&
  60. <span className="NumberInput__Container NumberInput__Container-LeftIndent">
  61. <NumberInput
  62. defaultValue={defaultDurationLate(interval, props.submission.secondsLate)}
  63. inline
  64. label={<ScreenReaderContent>{numberInputLabel}</ScreenReaderContent>}
  65. locale={props.locale}
  66. min="0"
  67. onBlur={props.onNumberInputBlur}
  68. showArrows={false}
  69. width="5rem"
  70. />
  71. <PresentationContent>
  72. <Container as="div" margin="0 small">
  73. <Typography>{numberInputText}</Typography>
  74. </Container>
  75. </PresentationContent>
  76. </span>
  77. }
  78. </div>
  79. );
  80. }
  81. SubmissionTrayRadioInput.propTypes = {
  82. checked: bool.isRequired,
  83. color: string.isRequired,
  84. latePolicy: shape({
  85. lateSubmissionInterval: string.isRequired
  86. }).isRequired,
  87. locale: string.isRequired,
  88. onChange: func.isRequired,
  89. onNumberInputBlur: func.isRequired,
  90. submission: shape({
  91. secondsLate: number.isRequired
  92. }).isRequired,
  93. text: string.isRequired,
  94. value: string.isRequired
  95. };
  96. SubmissionTrayRadioInput.defaultProps = {
  97. color: 'transparent'
  98. };