Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New editor: Style for query preview (#467)
- Loading branch information
1 parent
506fe13
commit 045cf3d
Showing
5 changed files
with
89 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/components/QueryEditor/VisualQueryEditor/KQLPreview.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import React from 'react'; | ||
|
||
import { render, screen } from '@testing-library/react'; | ||
import KQLPreview from './KQLPreview'; | ||
|
||
describe('KQLPreview', () => { | ||
it('should render the query preview', () => { | ||
render(<KQLPreview query="my query" />); | ||
// hidden by default | ||
expect(screen.queryByText('my query')).not.toBeVisible(); | ||
screen.getByText('show').click(); | ||
expect(screen.queryByText('my query')).toBeVisible(); | ||
// hide it again | ||
screen.getByText('hide').click(); | ||
expect(screen.queryByText('my query')).not.toBeVisible(); | ||
}); | ||
}); |
58 changes: 58 additions & 0 deletions
58
src/components/QueryEditor/VisualQueryEditor/KQLPreview.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import { EditorField, EditorFieldGroup, EditorRow } from '@grafana/experimental'; | ||
import { Button, useStyles2 } from '@grafana/ui'; | ||
import { css } from '@emotion/css'; | ||
import { GrafanaTheme2 } from '@grafana/data'; | ||
import Prism from 'prismjs'; | ||
import 'prismjs/components/prism-kusto'; | ||
import 'prismjs/themes/prism-tomorrow.min.css'; | ||
|
||
interface KQLPreviewProps { | ||
query: string; | ||
} | ||
|
||
const KQLPreview: React.FC<KQLPreviewProps> = ({ query }) => { | ||
const styles = useStyles2(getStyles); | ||
const [hidden, setHidden] = useState(true); | ||
|
||
useEffect(() => { | ||
Prism.highlightAll(); | ||
}, [query]); | ||
|
||
return ( | ||
<EditorRow> | ||
<EditorFieldGroup> | ||
<EditorField label="Query Preview"> | ||
<> | ||
<Button hidden={!hidden} variant="secondary" onClick={() => setHidden(false)} size="sm"> | ||
show | ||
</Button> | ||
<div className={styles.codeBlock} hidden={hidden}> | ||
<pre className={styles.code}> | ||
<code className="language-kusto">{query}</code> | ||
</pre> | ||
</div> | ||
<Button hidden={hidden} variant="secondary" onClick={() => setHidden(true)} size="sm"> | ||
hide | ||
</Button> | ||
</> | ||
</EditorField> | ||
</EditorFieldGroup> | ||
</EditorRow> | ||
); | ||
}; | ||
|
||
const getStyles = (theme: GrafanaTheme2) => { | ||
return { | ||
codeBlock: css({ | ||
width: '100%', | ||
display: 'table', | ||
tableLayout: 'fixed', | ||
}), | ||
code: css({ | ||
marginBottom: '4px', | ||
}), | ||
}; | ||
}; | ||
|
||
export default KQLPreview; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters