mirror of
https://github.com/elastic/kibana.git
synced 2025-06-28 11:05:39 -04:00
Fix placeholder in monaco editor dissapear when value is set (#217828)
## Summary This PR fixes [[Bug] Setting text in the placeholder property does not clear when value is set with onChange action](https://github.com/elastic/kibana/issues/149882) issue.
This commit is contained in:
parent
499f11d54e
commit
ea3dead452
2 changed files with 51 additions and 14 deletions
|
@ -38,7 +38,7 @@ export const Basic = {
|
||||||
{...params}
|
{...params}
|
||||||
languageId="plainText"
|
languageId="plainText"
|
||||||
onChange={action('on change')}
|
onChange={action('on change')}
|
||||||
value="Hello!"
|
placeholder="Type something here"
|
||||||
height={200}
|
height={200}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
|
|
|
@ -20,6 +20,7 @@ import {
|
||||||
EuiFlexGroup,
|
EuiFlexGroup,
|
||||||
EuiFlexItem,
|
EuiFlexItem,
|
||||||
useEuiTheme,
|
useEuiTheme,
|
||||||
|
UseEuiTheme,
|
||||||
} from '@elastic/eui';
|
} from '@elastic/eui';
|
||||||
import {
|
import {
|
||||||
monaco,
|
monaco,
|
||||||
|
@ -215,7 +216,6 @@ export const CodeEditor: React.FC<CodeEditorProps> = ({
|
||||||
const isReadOnly = options?.readOnly ?? false;
|
const isReadOnly = options?.readOnly ?? false;
|
||||||
|
|
||||||
const [_editor, setEditor] = useState<monaco.editor.IStandaloneCodeEditor | null>(null);
|
const [_editor, setEditor] = useState<monaco.editor.IStandaloneCodeEditor | null>(null);
|
||||||
const _placeholderWidget = useRef<PlaceholderWidget | null>(null);
|
|
||||||
const isSuggestionMenuOpen = useRef(false);
|
const isSuggestionMenuOpen = useRef(false);
|
||||||
const editorHint = useRef<HTMLDivElement>(null);
|
const editorHint = useRef<HTMLDivElement>(null);
|
||||||
const textboxMutationObserver = useRef<MutationObserver | null>(null);
|
const textboxMutationObserver = useRef<MutationObserver | null>(null);
|
||||||
|
@ -473,19 +473,8 @@ export const CodeEditor: React.FC<CodeEditorProps> = ({
|
||||||
};
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (placeholder && !value && _editor) {
|
|
||||||
// Mounts editor inside constructor
|
|
||||||
_placeholderWidget.current = new PlaceholderWidget(placeholder, euiTheme, _editor);
|
|
||||||
}
|
|
||||||
|
|
||||||
return () => {
|
|
||||||
_placeholderWidget.current?.dispose();
|
|
||||||
_placeholderWidget.current = null;
|
|
||||||
};
|
|
||||||
}, [placeholder, value, euiTheme, _editor]);
|
|
||||||
|
|
||||||
useFitToContent({ editor: _editor, fitToContent, isFullScreen });
|
useFitToContent({ editor: _editor, fitToContent, isFullScreen });
|
||||||
|
usePlaceholder({ placeholder, euiTheme, editor: _editor, value });
|
||||||
|
|
||||||
const { CopyButton } = useCopy({ isCopyable, value });
|
const { CopyButton } = useCopy({ isCopyable, value });
|
||||||
|
|
||||||
|
@ -660,6 +649,54 @@ const useCopy = ({ isCopyable, value }: { isCopyable: boolean; value: string })
|
||||||
return { showCopyButton, CopyButton };
|
return { showCopyButton, CopyButton };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const usePlaceholder = ({
|
||||||
|
placeholder,
|
||||||
|
euiTheme,
|
||||||
|
editor,
|
||||||
|
value,
|
||||||
|
}: {
|
||||||
|
placeholder: string | undefined;
|
||||||
|
euiTheme: UseEuiTheme['euiTheme'];
|
||||||
|
editor: monaco.editor.IStandaloneCodeEditor | null;
|
||||||
|
value: string;
|
||||||
|
}) => {
|
||||||
|
useEffect(() => {
|
||||||
|
if (!placeholder || !editor) return;
|
||||||
|
|
||||||
|
let placeholderWidget: PlaceholderWidget | null = null;
|
||||||
|
|
||||||
|
const addPlaceholder = () => {
|
||||||
|
if (!placeholderWidget) {
|
||||||
|
placeholderWidget = new PlaceholderWidget(placeholder, euiTheme, editor);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const removePlaceholder = () => {
|
||||||
|
if (placeholderWidget) {
|
||||||
|
placeholderWidget.dispose();
|
||||||
|
placeholderWidget = null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!value) {
|
||||||
|
addPlaceholder();
|
||||||
|
}
|
||||||
|
|
||||||
|
const onDidChangeContent = editor.getModel()?.onDidChangeContent(() => {
|
||||||
|
if (!editor.getModel()?.getValue()) {
|
||||||
|
addPlaceholder();
|
||||||
|
} else {
|
||||||
|
removePlaceholder();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
onDidChangeContent?.dispose();
|
||||||
|
removePlaceholder();
|
||||||
|
};
|
||||||
|
}, [placeholder, value, euiTheme, editor]);
|
||||||
|
};
|
||||||
|
|
||||||
const useFitToContent = ({
|
const useFitToContent = ({
|
||||||
editor,
|
editor,
|
||||||
fitToContent,
|
fitToContent,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue