mirror of
https://github.com/zhigang1992/react.git
synced 2026-03-27 22:54:38 +08:00
style(prettier): format code style
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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'};
|
||||
|
||||
Reference in New Issue
Block a user