Skip to content
This repository has been archived by the owner on Jun 30, 2022. It is now read-only.

[NEW] Message character limit feature #443

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion size-plugin.json

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion src/components/Composer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export class Composer extends Component {
// we only update composer if value length changed from 0 to 1 or 1 to 0
// everything else is managed by this.el
shouldComponentUpdate({ value: nextValue }) {
const { value } = this.props;
const { value, limitTextLength } = this.props;

const nextValueEmpty = !nextValue || nextValue.length === 0;
const valueEmpty = !value || value.length === 0;
Expand All @@ -166,6 +166,10 @@ export class Composer extends Component {
return true;
}

if (nextValue.length === limitTextLength || value.length === limitTextLength) {
return true;
}

return false;
}

Expand Down
10 changes: 10 additions & 0 deletions src/components/Footer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,13 @@ export const FooterOptions = ({ children }) => (
{children}
</PopoverMenu>
);


export const CharCounter = ({ className, style = {}, textLength, limitTextLength }) => (
<span
className={createClassName(styles, 'footer__remainder', { highlight: textLength === limitTextLength }, [className])}
style={style}
>
{textLength} / {limitTextLength}
</span>
);
11 changes: 11 additions & 0 deletions src/components/Footer/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@

@include pressable-button(2px);
}

&__remainder {
margin-left: 10px;
min-width: 100px;
font-weight: bold;
&--highlight {
color: $color-red;
transition: color .2s;
}
}

}

.powered-by {
Expand Down
3 changes: 2 additions & 1 deletion src/components/Screen/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export const ScreenContent = ({ children, nopadding }) => (
);


export const ScreenFooter = ({ children, options }) => (
export const ScreenFooter = ({ children, options, limit }) => (
<Footer>
{children && (
<FooterContent>
Expand All @@ -138,6 +138,7 @@ export const ScreenFooter = ({ children, options }) => (
)}
<FooterContent>
{options}
{limit}
<PoweredBy />
</FooterContent>
</Footer>
Expand Down
19 changes: 15 additions & 4 deletions src/routes/Chat/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { h, Component } from 'preact';

import { Composer, ComposerAction, ComposerActions } from '../../components/Composer';
import { FilesDropTarget } from '../../components/FilesDropTarget';
import { FooterOptions } from '../../components/Footer';
import { FooterOptions, CharCounter } from '../../components/Footer';
import { Menu } from '../../components/Menu';
import { MessageList } from '../../components/Messages';
import { Screen } from '../../components/Screen';
Expand Down Expand Up @@ -67,9 +67,13 @@ export default class Chat extends Component {
}

handleChangeText = (text) => {
this.setState({ text });
const { onChangeText } = this.props;
onChangeText && onChangeText(text);
let value = text;
const { onChangeText, limitTextLength } = this.props;
if (limitTextLength && limitTextLength < text.length) {
value = value.substring(0, limitTextLength);
}
this.setState({ text: value });
onChangeText && onChangeText(value);
}

toggleEmojiPickerState = () => {
Expand Down Expand Up @@ -110,6 +114,7 @@ export default class Chat extends Component {
onRemoveUserData,
lastReadMessageId,
queueInfo,
limitTextLength,
...props
}, {
atBottom = true,
Expand Down Expand Up @@ -174,6 +179,11 @@ export default class Chat extends Component {
</Menu.Group>
</FooterOptions>
) : null}
limit={limitTextLength
? <CharCounter
limitTextLength={limitTextLength}
textLength={text.length}
/> : null}
>
<Composer onUpload={onUpload}
onSubmit={this.handleSubmit}
Expand Down Expand Up @@ -203,6 +213,7 @@ export default class Chat extends Component {
)}
</ComposerActions>
)}
limitTextLength={limitTextLength}
/>
</Screen.Footer>
</FilesDropTarget>
Expand Down
3 changes: 2 additions & 1 deletion src/routes/Chat/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ export const ChatConnector = ({ ref, ...props }) => (
allowSwitchingDepartments,
forceAcceptDataProcessingConsent: allowRemoveUserData,
showConnecting,
limitTextLength,
} = {},
messages: {
conversationFinishedMessage,
Expand Down Expand Up @@ -423,7 +424,7 @@ export const ChatConnector = ({ ref, ...props }) => (
estimatedWaitTimeSeconds: queueInfo.estimatedWaitTimeSeconds,
message: queueInfo.message,
} : undefined}

limitTextLength={limitTextLength}
/>
)}
</Consumer>
Expand Down
3 changes: 3 additions & 0 deletions src/routes/Chat/stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ storiesOf('Routes/Chat', module)
onBottom={action('bottom')}
onUpload={action('upload')}
onSubmit={action('submit')}
limitTextLength={number('limitTextLength', 0)}
{...screenProps()}
/>
))
Expand All @@ -72,6 +73,7 @@ storiesOf('Routes/Chat', module)
onBottom={action('bottom')}
onUpload={action('upload')}
onSubmit={action('submit')}
limitTextLength={number('limitTextLength', 0)}
{...screenProps()}
/>
))
Expand All @@ -92,6 +94,7 @@ storiesOf('Routes/Chat', module)
onBottom={action('bottom')}
onUpload={action('upload')}
onSubmit={action('submit')}
limitTextLength={number('limitTextLength', 0)}
{...screenProps()}
/>
));
5 changes: 5 additions & 0 deletions src/routes/Chat/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@
border-radius: 50%;
}
}

.none__action {
opacity: 0.6;
margin: 0 6px;
}
}

@keyframes loader-rotate {
Expand Down