-
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
Changes from 7 commits
c963476
4470a5b
e37c1fa
3361146
9661a48
c61c8f7
36c4905
a30cfa4
5789472
20280fb
c53c79a
a5cc62b
d4c415a
389010a
0373472
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* 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 './EditableKey.css'; | ||
|
||
type OverrideKeyFn = (path: Array<string | number>, value: any) => void; | ||
|
||
type EditableKeyProps = {| | ||
key?: string, | ||
overrideKeyFn: OverrideKeyFn, | ||
|}; | ||
|
||
export default function EditableKey({ | ||
key = '', | ||
overrideKeyFn, | ||
}: EditableKeyProps) { | ||
const [editableKey, setEditableKey] = useState(key); | ||
const [isValid, setIsValid] = useState(false); | ||
const inputRef = useRef<HTMLInputElement | null>(null); | ||
|
||
useEffect( | ||
() => { | ||
if (inputRef.current !== null) { | ||
inputRef.current.focus(); | ||
} | ||
}, | ||
[inputRef], | ||
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. You shouldn't need to specify a ref as a dependency. |
||
); | ||
|
||
const handleChange = useCallback( | ||
({target}) => { | ||
const value = target.value.trim(); | ||
|
||
if (value) { | ||
setIsValid(true); | ||
} else { | ||
setIsValid(false); | ||
} | ||
|
||
setEditableKey(value); | ||
}, | ||
[overrideKeyFn], | ||
); | ||
|
||
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) { | ||
overrideKeyFn(editableKey); | ||
} else if (eventKey === 'Escape') { | ||
setEditableKey(key); | ||
} | ||
}, | ||
[editableKey, setEditableKey, isValid, key, overrideKeyFn], | ||
); | ||
|
||
return ( | ||
<input | ||
className={styles.Input} | ||
onChange={handleChange} | ||
onKeyDown={handleKeyDown} | ||
ref={inputRef} | ||
type="text" | ||
value={editableKey} | ||
/> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,10 +7,11 @@ | |
* @flow | ||
*/ | ||
|
||
import React, {Fragment, useCallback, useRef, useState} from 'react'; | ||
import React, {Fragment, useEffect, 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; | ||
|
||
|
@@ -27,20 +28,38 @@ export default function EditableValue({ | |
path, | ||
value, | ||
}: EditableValueProps) { | ||
const [isValid, setIsValid] = useState(true); | ||
const [hasPendingChanges, setHasPendingChanges] = useState(false); | ||
const [editableValue, setEditableValue] = useState(value); | ||
const [stringifiedValue, setStringifiedValue] = useState( | ||
JSON.stringify(value), | ||
); | ||
const [editableValue, setEditableValue] = useState(stringifiedValue); | ||
const inputRef = useRef<HTMLInputElement | null>(null); | ||
|
||
if (hasPendingChanges && editableValue === value) { | ||
useEffect( | ||
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. If the value changes we need to update the stringified version of it. 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. 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. Rather, that looks like it should be a 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 completely removed this bit. Thanks for the input! |
||
() => { | ||
setStringifiedValue(JSON.stringify(value)); | ||
}, | ||
[value], | ||
); | ||
|
||
if (hasPendingChanges && editableValue === stringifiedValue) { | ||
setHasPendingChanges(false); | ||
} | ||
|
||
const handleChange = useCallback( | ||
({target}) => { | ||
if (dataType === 'boolean') { | ||
setEditableValue(target.checked); | ||
setEditableValue(JSON.stringify(target.checked)); | ||
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 +69,15 @@ export default function EditableValue({ | |
|
||
const handleReset = useCallback( | ||
() => { | ||
setEditableValue(value); | ||
setEditableValue(stringifiedValue); | ||
setHasPendingChanges(false); | ||
setIsValid(true); | ||
|
||
if (inputRef.current !== null) { | ||
inputRef.current.focus(); | ||
} | ||
}, | ||
[value], | ||
[stringifiedValue], | ||
); | ||
|
||
const handleKeyDown = useCallback( | ||
|
@@ -67,71 +87,59 @@ 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 (value !== 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(stringifiedValue); | ||
setHasPendingChanges(false); | ||
setIsValid(true); | ||
} | ||
}, | ||
[editableValue, dataType, overrideValueFn, path, value], | ||
[editableValue, isValid, dataType, overrideValueFn, path, value], | ||
); | ||
|
||
// Render different input types based on the dataType | ||
let type = 'text'; | ||
if (dataType === 'boolean') { | ||
type = 'checkbox'; | ||
} else if (dataType === 'number') { | ||
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. 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 commentThe reason will be displayed to describe this comment to others. Learn more. 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 thought it would be cool to keep this as it is. 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.
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 commentThe reason will be displayed to describe this comment to others. Learn more. I'll just go ahead and make this change. |
||
type = 'number'; | ||
} | ||
|
||
let inputValue = value == null ? '' : value; | ||
let inputValue = value === undefined ? '' : stringifiedValue; | ||
if (hasPendingChanges) { | ||
inputValue = editableValue == null ? '' : editableValue; | ||
inputValue = editableValue; | ||
} | ||
|
||
let placeholder = ''; | ||
if (value === null) { | ||
placeholder = '(null)'; | ||
} else if (value === undefined) { | ||
if (value === 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} | ||
/> | ||
)} | ||
|
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; | ||
} |
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.
key
andref
can't be passed in as props though. (They are extracted and treated specially.) So this will never be anything but undefined (or""
).Can we pick a different name for whatever this is? (One that isn't overloaded in React terminology.) I would just suggest naming the state
value
(andsetValue
).I know this is copying the precedent set by
EditableValue
- but it's borderline mixing controlled and uncontrolled behavior in a way that seems similar similar to this. Maybe we should name the incoming thing e.g.initialValue
or something to be more clear?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.
Hm. Did you mean to use this component for existing keys too? Looks like you're only using it for new keys. Just curious!
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.
How about calling it
name
, going by theObject.getOwnPropertyNames
nomenclature?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.
@bvaughn I initially had this idea and played around with it in the
KeyValue
component.It worked pretty well, but it would add additional complexity to this PR and it would require some small tweaks.
Maybe we can create a new issue for this specific case after this get's merged?
What do you think?