Skip to content

fix: editable for basic value #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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