style(prettier): format code style

This commit is contained in:
unix
2020-05-06 14:18:28 +08:00
parent cf8e277324
commit 112c826575
263 changed files with 4927 additions and 3992 deletions

View File

@@ -7,39 +7,41 @@ describe('Code', () => {
const wrapper = mount(<Code>code</Code>)
expect(() => wrapper.unmount()).not.toThrow()
})
it('should support block mode', () => {
const wrapper = render(<Code block>code</Code>)
expect(wrapper).toMatchSnapshot()
})
it('should repspond to changed by width', () => {
const wrapper = render(<Code block width="50%">code</Code>)
const wrapper = render(
<Code block width="50%">
code
</Code>,
)
expect(wrapper).toMatchSnapshot()
})
it('should render pre element only in block mode', () => {
const wrapper = mount(<Code>code</Code>)
expect(wrapper.find('pre').length).toBe(0)
wrapper.setProps({ block: true })
expect(wrapper.find('pre').length).not.toBe(0)
})
it('should alert warning when use bash', () => {
let errorMessage = ''
const errorSpy = jest.spyOn(console, 'error')
.mockImplementation(msg => errorMessage = msg)
const errorSpy = jest.spyOn(console, 'error').mockImplementation((msg) => (errorMessage = msg))
mount(<Code bash>code</Code>)
expect(errorMessage.toLowerCase()).toContain('deprecated')
errorSpy.mockRestore()
})
it('should alert warning when use darkBash', () => {
let errorMessage = ''
const errorSpy = jest.spyOn(console, 'error')
.mockImplementation(msg => errorMessage = msg)
const errorSpy = jest.spyOn(console, 'error').mockImplementation((msg) => (errorMessage = msg))
mount(<Code darkBash>code</Code>)
expect(errorMessage.toLowerCase()).toContain('deprecated')
errorSpy.mockRestore()

View File

@@ -21,7 +21,13 @@ type NativeAttrs = Omit<React.HTMLAttributes<any>, keyof Props>
export type CodeProps = Props & typeof defaultProps & NativeAttrs
const Code: React.FC<React.PropsWithChildren<CodeProps>> = ({
children, block, bash, darkBash, className, width, ...props
children,
block,
bash,
darkBash,
className,
width,
...props
}) => {
if (bash) {
useWarning('Props "bash" is deprecated. Use `Snippet` instead of it.', 'code')
@@ -29,33 +35,32 @@ const Code: React.FC<React.PropsWithChildren<CodeProps>> = ({
if (darkBash) {
useWarning('Props "darkBash" is deprecated. Use `Snippet` instead of it.', 'code')
}
const isBash = bash || darkBash
const isBlock = isBash || block
if (!isBlock) return <code {...props}>{children}</code>
const classes = useMemo(
() => `${darkBash ? 'dark' : ''} ${className}`,
[className, darkBash]
)
const classes = useMemo(() => `${darkBash ? 'dark' : ''} ${className}`, [className, darkBash])
return (
<>
<pre className={classes} {...props}><code>{children}</code></pre>
<pre className={classes} {...props}>
<code>{children}</code>
</pre>
<style jsx>{`
pre {
width: ${width ? width : 'initial'};
max-width: 100%;
}
.dark {
color: white;
background: black;
}
.dark code {
color: white;
}
pre:before {
content: '$ ';
display: ${isBash ? 'inline-block' : 'none'};