Skip to content

Commit

Permalink
fix: editable text selection
Browse files Browse the repository at this point in the history
  • Loading branch information
segunadebayo committed Sep 19, 2022
1 parent ef722ca commit d5003f9
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 236 deletions.
5 changes: 5 additions & 0 deletions .changeset/modern-mails-develop.md
@@ -0,0 +1,5 @@
---
"@chakra-ui/editable": patch
---

Fix issue where value change triggers text selection unexpected.
1 change: 1 addition & 0 deletions packages/components/editable/package.json
Expand Up @@ -40,6 +40,7 @@
"@chakra-ui/react-use-merge-refs": "workspace:*",
"@chakra-ui/react-use-controllable-state": "workspace:*",
"@chakra-ui/react-use-focus-on-pointer-down": "workspace:*",
"@chakra-ui/react-use-callback-ref": "workspace:*",
"@chakra-ui/react-context": "workspace:*",
"@chakra-ui/react-types": "workspace:*",
"@chakra-ui/shared-utils": "workspace:*"
Expand Down
12 changes: 9 additions & 3 deletions packages/components/editable/src/use-editable.ts
Expand Up @@ -3,9 +3,10 @@ import { useSafeLayoutEffect } from "@chakra-ui/react-use-safe-layout-effect"
import { useUpdateEffect } from "@chakra-ui/react-use-update-effect"
import { useControllableState } from "@chakra-ui/react-use-controllable-state"
import { mergeRefs } from "@chakra-ui/react-use-merge-refs"
import { useCallbackRef } from "@chakra-ui/react-use-callback-ref"
import { ariaAttr, callAllHandlers } from "@chakra-ui/shared-utils"
import { PropGetter } from "@chakra-ui/react-types"
import React, { useCallback, useRef, useState } from "react"
import { useCallback, useRef, useState } from "react"

export interface UseEditableProps {
/**
Expand Down Expand Up @@ -86,10 +87,12 @@ export function useEditable(props: UseEditableProps = {}) {
submitOnBlur = true,
selectAllOnFocus = true,
placeholder,
onEdit: onEditProp,
onEdit: onEditCallback,
...htmlProps
} = props

const onEditProp = useCallbackRef(onEditCallback)

const defaultIsEditing = Boolean(startWithEditView && !isDisabled)

const [isEditing, setIsEditing] = useState(defaultIsEditing)
Expand Down Expand Up @@ -138,7 +141,10 @@ export function useEditable(props: UseEditableProps = {}) {
}

inputRef.current?.focus()
if (selectAllOnFocus) inputRef.current?.select()

if (selectAllOnFocus) {
inputRef.current?.select()
}

onEditProp?.()
}, [isEditing, onEditProp, selectAllOnFocus])
Expand Down

1 comment on commit d5003f9

@vercel
Copy link

@vercel vercel bot commented on d5003f9 Sep 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.