You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
1.9 KiB

  1. // Package imports.
  2. import React from 'react';
  3. import PropTypes from 'prop-types';
  4. import { defineMessages, FormattedMessage } from 'react-intl';
  5. // Components.
  6. import Collapsable from 'flavours/glitch/components/collapsable';
  7. // Utils.
  8. import {
  9. assignHandlers,
  10. hiddenComponent,
  11. } from 'flavours/glitch/util/react_helpers';
  12. // Messages.
  13. const messages = defineMessages({
  14. placeholder: {
  15. defaultMessage: 'Write your warning here',
  16. id: 'compose_form.spoiler_placeholder',
  17. },
  18. });
  19. // Handlers.
  20. const handlers = {
  21. // Handles a keypress.
  22. handleKeyDown ({
  23. ctrlKey,
  24. keyCode,
  25. metaKey,
  26. }) {
  27. const { onSubmit } = this.props;
  28. // We submit the status on control/meta + enter.
  29. if (onSubmit && keyCode === 13 && (ctrlKey || metaKey)) {
  30. onSubmit();
  31. }
  32. },
  33. };
  34. // The component.
  35. export default class ComposerSpoiler extends React.PureComponent {
  36. // Constructor.
  37. constructor (props) {
  38. super(props);
  39. assignHandlers(this, handlers);
  40. }
  41. // Rendering.
  42. render () {
  43. const { handleKeyDown } = this.handlers;
  44. const {
  45. hidden,
  46. intl,
  47. onChange,
  48. text,
  49. } = this.props;
  50. // The result.
  51. return (
  52. <Collapsable
  53. isVisible={!hidden}
  54. fullHeight={50}
  55. >
  56. <label className='composer--spoiler'>
  57. <span {...hiddenComponent}>
  58. <FormattedMessage {...messages.placeholder} />
  59. </span>
  60. <input
  61. id='glitch.composer.spoiler.input'
  62. onChange={onChange}
  63. onKeyDown={handleKeyDown}
  64. placeholder={intl.formatMessage(messages.placeholder)}
  65. type='text'
  66. value={text}
  67. />
  68. </label>
  69. </Collapsable>
  70. );
  71. }
  72. }
  73. // Props.
  74. ComposerSpoiler.propTypes = {
  75. hidden: PropTypes.bool,
  76. intl: PropTypes.object.isRequired,
  77. onChange: PropTypes.func,
  78. onSubmit: PropTypes.func,
  79. text: PropTypes.string,
  80. };