-
-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
184 additions
and
40 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
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import React, { memo, useState, FormEvent, ChangeEvent, useRef } from 'react' | ||
import { useInspectorContext } from '../../../contexts/inspector-context' | ||
import { getSelectedComponent } from '../../../core/selectors/components' | ||
import { useSelector } from 'react-redux' | ||
import { IoIosFlash } from 'react-icons/io' | ||
import { | ||
IconButton, | ||
Flex, | ||
Box, | ||
SimpleGrid, | ||
InputGroup, | ||
InputRightElement, | ||
Input, | ||
ButtonGroup, | ||
} from '@chakra-ui/core' | ||
import useDispatch from '../../../hooks/useDispatch' | ||
import { useForm } from '../../../hooks/useForm' | ||
|
||
const SEPARATOR = '=' | ||
|
||
const CustomPropsPanel = () => { | ||
const dispatch = useDispatch() | ||
const inputRef = useRef<HTMLInputElement>(null) | ||
|
||
const { activePropsRef } = useInspectorContext() | ||
const { props, id } = useSelector(getSelectedComponent) | ||
const { setValue } = useForm() | ||
|
||
const [quickProps, setQuickProps] = useState('') | ||
const [hasError, setError] = useState(false) | ||
|
||
const onDelete = (propsName: string) => { | ||
dispatch.components.deleteProps({ | ||
id, | ||
name: propsName, | ||
}) | ||
} | ||
|
||
const activeProps = activePropsRef.current || [] | ||
const customProps = Object.keys(props).filter( | ||
propsName => !activeProps.includes(propsName), | ||
) | ||
|
||
return ( | ||
<> | ||
<form | ||
onSubmit={(event: FormEvent) => { | ||
event.preventDefault() | ||
|
||
const [name, value] = quickProps.split(SEPARATOR) | ||
|
||
if (name && value) { | ||
setValue(name, value) | ||
setQuickProps('') | ||
setError(false) | ||
} else { | ||
setError(true) | ||
} | ||
}} | ||
> | ||
<InputGroup mb={3} size="sm"> | ||
<InputRightElement | ||
children={<Box as={IoIosFlash} color="gray.300" />} | ||
/> | ||
<Input | ||
ref={inputRef} | ||
isInvalid={hasError} | ||
value={quickProps} | ||
placeholder={`props${SEPARATOR}value`} | ||
onChange={(event: ChangeEvent<HTMLInputElement>) => | ||
setQuickProps(event.target.value) | ||
} | ||
/> | ||
</InputGroup> | ||
</form> | ||
|
||
{customProps.map((propsName, i) => ( | ||
<Flex | ||
alignItems="center" | ||
px={2} | ||
bg={i % 2 === 0 ? 'white' : 'gray.50'} | ||
fontSize="xs" | ||
justifyContent="space-between" | ||
> | ||
<SimpleGrid width="100%" columns={2} spacing={1}> | ||
<Box fontWeight="bold">{propsName}</Box> | ||
<Box>{props[propsName]}</Box> | ||
</SimpleGrid> | ||
|
||
<ButtonGroup display="flex" size="xs" isAttached> | ||
<IconButton | ||
onClick={() => { | ||
setQuickProps(`${propsName}=`) | ||
if (inputRef.current) { | ||
inputRef.current.focus() | ||
} | ||
}} | ||
variant="ghost" | ||
size="xs" | ||
aria-label="edit" | ||
icon="edit" | ||
/> | ||
<IconButton | ||
onClick={() => onDelete(propsName)} | ||
variant="ghost" | ||
size="xs" | ||
aria-label="delete" | ||
icon="small-close" | ||
/> | ||
</ButtonGroup> | ||
</Flex> | ||
))} | ||
</> | ||
) | ||
} | ||
|
||
export default memo(CustomPropsPanel) |
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,32 @@ | ||
import React, { useContext, useRef, MutableRefObject } from 'react' | ||
|
||
interface InspectorContextInterface { | ||
activePropsRef: MutableRefObject<string[] | null> | ||
} | ||
|
||
const InspectorContext = React.createContext<InspectorContextInterface>({ | ||
activePropsRef: React.createRef(), | ||
}) | ||
|
||
interface InspectorProviderProps { | ||
children: React.ReactNode | ||
} | ||
|
||
function InspectorProvider(props: InspectorProviderProps) { | ||
const activePropsRef = useRef<string[] | null>(null) | ||
|
||
return ( | ||
<InspectorContext.Provider | ||
value={{ | ||
activePropsRef, | ||
}} | ||
{...props} | ||
/> | ||
) | ||
} | ||
|
||
function useInspectorContext() { | ||
return useContext(InspectorContext) | ||
} | ||
|
||
export { InspectorProvider, useInspectorContext } |
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