Skip to content

Commit

Permalink
fix(variable-input): initalContent over placeholder
Browse files Browse the repository at this point in the history
  • Loading branch information
pepeladeira committed Nov 28, 2024
1 parent 82a2da5 commit e127035
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/components/variable-input/variable-input.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ export const WithInputProps: Story = {

export const WithInitialContent: Story = {
args: {
label: 'Expression',
placeholder: 'Enter the expression',
initialContent: 'Hello {{name}}! How are you?',
options
}
Expand Down
11 changes: 11 additions & 0 deletions src/components/variable-input/variable-input.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,15 @@ describe('VariableInput Component', () => {

expect(getByText('No results found')).toBeInTheDocument();
});

it('should prioritize initialContent over placeholder', () => {
const initialContent = 'Initial content';
const { container } = render(<VariableInput options={options} initialContent={initialContent} placeholder="Placeholder" />);

const editor = container.querySelector("[contenteditable='true']");
if (editor) {
expect(editor.innerHTML).toBe(initialContent);
expect(editor.innerHTML).not.toBe('Placeholder');
}
});
});
4 changes: 2 additions & 2 deletions src/components/variable-input/variable-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,13 @@ export function VariableInput({
const editor = editorRef.current;

if (editor) {
if (!isFocused && !text && placeholder) {
if (!isFocused && !text && !initialContent && placeholder) {
editor.innerHTML = placeholderTag;
} else if (isFocused && editor.innerHTML === placeholderTag) {
editor.innerHTML = '';
}
}
}, [isFocused, text, placeholder]);
}, [isFocused, text, placeholder, initialContent]);

React.useEffect(() => {
if (!open) {
Expand Down

0 comments on commit e127035

Please sign in to comment.