Skip to content
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
7 changes: 6 additions & 1 deletion packages/mentis/src/components/MentionInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ export const MentionInput: React.FC<MentionInputProps> = ({
onChange,
onKeyDown,
}) => {
// Get the custom placeholder or fall back to default
const placeholder =
slotsProps?.contentEditable?.["data-placeholder"] ||
`Type ${trigger} to mention someone`;

const {
editorRef,
showModal,
Expand Down Expand Up @@ -42,7 +47,7 @@ export const MentionInput: React.FC<MentionInputProps> = ({
return (
<div className="mention-input-container" {...slotsProps?.container}>
<div
data-placeholder={`Type ${trigger} to mention someone`}
data-placeholder={placeholder}
role="combobox"
aria-autocomplete="list"
aria-expanded={showModal}
Expand Down
6 changes: 4 additions & 2 deletions packages/mentis/src/hooks/useMentionInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ export function useMentionInput({
const currentData = extractMentionData(editorRef.current);
if (currentData.dataValue !== dataValue) {
if (dataValue === "") {
// Clear all content including mention chips
// Clear all content including mention chips and ensure truly empty for CSS :empty
editorRef.current.innerHTML = "";
editorRef.current.textContent = "";
} else {
// Reconstruct content from dataValue
const reconstructedHTML = reconstructFromDataValue({
Expand All @@ -62,8 +63,9 @@ export function useMentionInput({
const currentText = getTextContent(editorRef.current);
if (currentText !== displayValue) {
if (displayValue === "") {
// Clear all content including mention chips
// Clear all content including mention chips and ensure truly empty for CSS :empty
editorRef.current.innerHTML = "";
editorRef.current.textContent = "";
} else {
// Update text content
editorRef.current.textContent = displayValue;
Expand Down
43 changes: 42 additions & 1 deletion packages/mentis/tests/input.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ describe("Input props", () => {

// Update to a value with mentions
rerender(
<MentionInput options={options} displayValue="Hello @john, how are you?" />
<MentionInput
options={options}
displayValue="Hello @john, how are you?"
/>
);
editorElement = screen.getByRole("combobox");
expect(editorElement).toHaveTextContent("Hello @john, how are you?");
Expand Down Expand Up @@ -152,4 +155,42 @@ describe("Input props", () => {
customPlaceholder
);
});

test("Placeholder should reappear after content is added and then removed", async () => {
const customPlaceholder = "Type your message here...";
const { rerender } = render(
<MentionInput
options={[]}
slotsProps={{
contentEditable: {
"data-placeholder": customPlaceholder,
},
}}
/>
);

const user = userEvent.setup();
const editorElement = screen.getByRole("combobox");

// Add some content
await user.type(editorElement, "Hello world");
expect(editorElement).toHaveTextContent("Hello world");

// Clear the content by setting displayValue to empty string
rerender(
<MentionInput
options={[]}
displayValue=""
slotsProps={{
contentEditable: {
"data-placeholder": customPlaceholder,
},
}}
/>
);

// Element should be empty and placeholder should be visible via CSS
expect(editorElement).toHaveTextContent("");
expect(editorElement.innerHTML).toBe("");
});
});