Skip to content

Commit 0ace85a

Browse files
committed
Address PR review feedback (#6840)
- assign generated IDs to externally appended PII pattern rows - cover editing appended rows without remounting
1 parent 63337d3 commit 0ace85a

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

apps/sim/components/pii/custom-patterns-editor.test.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,26 @@ describe('CustomPatternsEditor', () => {
8282
expect(onChange).toHaveBeenCalledWith([{ name: 'X', regex: 'b+', replacement: '<X>' }])
8383
})
8484

85-
it('preserves an existing row while rows are appended and truncated', () => {
85+
it('preserves rows while they are appended, edited, and truncated', () => {
8686
const onChange = vi.fn()
8787
const firstPattern = row('a+')
8888
renderEditor([firstPattern], onChange)
8989

9090
const firstRegexInput = container.querySelector('input[value="a+"]') as HTMLInputElement
9191
firstRegexInput.focus()
9292

93-
renderEditor([firstPattern, row('b+')], onChange)
93+
const secondPattern = row('b+')
94+
renderEditor([firstPattern, secondPattern], onChange)
9495
expect(container.querySelector('input[value="a+"]')).toBe(firstRegexInput)
9596
expect(document.activeElement).toBe(firstRegexInput)
9697

98+
const secondRegexInput = container.querySelector('input[value="b+"]') as HTMLInputElement
99+
secondRegexInput.focus()
100+
renderEditor([firstPattern, { ...secondPattern, regex: 'b*' }], onChange)
101+
expect(container.querySelector('input[value="b*"]')).toBe(secondRegexInput)
102+
expect(document.activeElement).toBe(secondRegexInput)
103+
104+
firstRegexInput.focus()
97105
renderEditor([firstPattern], onChange)
98106
expect(container.querySelector('input[value="a+"]')).toBe(firstRegexInput)
99107
expect(document.activeElement).toBe(firstRegexInput)

apps/sim/components/pii/custom-patterns-editor.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client'
22

3-
import { useState } from 'react'
3+
import { useEffect, useState } from 'react'
44
import { Chip, ChipInput } from '@sim/emcn'
55
import { Plus, Trash } from '@sim/emcn/icons'
66
import { generateShortId } from '@sim/utils/id'
@@ -28,6 +28,14 @@ interface CustomPatternsEditorProps {
2828
export function CustomPatternsEditor({ patterns, onChange }: CustomPatternsEditorProps) {
2929
const [patternIds, setPatternIds] = useState(() => patterns.map(() => generateShortId()))
3030

31+
useEffect(() => {
32+
setPatternIds((current) => {
33+
if (current.length === patterns.length) return current
34+
if (current.length > patterns.length) return current.slice(0, patterns.length)
35+
return [...current, ...patterns.slice(current.length).map(() => generateShortId())]
36+
})
37+
}, [patterns.length])
38+
3139
function updateRow(index: number, patch: Partial<CustomPiiPattern>) {
3240
onChange(patterns.map((pattern, i) => (i === index ? { ...pattern, ...patch } : pattern)))
3341
}

0 commit comments

Comments
 (0)