-
Notifications
You must be signed in to change notification settings - Fork 47k
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
[DevTools] Support for adding props | Improved state/props value editing #16700
Merged
bvaughn
merged 15 commits into
facebook:master
from
hristo-kanchev:feature/add-edit-entries
Sep 10, 2019
Merged
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
c963476
Extracted sanitizeForParse
hristo-kanchev 4470a5b
Added canAddEntries flag to InspectedElementTree
hristo-kanchev e37c1fa
Added EditableKey component.
hristo-kanchev 3361146
Added support to add an additional entry.
hristo-kanchev 9661a48
Added support to add more complex data structures in the EditableValu…
hristo-kanchev c61c8f7
Fixed flow error.
hristo-kanchev 36c4905
Removed unneeded fragment.
hristo-kanchev a30cfa4
Renamed EditableKey -> EditableName
hristo-kanchev 5789472
Removed unneeded dependency
hristo-kanchev 20280fb
Removed problematic props to state hook.
hristo-kanchev c53c79a
Prettified changes.
hristo-kanchev a5cc62b
Removed unused import.
hristo-kanchev d4c415a
Fixed shouldStringify check.
hristo-kanchev 389010a
Removed testing props from EditableProps.
hristo-kanchev 0373472
Made some inline tweaks
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
15 changes: 15 additions & 0 deletions
15
packages/react-devtools-shared/src/devtools/views/Components/EditableName.css
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,15 @@ | ||
.Input { | ||
width: 100px; | ||
background: none; | ||
border: 1px solid transparent; | ||
color: var(--color-attribute-name); | ||
border-radius: 0.125rem; | ||
font-family: var(--font-family-monospace); | ||
font-size: var(--font-size-monospace-normal); | ||
} | ||
|
||
.Input:focus { | ||
color: var(--color-attribute-editable-value); | ||
background-color: var(--color-button-background-focus); | ||
outline: none; | ||
} |
75 changes: 75 additions & 0 deletions
75
packages/react-devtools-shared/src/devtools/views/Components/EditableName.js
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,75 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
import React, {useRef, useCallback, useEffect, useState} from 'react'; | ||
import styles from './EditableName.css'; | ||
|
||
type OverrideNameFn = (path: Array<string | number>, value: any) => void; | ||
|
||
type EditableNameProps = {| | ||
initialValue?: string, | ||
overrideNameFn: OverrideNameFn, | ||
|}; | ||
|
||
export default function EditableName({ | ||
initialValue = '', | ||
overrideNameFn, | ||
}: EditableNameProps) { | ||
const [editableName, setEditableName] = useState(initialValue); | ||
const [isValid, setIsValid] = useState(false); | ||
const inputRef = useRef<HTMLInputElement | null>(null); | ||
|
||
useEffect(() => { | ||
if (inputRef.current !== null) { | ||
inputRef.current.focus(); | ||
} | ||
}, []); | ||
|
||
const handleChange = useCallback( | ||
({target}) => { | ||
const value = target.value.trim(); | ||
|
||
if (value) { | ||
setIsValid(true); | ||
} else { | ||
setIsValid(false); | ||
} | ||
|
||
setEditableName(value); | ||
}, | ||
[overrideNameFn], | ||
); | ||
|
||
const handleKeyDown = useCallback( | ||
event => { | ||
// Prevent keydown events from e.g. change selected element in the tree | ||
event.stopPropagation(); | ||
|
||
const eventKey = event.key; | ||
|
||
if ((eventKey === 'Enter' || eventKey === 'Tab') && isValid) { | ||
overrideNameFn(editableName); | ||
} else if (eventKey === 'Escape') { | ||
setEditableName(initialValue); | ||
} | ||
}, | ||
[editableName, setEditableName, isValid, initialValue, overrideNameFn], | ||
); | ||
|
||
return ( | ||
<input | ||
className={styles.Input} | ||
onChange={handleChange} | ||
onKeyDown={handleKeyDown} | ||
ref={inputRef} | ||
type="text" | ||
value={editableName} | ||
/> | ||
); | ||
} |
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 |
---|---|---|
|
@@ -11,36 +11,45 @@ import React, {Fragment, useCallback, useRef, useState} from 'react'; | |
import Button from '../Button'; | ||
import ButtonIcon from '../ButtonIcon'; | ||
import styles from './EditableValue.css'; | ||
import {sanitizeForParse} from '../../utils'; | ||
|
||
type OverrideValueFn = (path: Array<string | number>, value: any) => void; | ||
|
||
type EditableValueProps = {| | ||
dataType: string, | ||
overrideValueFn: OverrideValueFn, | ||
path: Array<string | number>, | ||
value: any, | ||
initialValue: any, | ||
|}; | ||
|
||
export default function EditableValue({ | ||
dataType, | ||
overrideValueFn, | ||
path, | ||
value, | ||
initialValue, | ||
}: EditableValueProps) { | ||
const [isValid, setIsValid] = useState(true); | ||
const [hasPendingChanges, setHasPendingChanges] = useState(false); | ||
const [editableValue, setEditableValue] = useState(value); | ||
const [editableValue, setEditableValue] = useEditableValue(initialValue); | ||
const inputRef = useRef<HTMLInputElement | null>(null); | ||
|
||
if (hasPendingChanges && editableValue === value) { | ||
if (hasPendingChanges && editableValue === JSON.stringify(initialValue)) { | ||
setHasPendingChanges(false); | ||
} | ||
|
||
const handleChange = useCallback( | ||
({target}) => { | ||
if (dataType === 'boolean') { | ||
setEditableValue(target.checked); | ||
setEditableValue(target.checked, {shouldStringify: true}); | ||
overrideValueFn(path, target.checked); | ||
} else { | ||
let isValidJSON = false; | ||
try { | ||
JSON.parse(sanitizeForParse(target.value)); | ||
isValidJSON = true; | ||
} catch (error) {} | ||
|
||
setIsValid(isValidJSON); | ||
setEditableValue(target.value); | ||
} | ||
setHasPendingChanges(true); | ||
|
@@ -50,14 +59,15 @@ export default function EditableValue({ | |
|
||
const handleReset = useCallback( | ||
() => { | ||
setEditableValue(value); | ||
setEditableValue(initialValue, {shouldStringify: true}); | ||
setHasPendingChanges(false); | ||
setIsValid(true); | ||
|
||
if (inputRef.current !== null) { | ||
inputRef.current.focus(); | ||
} | ||
}, | ||
[value], | ||
[initialValue], | ||
); | ||
|
||
const handleKeyDown = useCallback( | ||
|
@@ -67,71 +77,60 @@ export default function EditableValue({ | |
|
||
const {key} = event; | ||
|
||
if (key === 'Enter') { | ||
if (dataType === 'number') { | ||
const parsedValue = parseFloat(editableValue); | ||
if (!Number.isNaN(parsedValue)) { | ||
overrideValueFn(path, parsedValue); | ||
} | ||
} else { | ||
overrideValueFn(path, editableValue); | ||
if (key === 'Enter' && isValid) { | ||
const parsedEditableValue = JSON.parse(sanitizeForParse(editableValue)); | ||
|
||
if (initialValue !== parsedEditableValue) { | ||
overrideValueFn(path, parsedEditableValue); | ||
} | ||
|
||
// Don't reset the pending change flag here. | ||
// The inspected fiber won't be updated until after the next "inspectElement" message. | ||
// We'll reset that flag during a subsequent render. | ||
} else if (key === 'Escape') { | ||
setEditableValue(value); | ||
setEditableValue(initialValue, {shouldStringify: true}); | ||
setHasPendingChanges(false); | ||
setIsValid(true); | ||
} | ||
}, | ||
[editableValue, dataType, overrideValueFn, path, value], | ||
[editableValue, isValid, dataType, overrideValueFn, path, initialValue], | ||
); | ||
|
||
// Render different input types based on the dataType | ||
let type = 'text'; | ||
if (dataType === 'boolean') { | ||
type = 'checkbox'; | ||
} else if (dataType === 'number') { | ||
type = 'number'; | ||
} | ||
|
||
let inputValue = value == null ? '' : value; | ||
let inputValue = | ||
initialValue === undefined ? '' : JSON.stringify(initialValue); | ||
if (hasPendingChanges) { | ||
inputValue = editableValue == null ? '' : editableValue; | ||
inputValue = editableValue; | ||
} | ||
|
||
let placeholder = ''; | ||
if (value === null) { | ||
placeholder = '(null)'; | ||
} else if (value === undefined) { | ||
if (initialValue === undefined) { | ||
placeholder = '(undefined)'; | ||
} else if (dataType === 'string') { | ||
placeholder = '(string)'; | ||
} else { | ||
placeholder = 'Enter valid JSON'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be good to have this placeholder for non |
||
} | ||
|
||
return ( | ||
<Fragment> | ||
{dataType === 'boolean' && ( | ||
<label className={styles.CheckboxLabel}> | ||
<input | ||
checked={inputValue} | ||
checked={inputValue === 'true'} | ||
className={styles.Checkbox} | ||
onChange={handleChange} | ||
onKeyDown={handleKeyDown} | ||
ref={inputRef} | ||
type={type} | ||
type="checkbox" | ||
/> | ||
</label> | ||
)} | ||
{dataType !== 'boolean' && ( | ||
<input | ||
className={styles.Input} | ||
className={isValid ? styles.Input : styles.Invalid} | ||
onChange={handleChange} | ||
onKeyDown={handleKeyDown} | ||
placeholder={placeholder} | ||
ref={inputRef} | ||
type={type} | ||
type="text" | ||
value={inputValue} | ||
/> | ||
)} | ||
|
@@ -147,3 +146,22 @@ export default function EditableValue({ | |
</Fragment> | ||
); | ||
} | ||
|
||
function useEditableValue(initialValue: any): [any, Function] { | ||
const [editableValue, setEditableValue] = useState( | ||
JSON.stringify(initialValue), | ||
); | ||
|
||
function setEditableValueWithStringify( | ||
value: any, | ||
{shouldStringify}: Object = {}, | ||
) { | ||
if (shouldStringify) { | ||
setEditableValue(JSON.stringify(value)); | ||
} else { | ||
setEditableValue(value); | ||
} | ||
} | ||
|
||
return [editableValue, setEditableValueWithStringify]; | ||
} |
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 |
---|---|---|
|
@@ -46,3 +46,8 @@ | |
font-style: italic; | ||
padding-left: 0.75rem; | ||
} | ||
|
||
.AddEntry { | ||
display: flex; | ||
padding-left: 0.9rem; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need this anymore. Now we have the ability to change the value from a number to an array for example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it expected that once a prop becomes a boolean, you can't change it back?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought it would be cool to keep this as it is.
Users can still override it via Add new prop -> type
name
and set the value to"hello world"
for example.Do you think it would be better if we just displayed
true
/false
instead of the checkbox?That way we can change it more easily.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That seems pretty non-obvious though. (I don't think it would occur to people to do this).
Yeah 😄 I think we should do this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll just go ahead and make this change.