Skip to content

Commit

Permalink
fix: editable for basic value (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
himself65 authored Oct 2, 2022
1 parent 4345a5a commit 7d0e771
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
21 changes: 17 additions & 4 deletions docs/pages/full/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import {
AppBar,
FormControl, FormControlLabel,
FormControl,
FormControlLabel,
InputLabel,
MenuItem,
Select, Switch,
TextField, Toolbar, Typography
Select,
Switch,
TextField,
Toolbar,
Typography
} from '@mui/material'
import {
applyValue,
Expand Down Expand Up @@ -106,6 +110,7 @@ const IndexPage: React.FC = () => {
const [theme, setTheme] = useState<JsonViewerTheme>('light')
const [src, setSrc] = useState(() => example)
const [displayDataTypes, setDisplayDataTypes] = useState(true)
const [editable, setEditable] = useState(true)
useEffect(() => {
const loop = () => {
setSrc(src => ({
Expand Down Expand Up @@ -135,7 +140,14 @@ const IndexPage: React.FC = () => {
<Toolbar/>
<FormControlLabel
control={<Switch
value={displayDataTypes}
checked={editable}
onChange={event => setEditable(event.target.checked)}
/>}
label='Editable'
/>
<FormControlLabel
control={<Switch
checked={displayDataTypes}
onChange={event => setDisplayDataTypes(event.target.checked)}
/>}
label='DisplayDataTypes'
Expand Down Expand Up @@ -189,6 +201,7 @@ const IndexPage: React.FC = () => {
</FormControl>
<JsonViewer
value={src}
editable={editable}
indentWidth={indent}
theme={theme}
displayDataTypes={displayDataTypes}
Expand Down
6 changes: 3 additions & 3 deletions src/components/DataKeyPair.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ const IconBox = styled(props => <Box {...props} component='span'/>)`

export const DataKeyPair: React.FC<DataKeyPairProps> = (props) => {
const { value, path, nestedIndex } = props
const propsEditable = props.editable ?? false
const propsEditable = props.editable ?? undefined
const storeEditable = useJsonViewerStore(store => store.editable)
const editable = useMemo(() => {
if (storeEditable === false) {
return false
}
if (!propsEditable) {
if (propsEditable === false) {
// props.editable is false which means we cannot provide the suitable way to edit it
return false
}
if (typeof storeEditable === 'function') {
return !!storeEditable(path, value)
}
return propsEditable
return storeEditable
}, [path, propsEditable, storeEditable, value])
const [tempValue, setTempValue] = useState(typeof value === 'function' ? () => value : value)
const depth = path.length
Expand Down

0 comments on commit 7d0e771

Please sign in to comment.