Skip to content

Commit

Permalink
fix: pre-select active anatomy preset
Browse files Browse the repository at this point in the history
  • Loading branch information
martastain committed Dec 12, 2023
1 parent 551272a commit eb17a90
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/pages/SettingsPage/AnatomyPresets/AnatomyPresets.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState, useMemo } from 'react'
import { useEffect, useState, useMemo, useRef } from 'react'

import { Dialog } from 'primereact/dialog'
import { InputText } from 'primereact/inputtext'
Expand Down Expand Up @@ -36,13 +36,26 @@ const AnatomyPresets = () => {
const [showNameDialog, setShowNameDialog] = useState(false)
const [newPresetName, setNewPresetName] = useState('')

const nameInputRef = useRef(null)

//
// Hooks
//

// get presets lists data
const { data: presetList = [], isLoading } = useGetAnatomyPresetsQuery()

useEffect(() => {
// preselect primary preset if there is one
// otherwise select default preset
const primaryPreset = presetList.find((p) => p.primary === 'PRIMARY')
if (primaryPreset) {
setSelectedPreset(primaryPreset.name)
} else {
setSelectedPreset('_')
}
}, [presetList])

const isSelectedPrimary = useMemo(() => {
// find preset in list
const preset = presetList.find((p) => p.name === selectedPreset)
Expand All @@ -68,6 +81,15 @@ const AnatomyPresets = () => {
return !isEqual(originalData, newData)
}, [newData, originalData])

useEffect(() => {
// focus input when dialog is shown
if (showNameDialog && nameInputRef.current) {
setTimeout(() => {
nameInputRef.current.focus()
}, 100)
}
}, [showNameDialog, nameInputRef])

//
// Actions
//
Expand Down Expand Up @@ -164,6 +186,7 @@ const AnatomyPresets = () => {
>
<InputText
value={newPresetName}
ref={nameInputRef}
onChange={(e) => setNewPresetName(e.target.value)}
placeholder="Preset name"
style={{
Expand Down

0 comments on commit eb17a90

Please sign in to comment.