|
| 1 | +import dedent from 'dedent-js' |
| 2 | +import { TransformOptions } from '../../src' |
| 3 | +import { astxTestcase } from '../astxTestcase' |
| 4 | + |
| 5 | +astxTestcase({ |
| 6 | + file: __filename, |
| 7 | + parsers: ['babel/tsx'], |
| 8 | + input: dedent` |
| 9 | + import * as React from 'react' |
| 10 | + import { NavLink } from 'react-router-dom' |
| 11 | + import ListItem from '@material-ui/core/ListItem' |
| 12 | + import { withStyles } from '@material-ui/core/styles' |
| 13 | + import type { Theme } from '../../theme' |
| 14 | + import classNames from 'classnames' |
| 15 | + export const SIDEBAR_ITEM_HEIGHT = 30 |
| 16 | + |
| 17 | + const sidebarItemStyles = (theme: Theme): {} => { |
| 18 | + // eslint-disable-line flowtype/require-return-type |
| 19 | + return { |
| 20 | + root: { |
| 21 | + position: 'relative', |
| 22 | + paddingLeft: theme.spacing(3.5), |
| 23 | + height: SIDEBAR_ITEM_HEIGHT, |
| 24 | + paddingTop: 0, |
| 25 | + paddingBottom: 0, |
| 26 | + paddingRight: theme.spacing(2), |
| 27 | + }, |
| 28 | + active: { |
| 29 | + backgroundColor: 'rgba(255,255,255,0.2)', |
| 30 | + '&:focus': { |
| 31 | + backgroundColor: 'rgba(255,255,255,0.1)', |
| 32 | + }, |
| 33 | + '&:hover': { |
| 34 | + backgroundColor: 'rgba(255,255,255,0.25)', |
| 35 | + }, |
| 36 | + }, |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + export type SidebarItemProps = { |
| 41 | + classes: Classes |
| 42 | + children: React.ReactNode |
| 43 | + className?: string |
| 44 | + component?: ListItem['component'] | null | undefined |
| 45 | + } |
| 46 | + const SidebarItem = withStyles(sidebarItemStyles)( |
| 47 | + ({ |
| 48 | + classes, |
| 49 | + className, |
| 50 | + children, |
| 51 | + ...props |
| 52 | + }: SidebarItemProps): React.ReactElement => { |
| 53 | + if (props.component === NavLink) |
| 54 | + (props as any).activeClassName = classes.active |
| 55 | + return ( |
| 56 | + <ListItem |
| 57 | + {...props} |
| 58 | + button |
| 59 | + className={classNames(classes.root, className)} |
| 60 | + > |
| 61 | + {children} |
| 62 | + </ListItem> |
| 63 | + ) |
| 64 | + } |
| 65 | + ) |
| 66 | + export default SidebarItem |
| 67 | + `, |
| 68 | + astx: ({ astx }: TransformOptions): void => { |
| 69 | + const styleFn = astx.find`(theme: Theme): $Maybe<$Ret> => $body`() |
| 70 | + styleFn.$body.destruct`({ $$props })`().replace`({ $$props } as const)`() |
| 71 | + styleFn.$body.find`return { $$props }`() |
| 72 | + .replace`return { $$props } as const`() |
| 73 | + |
| 74 | + styleFn.destruct`($$args): $Ret => $body`().replace`($$args) => $body`() |
| 75 | + }, |
| 76 | + expected: dedent` |
| 77 | + import * as React from 'react' |
| 78 | + import { NavLink } from 'react-router-dom' |
| 79 | + import ListItem from '@material-ui/core/ListItem' |
| 80 | + import { withStyles } from '@material-ui/core/styles' |
| 81 | + import type { Theme } from '../../theme' |
| 82 | + import classNames from 'classnames' |
| 83 | + export const SIDEBAR_ITEM_HEIGHT = 30 |
| 84 | + |
| 85 | + const sidebarItemStyles = (theme: Theme) => { |
| 86 | + // eslint-disable-line flowtype/require-return-type |
| 87 | + return ({ |
| 88 | + root: { |
| 89 | + position: 'relative', |
| 90 | + paddingLeft: theme.spacing(3.5), |
| 91 | + height: SIDEBAR_ITEM_HEIGHT, |
| 92 | + paddingTop: 0, |
| 93 | + paddingBottom: 0, |
| 94 | + paddingRight: theme.spacing(2), |
| 95 | + }, |
| 96 | + active: { |
| 97 | + backgroundColor: 'rgba(255,255,255,0.2)', |
| 98 | + '&:focus': { |
| 99 | + backgroundColor: 'rgba(255,255,255,0.1)', |
| 100 | + }, |
| 101 | + '&:hover': { |
| 102 | + backgroundColor: 'rgba(255,255,255,0.25)', |
| 103 | + }, |
| 104 | + } |
| 105 | + } as const); |
| 106 | + }; |
| 107 | + |
| 108 | + export type SidebarItemProps = { |
| 109 | + classes: Classes |
| 110 | + children: React.ReactNode |
| 111 | + className?: string |
| 112 | + component?: ListItem['component'] | null | undefined |
| 113 | + } |
| 114 | + const SidebarItem = withStyles(sidebarItemStyles)( |
| 115 | + ({ |
| 116 | + classes, |
| 117 | + className, |
| 118 | + children, |
| 119 | + ...props |
| 120 | + }: SidebarItemProps): React.ReactElement => { |
| 121 | + if (props.component === NavLink) |
| 122 | + (props as any).activeClassName = classes.active |
| 123 | + return ( |
| 124 | + <ListItem |
| 125 | + {...props} |
| 126 | + button |
| 127 | + className={classNames(classes.root, className)} |
| 128 | + > |
| 129 | + {children} |
| 130 | + </ListItem> |
| 131 | + ) |
| 132 | + } |
| 133 | + ) |
| 134 | + export default SidebarItem |
| 135 | + `, |
| 136 | +}) |
0 commit comments