Skip to content

Commit 8b686fe

Browse files
authored
feat: add prop disabled to ChatAutoComplete props (#2617)
1 parent 3801375 commit 8b686fe

File tree

2 files changed

+43
-19
lines changed

2 files changed

+43
-19
lines changed

src/components/ChatAutoComplete/ChatAutoComplete.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ export type SuggestionListProps<
9696
>;
9797

9898
export type ChatAutoCompleteProps<T extends UnknownType = UnknownType> = {
99+
/** Override the default disabled state of the underlying `textarea` component. */
100+
disabled?: boolean;
99101
/** Function to override the default submit handler on the underlying `textarea` component */
100102
handleSubmit?: (event: React.BaseSyntheticEvent) => void;
101103
/** Function to run on blur of the underlying `textarea` component */
@@ -167,7 +169,7 @@ const UnMemoizedChatAutoComplete = <
167169
closeCommandsList={messageInput.closeCommandsList}
168170
closeMentionsList={messageInput.closeMentionsList}
169171
containerClassName='str-chat__textarea str-chat__message-textarea-react-host'
170-
disabled={disabled || !!cooldownRemaining}
172+
disabled={(props.disabled ?? disabled) || !!cooldownRemaining}
171173
disableMentions={messageInput.disableMentions}
172174
grow={messageInput.grow}
173175
handleSubmit={props.handleSubmit || messageInput.handleSubmit}

src/components/ChatAutoComplete/__tests__/ChatAutocomplete.test.js

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const renderComponent = async (
5252
messageInputContextOverrides = {},
5353
activeChannel = channel,
5454
) => {
55-
const placeholderText = props.placeholder || 'placeholder';
55+
const placeholderText = props.placeholder === null ? null : props.placeholder || 'placeholder';
5656

5757
const OverrideMessageInputContext = ({ children }) => {
5858
const currentContext = useMessageInputContext();
@@ -70,24 +70,32 @@ const renderComponent = async (
7070
<ChatAutoComplete {...props} placeholder={placeholderText} />
7171
</OverrideMessageInputContext>
7272
);
73+
let renderResult;
74+
await act(() => {
75+
renderResult = render(
76+
<Chat client={chatClient}>
77+
<ActiveChannelSetter activeChannel={activeChannel} />
78+
<Channel>
79+
<MessageInput emojiSearchIndex={searchIndexMock} Input={AutoComplete} />
80+
</Channel>
81+
</Chat>,
82+
);
83+
});
7384

74-
const renderResult = render(
75-
<Chat client={chatClient}>
76-
<ActiveChannelSetter activeChannel={activeChannel} />
77-
<Channel>
78-
<MessageInput emojiSearchIndex={searchIndexMock} Input={AutoComplete} />
79-
</Channel>
80-
</Chat>,
81-
);
82-
const textarea = await waitFor(() => renderResult.getByPlaceholderText(placeholderText));
83-
const typeText = (text) => {
84-
fireEvent.change(textarea, {
85-
target: {
86-
selectionEnd: text.length,
87-
value: text,
88-
},
89-
});
90-
};
85+
let textarea;
86+
let typeText;
87+
if (placeholderText !== null) {
88+
textarea = await waitFor(() => renderResult.getByPlaceholderText(placeholderText));
89+
90+
typeText = (text) => {
91+
fireEvent.change(textarea, {
92+
target: {
93+
selectionEnd: text.length,
94+
value: text,
95+
},
96+
});
97+
};
98+
}
9199
return { ...renderResult, textarea, typeText };
92100
};
93101

@@ -132,6 +140,20 @@ describe('ChatAutoComplete', () => {
132140
expect(textarea).toBeDisabled();
133141
});
134142

143+
it('should give preference to prop disabled over the MessageInputContext value', async () => {
144+
const { textarea } = await renderComponent({ disabled: false }, { disabled: true });
145+
146+
expect(textarea).toBeEnabled();
147+
});
148+
149+
it('should give preference to cooldown value over the prop disabled', async () => {
150+
await renderComponent({ disabled: false, placeholder: null }, { cooldownRemaining: 10 });
151+
expect(screen.queryByPlaceholderText('Placeholder')).not.toBeInTheDocument();
152+
const textarea = screen.getByTestId('message-input');
153+
expect(textarea).toBeDisabled();
154+
expect(textarea).toHaveProperty('placeholder', 'Slow Mode ON');
155+
});
156+
135157
it('should let you select emojis when you type :<emoji>', async () => {
136158
const emojiAutocompleteText = ':smile';
137159
const { findByText, textarea, typeText } = await renderComponent();

0 commit comments

Comments
 (0)