-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16501 from Nexus-Mods/fblo-mutable-index
added index input element + handlers
- Loading branch information
Showing
3 changed files
with
122 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/extensions/file_based_loadorder/views/loadOrderIndex.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import React, { useState } from 'react'; | ||
import { ILoadOrderEntry } from '../../../types/api'; | ||
import { IExtensionApi, LoadOrder } from '../../../types/api'; | ||
|
||
interface IProps { | ||
className?: string; | ||
api: IExtensionApi; | ||
item: ILoadOrderEntry; | ||
loadOrder: LoadOrder; | ||
currentPosition: number; | ||
lockedEntriesCount: number; | ||
isLocked: (item: ILoadOrderEntry) => boolean | ||
onApplyIndex: (idx: number) => void; | ||
} | ||
|
||
export function LoadOrderIndexInput(props: IProps) { | ||
const { item, loadOrder, currentPosition, lockedEntriesCount, onApplyIndex } = props; | ||
|
||
// Valid ranges. | ||
const startIndex = lockedEntriesCount + 1; | ||
const maxIndex = loadOrder.length; | ||
|
||
const [inputValue, setInputValue] = useState(currentPosition.toString()); | ||
|
||
const handleInputChange = React.useCallback((event: React.ChangeEvent<HTMLInputElement>) => { | ||
// Meant to be used as validation only. | ||
try { | ||
const newIndex = parseInt(event.target.value, 10); | ||
setInputValue(newIndex.toString()); | ||
} catch (err) { | ||
setInputValue(currentPosition.toString()); | ||
} | ||
}, [currentPosition, maxIndex, startIndex]); | ||
|
||
const handleKeyPress = React.useCallback((event: React.KeyboardEvent<HTMLInputElement>) => { | ||
if (event.key === 'Enter') { | ||
// Apply new index | ||
let newIndex = parseInt(inputValue, 10); | ||
newIndex = Math.max(startIndex, Math.min(maxIndex, newIndex)); | ||
onApplyIndex(newIndex); | ||
setInputValue(newIndex.toString()); | ||
} | ||
if (event.key === 'Escape') { | ||
// reset | ||
setInputValue(currentPosition.toString()); | ||
} | ||
}, [currentPosition, maxIndex, startIndex, inputValue]); | ||
|
||
React.useEffect(() => { | ||
setInputValue(currentPosition.toString()); | ||
}, [currentPosition]); | ||
|
||
return props.isLocked(item) ? ( | ||
<p className={props.className}>{inputValue}</p> | ||
) : ( | ||
<div className={props.className}> | ||
<input | ||
type='number' | ||
value={inputValue} | ||
onChange={handleInputChange} | ||
onKeyDown={handleKeyPress} | ||
min={startIndex} | ||
max={maxIndex} | ||
defaultValue={currentPosition} | ||
/> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters