Skip to content

Commit

Permalink
fix(editable): chakra-ui#5670 call setPrevValue in onEdit to avoid ou…
Browse files Browse the repository at this point in the history
…tdated data when controlled
  • Loading branch information
takethefake committed Mar 3, 2022
1 parent 577aeb9 commit c38bda3
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changeset/dry-worms-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@chakra-ui/editable": patch
---

Call `setPrevValue` onEdit to avoid an outdated prev value when the field is
controlled
3 changes: 2 additions & 1 deletion packages/editable/src/use-editable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,11 @@ export function useEditable(props: UseEditableProps = {}) {
}, [isEditing, onEditProp, selectAllOnFocus])

const onEdit = useCallback(() => {
setPrevValue(value)
if (isInteractive) {
setIsEditing(true)
}
}, [isInteractive])
}, [isInteractive, value])

const onCancel = useCallback(() => {
setIsEditing(false)
Expand Down
34 changes: 34 additions & 0 deletions packages/editable/stories/editable.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
EditableTextarea,
useEditableControls,
} from "../src"
import { Heading } from "@chakra-ui/layout"

export default {
title: "Components / Forms / Editable",
Expand Down Expand Up @@ -125,3 +126,36 @@ export const TextareaAsInput = () => {
</Editable>
)
}

export const EditableEventHandler = () => {
const [name, setName] = React.useState("")
console.log("State 'name' is ", name)

React.useEffect(() => {
setName("John")
}, [])

return (
<>
<Heading>Name State=[{name}]</Heading>
<Editable
value={name}
onChange={(value) => {
console.log("onChange called with ", value)
}}
onSubmit={(value) => {
console.log("onSubmit called with ", value)
setName(value)
}}
onCancel={(value) => {
console.log("onCancel called with ", value)
setName(value)
}}
placeholder="Enter your name"
>
<EditablePreview />
<EditableInput />
</Editable>
</>
)
}
43 changes: 43 additions & 0 deletions packages/editable/tests/editable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,46 @@ test("startWithEditView when true focuses on the input ", () => {

expect(document.activeElement === input).toBe(true)
})

test.each([{ startWithEditView: true }, { startWithEditView: false }])(
"controlled: sets value correct onCancel, startWithEditView: $startWithEditView",
({ startWithEditView }) => {
const Component = () => {
const [name, setName] = React.useState("")

React.useEffect(() => {
setName("John")
}, [])

return (
<Editable
value={name}
startWithEditView={startWithEditView}
onChange={(value) => {
setName(value)
}}
onSubmit={(value) => {
setName(value)
}}
onCancel={(value) => {
setName(value)
}}
placeholder="Enter your name"
>
<EditablePreview data-testid="preview" />
<EditableInput data-testid="input" />
</Editable>
)
}

render(<Component />)
const input = screen.getByTestId("input")
const preview = screen.getByTestId("preview")
if (!startWithEditView) {
fireEvent.focus(preview)
}
fireEvent.keyDown(input, { key: "Escape" })

expect(preview).toHaveTextContent("John")
},
)

0 comments on commit c38bda3

Please sign in to comment.