inputboxFoundation.ts 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { handlePrevent } from "../utils/a11y";
  2. import BaseFoundation, { DefaultAdapter } from "../base/foundation";
  3. import { strings } from './constants';
  4. const { SEND_HOT_KEY } = strings;
  5. export interface InputBoxAdapter<P = Record<string, any>, S = Record<string, any>> extends DefaultAdapter<P, S> {
  6. notifyInputChange: (props: { inputValue: string; attachment: any[]}) => void;
  7. setInputValue: (value: string) => void;
  8. setAttachment: (attachment: any[]) => void;
  9. notifySend: (content: string, attachment: any[]) => void
  10. }
  11. export default class InputBoxFoundation <P = Record<string, any>, S = Record<string, any>> extends BaseFoundation<InputBoxAdapter<P, S>, P, S> {
  12. constructor(adapter: InputBoxAdapter<P, S>) {
  13. super({ ...adapter });
  14. }
  15. onInputAreaChange = (value: string) => {
  16. const attachment = this.getState('attachment');
  17. this._adapter.setInputValue(value);
  18. this._adapter.notifyInputChange({ inputValue: value, attachment });
  19. }
  20. onAttachmentAdd = (props: any) => {
  21. const { fileList } = props;
  22. const { uploadProps } = this.getProps();
  23. const { onChange } = uploadProps;
  24. if (onChange) {
  25. onChange(props);
  26. }
  27. const { content } = this.getStates();
  28. let newFileList = [...fileList];
  29. this._adapter.setAttachment(newFileList);
  30. this._adapter.notifyInputChange({
  31. inputValue: content,
  32. attachment: newFileList
  33. });
  34. }
  35. onAttachmentDelete = (props: any) => {
  36. const { content, attachment } = this.getStates();
  37. const newAttachMent = attachment.filter(item => item.uid !== props.uid);
  38. this._adapter.setAttachment(newAttachMent);
  39. this._adapter.notifyInputChange({
  40. inputValue: content,
  41. attachment: newAttachMent
  42. });
  43. }
  44. onSend = (e: any) => {
  45. if (this.getDisableSend()) {
  46. return;
  47. }
  48. const { content, attachment } = this.getStates();
  49. this._adapter.setInputValue('');
  50. this._adapter.setAttachment([]);
  51. this._adapter.notifySend(content, attachment);
  52. }
  53. getDisableSend = () => {
  54. const { content, attachment } = this.getStates();
  55. const { disableSend: disableSendInProps } = this.getProps();
  56. const disabledSend = disableSendInProps || (content.length === 0 && attachment.length === 0);
  57. return disabledSend;
  58. }
  59. onEnterPress = (e: any) => {
  60. const { sendHotKey } = this.getProps();
  61. if (sendHotKey === SEND_HOT_KEY.SHIFT_PLUS_ENTER && e.shiftKey === false) {
  62. return ;
  63. } else if (sendHotKey === SEND_HOT_KEY.ENTER && e.shiftKey === true) {
  64. return ;
  65. }
  66. handlePrevent(e);
  67. this.onSend(e);
  68. };
  69. onPaste = (e: any) => {
  70. const items = e.clipboardData?.items;
  71. const { manualUpload, pasteUpload } = this.getProps();
  72. let files = [];
  73. if (pasteUpload && items) {
  74. for (const it of items) {
  75. const file = it.getAsFile();
  76. file && files.push(it.getAsFile());
  77. }
  78. if (files.length) {
  79. // 文件上传,则需要阻止默认粘贴行为
  80. // File upload, you need to prevent the default paste behavior
  81. manualUpload(files);
  82. e.preventDefault();
  83. e.stopPropagation();
  84. }
  85. }
  86. }
  87. }