-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat #231 - Implement Code component
- Loading branch information
1 parent
d644269
commit d0cfe9b
Showing
7 changed files
with
368 additions
and
9 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,57 @@ | ||
import React from 'react' | ||
import { Code, CodeProps } from '.' | ||
import { Meta, Story } from '@storybook/react/types-6-0' | ||
|
||
export default { | ||
argTypes: { | ||
height: { defaultValue: 64 } | ||
}, | ||
component: Code, | ||
title: 'Code' | ||
} as Meta | ||
|
||
const Template: Story<CodeProps> = args => <Code {...args} /> | ||
|
||
/* eslint-disable sort-keys */ | ||
const testJson = { | ||
dassana: { | ||
context: { | ||
attachedResources: { | ||
'count-of-attached-eni': 3, | ||
'does-any-eni-have-public-ip': true, | ||
'are-there-flows-from-internet-in-vpcflows': false | ||
} | ||
} | ||
}, | ||
resourceConfig: { | ||
configuration: { | ||
description: 'ssh-from-world', | ||
groupName: 'prod-cache', | ||
ipPermissions: [ | ||
{ | ||
fromPort: 22, | ||
ipProtocol: 'tcp', | ||
ipv6Ranges: [ | ||
{ | ||
cidrIpv6: '::/0' | ||
} | ||
], | ||
prefixListIds: [], | ||
toPort: 22, | ||
userIdGroupPairs: [], | ||
ipv4Ranges: [ | ||
{ | ||
cidrIp: '0.0.0.0/0' | ||
} | ||
] | ||
} | ||
] | ||
} | ||
} | ||
} | ||
/* eslint-enable sort-keys */ | ||
|
||
export const Default = Template.bind({}) | ||
Default.args = { | ||
code: testJson | ||
} |
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,38 @@ | ||
import './prism.css' | ||
import cn from 'classnames' | ||
import Prism from 'prismjs' | ||
import React, { FC, useEffect } from 'react' | ||
require('prismjs/plugins/line-numbers/prism-line-numbers') | ||
require('prismjs/components/prism-json') | ||
|
||
// overwriting default styles react example https://github.com/reactjs/reactjs.org/blob/master/src/prism-styles.js#L11 | ||
export interface CodeProps { | ||
code: string | Record<string, any> | ||
language?: 'json' | 'javascript' | ||
lineNumbers?: boolean | ||
readOnly?: boolean | ||
} | ||
|
||
export const Code: FC<CodeProps> = ({ | ||
code, | ||
language = 'json', | ||
lineNumbers = true, | ||
readOnly = true | ||
}: CodeProps) => { | ||
useEffect(() => { | ||
Prism.highlightAll() | ||
}, []) | ||
|
||
return ( | ||
<pre | ||
className={cn({ 'line-numbers': lineNumbers })} | ||
contentEditable={!readOnly} | ||
> | ||
<code className={`language-${language}`}> | ||
{typeof code === 'string' | ||
? code | ||
: JSON.stringify(code, null, '\t')} | ||
</code> | ||
</pre> | ||
) | ||
} |
Oops, something went wrong.