Skip to content

Commit 3077553

Browse files
authored
fix(secrets): preserve caret when revealing values (#7246)
1 parent d673548 commit 3077553

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

apps/sim/app/workspace/[workspaceId]/settings/components/secrets/components/secret-value-field/secret-value-field.test.tsx

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ import { createRoot, type Root } from 'react-dom/client'
66
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
77

88
vi.mock('@sim/emcn', () => ({
9-
ChipInput: (props: ComponentProps<'input'>) => <input {...props} />,
9+
ChipInput: ({
10+
inputClassName,
11+
...props
12+
}: ComponentProps<'input'> & { inputClassName?: string }) => (
13+
<input {...props} className={inputClassName} />
14+
),
1015
}))
1116

1217
import { SecretValueField } from '@/app/workspace/[workspaceId]/settings/components/secrets/components/secret-value-field/secret-value-field'
@@ -33,6 +38,26 @@ afterEach(() => {
3338
})
3439

3540
describe('SecretValueField', () => {
41+
it('preserves the caret position when revealing an editable value', () => {
42+
const value = 'editable-secret-value'
43+
act(() => root.render(<SecretValueField value={value} />))
44+
45+
expect(input().value).toBe(value)
46+
expect(input().className).toContain('[-webkit-text-security:disc]')
47+
48+
input().setSelectionRange(15, 15)
49+
act(() => input().focus())
50+
51+
expect(input().value).toBe(value)
52+
expect(input().selectionStart).toBe(15)
53+
expect(input().readOnly).toBe(false)
54+
expect(input().className).not.toContain('[-webkit-text-security:disc]')
55+
56+
act(() => input().blur())
57+
expect(input().value).toBe(value)
58+
expect(input().className).toContain('[-webkit-text-security:disc]')
59+
})
60+
3661
it('lets a read-only viewer reveal an allowed value without making it editable', () => {
3762
act(() => root.render(<SecretValueField value='visible-secret' canEdit={false} canReveal />))
3863

apps/sim/app/workspace/[workspaceId]/settings/components/secrets/components/secret-value-field/secret-value-field.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,11 @@ export function SecretValueField({
5555
const editable = canEdit && !readOnly
5656
const revealable = canEdit || canReveal
5757
const maskActive = revealable && !unmasked && !focused
58+
const visuallyMaskEditableValue = editable && maskActive
5859
const displayValue =
59-
!revealable || (maskActive && value.length > 0) ? BULLET.repeat(VIEWER_MASK_LENGTH) : value
60+
!revealable || (!editable && maskActive && value.length > 0)
61+
? BULLET.repeat(VIEWER_MASK_LENGTH)
62+
: value
6063

6164
return (
6265
<ChipInput
@@ -66,6 +69,7 @@ export function SecretValueField({
6669
value={displayValue}
6770
readOnly
6871
style={style}
72+
inputClassName={visuallyMaskEditableValue ? '[-webkit-text-security:disc]' : undefined}
6973
onChange={(event) => {
7074
if (editable) onChange?.(event.target.value)
7175
}}

0 commit comments

Comments
 (0)