Skip to content

Commit

Permalink
feat: add autoselect on focus (#807)
Browse files Browse the repository at this point in the history
* RM#86572
  • Loading branch information
gca-axelor authored and lme-axelor committed Nov 26, 2024
1 parent bbbcda3 commit 97930b6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
5 changes: 5 additions & 0 deletions changelogs/unreleased/86572.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"title": "Numerical inputs: add auto-select on focus",
"type": "feat",
"packages": "ui"
}
13 changes: 11 additions & 2 deletions packages/ui/src/components/molecules/Increment/Increment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import React, {useCallback, useEffect, useMemo, useState} from 'react';
import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';
import {KeyboardTypeOptions, Platform, StyleSheet, View} from 'react-native';
import {
formatNumber as _format,
Expand Down Expand Up @@ -66,6 +66,7 @@ const Increment = ({
}: IncrementProps) => {
const Colors = useThemeColor();
const cutDecimalExcess = useDigitFormat();
const inputRef = useRef<any>(null);

const [valueQty, setValueQty] = useState<string>(value);

Expand Down Expand Up @@ -158,6 +159,13 @@ const Increment = ({
onBlur();
};

const handleFocus = () => {
if (inputRef.current) {
inputRef.current.setSelection(0, valueQty.length);
}
onFocus();
};

const styles = useMemo(() => getStyles(Colors), [Colors]);

return (
Expand All @@ -172,11 +180,12 @@ const Increment = ({
<View
style={[styles.inputLine, isBigButton ? styles.fixedInputWidth : null]}>
<Input
inputRef={inputRef}
style={[styles.input, inputStyle]}
value={valueQty != null ? String(valueQty) : ''}
onChange={input => setValueQty(input)}
keyboardType={keyboardType}
onSelection={onFocus}
onSelection={handleFocus}
onEndFocus={handleEndInput}
readOnly={readonly}
/>
Expand Down

0 comments on commit 97930b6

Please sign in to comment.