Skip to content

Commit

Permalink
feat: add Highlight component
Browse files Browse the repository at this point in the history
* add high light component

* add tabs

* remove log

* improve highlight support

Co-authored-by: shinework <[email protected]>
  • Loading branch information
PierreCrb and baptadn authored Sep 9, 2022
1 parent 466dcde commit 6de48fc
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 38 deletions.
1 change: 1 addition & 0 deletions src/components/editor/ComponentPreview.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const componentsToTest = [
'Textarea',
'CircularProgress',
'Heading',
'Highlight',
'Tag',
'Switch',
'FormLabel',
Expand Down
4 changes: 4 additions & 0 deletions src/components/editor/ComponentPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import SelectPreview from '~components/editor/previews/SelectPreview'
import NumberInputPreview from '~components/editor/previews/NumberInputPreview'
import BreadcrumbPreview from './previews/BreadcrumbPreview'
import BreadcrumbItemPreview from './previews/BreadcrumbItemPreview'
import HighlightPreview from './previews/HighlightPreview'

const ComponentPreview: React.FC<{
componentName: string
Expand Down Expand Up @@ -154,6 +155,9 @@ const ComponentPreview: React.FC<{
return <SelectPreview component={component} />
case 'NumberInput':
return <NumberInputPreview component={component} />
case 'Highlight':
return <HighlightPreview component={component} />

default:
return null
}
Expand Down
15 changes: 15 additions & 0 deletions src/components/editor/previews/HighlightPreview.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react'
import { useInteractive } from '~hooks/useInteractive'
import { Box, Highlight } from '@chakra-ui/react'

const HighlightPreview: React.FC<IPreviewProps> = ({ component }) => {
const { props, ref } = useInteractive(component, true, true)

return (
<Box {...props} ref={ref}>
<Highlight {...component.props} styles={component.props} />
</Box>
)
}

export default HighlightPreview
2 changes: 2 additions & 0 deletions src/components/inspector/panels/Panels.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import NumberInputPanel from '~components/inspector/panels/components/NumberInpu
import AspectRatioPanel from '~components/inspector/panels/components/AspectRatioPanel'
import BreadcrumbPanel from '~components/inspector/panels/components/BreadcrumbPanel'
import BreadcrumbItemPanel from '~components/inspector/panels/components/BreadcrumbItemPanel'
import HighlightPanel from '~components/inspector/panels/components/HighlightPanel'

const Panels: React.FC<{ component: IComponent; isRoot: boolean }> = ({
component,
Expand Down Expand Up @@ -81,6 +82,7 @@ const Panels: React.FC<{ component: IComponent; isRoot: boolean }> = ({
{type === 'Textarea' && <TextareaPanel />}
{type === 'CircularProgress' && <CircularProgressPanel />}
{type === 'Heading' && <HeadingPanel />}
{type === 'Highlight' && <HighlightPanel />}
{type === 'SimpleGrid' && <SimpleGridPanel />}
{type === 'Switch' && <SwitchPanel />}
{type === 'Alert' && <AlertPanel />}
Expand Down
1 change: 0 additions & 1 deletion src/components/inspector/panels/StylesPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { memo } from 'react'
import { Accordion } from '@chakra-ui/react'

import PaddingPanel from '~components/inspector/panels/styles/PaddingPanel'
import DimensionPanel from '~components/inspector/panels/styles/DimensionPanel'
import BorderPanel from '~components/inspector/panels/styles/BorderPanel'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import React from 'react'
import ChildrenControl from '~components/inspector/controls/ChildrenControl'

const FormErrorMessagePanel = () => {
return (
<>
<ChildrenControl />
</>
)
}
const FormErrorMessagePanel = () => <ChildrenControl />

export default FormErrorMessagePanel
12 changes: 12 additions & 0 deletions src/components/inspector/panels/components/HighlightPanel.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { memo } from 'react'
import ChildrenControl from '~components/inspector/controls/ChildrenControl'
import TextControl from '~components/inspector/controls/TextControl'

const HighlightPanel = () => (
<>
<ChildrenControl />
<TextControl label="Query" name="query" />
</>
)

export default memo(HighlightPanel)
2 changes: 2 additions & 0 deletions src/componentsList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export const menuItems: MenuItems = {
},
Grid: {},
Heading: {},
Highlight: {},
Icon: {},
IconButton: {},
Image: {},
Expand Down Expand Up @@ -141,6 +142,7 @@ export const componentsList: ComponentType[] = [
'FormLabel',
'Grid',
'Heading',
'Highlight',
'Icon',
'IconButton',
'Image',
Expand Down
5 changes: 3 additions & 2 deletions src/hooks/useInteractive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import { getShowLayout, getFocusedComponent } from '../core/selectors/app'

export const useInteractive = (
component: IComponent,
enableVisualHelper: boolean = false,
enableVisualHelper = false,
withoutComponentProps = false,
) => {
const dispatch = useDispatch()
const showLayout = useSelector(getShowLayout)
Expand All @@ -24,7 +25,7 @@ export const useInteractive = (

const ref = useRef<HTMLDivElement>(null)
let props = {
...component.props,
...(withoutComponentProps ? {} : component.props),
onMouseOver: (event: MouseEvent) => {
event.stopPropagation()
dispatch.components.hover(component.id)
Expand Down
1 change: 1 addition & 0 deletions src/react-app-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type ComponentType =
| 'FormErrorMessage'
| 'Grid'
| 'Heading'
| 'Highlight'
| 'Icon'
| 'IconButton'
| 'Image'
Expand Down
76 changes: 48 additions & 28 deletions src/utils/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,41 @@ type BuildBlockParams = {
forceBuildBlock?: boolean
}

const buildStyledProps = (propsNames: string[], childComponent: IComponent) => {
let propsContent = ``

propsNames.forEach((propName: string) => {
const propsValue = childComponent.props[propName]

if (
propName.toLowerCase().includes('icon') &&
childComponent.type !== 'Icon'
) {
if (Object.keys(icons).includes(propsValue)) {
let operand = `={<${propsValue} />}`

propsContent += `${propName}${operand} `
}
} else if (propName !== 'children' && propsValue) {
let operand = `='${propsValue}'`

if (propsValue === true || propsValue === 'true') {
operand = ``
} else if (
propsValue === 'false' ||
isBoolean(propsValue) ||
!isNaN(propsValue)
) {
operand = `={${propsValue}}`
}

propsContent += `${propName}${operand} `
}
})

return propsContent
}

const buildBlock = ({
component,
components,
Expand All @@ -53,34 +88,19 @@ const buildBlock = ({
return true
})

propsNames.forEach((propName: string) => {
const propsValue = childComponent.props[propName]

if (
propName.toLowerCase().includes('icon') &&
childComponent.type !== 'Icon'
) {
if (Object.keys(icons).includes(propsValue)) {
let operand = `={<${propsValue} />}`

propsContent += `${propName}${operand} `
}
} else if (propName !== 'children' && propsValue) {
let operand = `='${propsValue}'`

if (propsValue === true || propsValue === 'true') {
operand = ``
} else if (
propsValue === 'false' ||
isBoolean(propsValue) ||
!isNaN(propsValue)
) {
operand = `={${propsValue}}`
}

propsContent += `${propName}${operand} `
}
})
// Special case for Highlight component
if (componentName === 'Highlight') {
const [query, children, ...restProps] = propsNames
propsContent += buildStyledProps([query, children], childComponent)

propsContent += `styles={{${restProps
.filter(propName => childComponent.props[propName])
.map(
propName => `${propName}:'${childComponent.props[propName]}'`,
)}}}`
} else {
propsContent += buildStyledProps(propsNames, childComponent)
}

if (
typeof childComponent.props.children === 'string' &&
Expand Down
6 changes: 6 additions & 0 deletions src/utils/defaultProps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import {
TabProps,
BreadcrumbLinkProps,
ListProps,
HighlightProps,
} from '@chakra-ui/react'

import iconsList from '~iconsList'
Expand Down Expand Up @@ -85,6 +86,7 @@ type PreviewDefaultProps = {
Textarea?: PropsWithForm<TextareaProps>
CircularProgress?: PropsWithForm<CircularProgressProps>
Heading?: PropsWithForm<HeadingProps>
Highlight?: PropsWithForm<HighlightProps>
Tag?: PropsWithForm<TagProps>
SimpleGrid?: PropsWithForm<SimpleGridProps>
Switch?: PropsWithForm<SwitchProps>
Expand Down Expand Up @@ -211,6 +213,10 @@ export const DEFAULT_PROPS: PreviewDefaultProps = {
Heading: {
children: 'Heading title',
},
Highlight: {
children: 'Heading title',
query: 'title',
},
Icon: { icon: 'CopyIcon' },
IconButton: {
'aria-label': 'icon',
Expand Down
1 change: 1 addition & 0 deletions src/utils/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const COMPONENTS: (ComponentType | MetaComponentType)[] = [
'FormErrorMessage',
'Grid',
'Heading',
'Highlight',
'Icon',
'IconButton',
'Image',
Expand Down

0 comments on commit 6de48fc

Please sign in to comment.