Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task/review numberselector #68

Merged
merged 5 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions example/src/NumberField/NumberFieldPage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useState } from 'react';
import { StyleSheet, View } from 'react-native';
import { NumberField, Screen, useTheme } from 'smartway-react-native-ui';

Expand All @@ -14,34 +14,46 @@ export const NumberFieldPage = () => {
marginVertical: 10,
},
});
const value = '999';

return (
<Screen style={styles.container}>
<View style={{ flexDirection: 'row' }}>
<NumberField style={styles.numberField} value="999" size="m" state="prefilled" />
<NumberField style={styles.numberField} value="999" state="prefilled-focused" />
<NumberField style={styles.numberField} value="999" state="filled-focused" />
<NumberField style={styles.numberField} value="999" state="filled" />
<NumberField style={styles.numberField} value="999" state="readonly" />
<NumberField style={styles.numberField} value="999" state="error" />
<NumberField
style={styles.numberField}
value={value}
minValue={0}
maxValue={100}
size="m"
state="prefilled"
/>
<NumberField style={styles.numberField} value={value} state="prefilled-focused" />
<NumberField style={styles.numberField} value={value} state="filled-focused" />
<NumberField style={styles.numberField} value={value} state="filled" />
<NumberField style={styles.numberField} value={value} state="readonly" />
<NumberField style={styles.numberField} value={value} state="error" />
</View>
<View style={{ flexDirection: 'row' }}>
<NumberField style={styles.numberField} value="999" size="s" state="prefilled" />
<NumberField style={styles.numberField} value={value} size="s" state="prefilled" />
<NumberField
style={styles.numberField}
value="999"
value={value}
size="s"
state="prefilled-focused"
/>
<NumberField
style={styles.numberField}
value="999"
value={value}
size="s"
state="filled-focused"
/>
<NumberField style={styles.numberField} value="999" size="s" state="filled" />
<NumberField style={styles.numberField} value="999" size="s" state="readonly" />
<NumberField style={styles.numberField} value="999" size="s" state="error" />
<NumberField style={styles.numberField} value={value} size="s" state="filled" />
<NumberField style={styles.numberField} value={value} size="s" state="readonly" />
<NumberField style={styles.numberField} value={value} size="s" state="error" />
</View>
<View style={{ flexDirection: 'row' }}>
{/* Example without value to hilight the last value on cleared case */}
<NumberField style={styles.numberField} size="m" state="prefilled" />
</View>
</Screen>
);
Expand Down
2 changes: 1 addition & 1 deletion example/src/NumberSelector/NumberSelectorPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { StyleSheet, View } from 'react-native';
import { NumberSelector, Screen } from 'smartway-react-native-ui';

export const NumberSelectorPage = () => {
const [value, setValue] = useState<number>(0);
const [value, setValue] = useState<number>(10);
const [otherValue, setOtherValue] = useState<number>(0);

const styles = StyleSheet.create({
Expand Down
236 changes: 179 additions & 57 deletions src/components/numberField/NumberField.tsx
Original file line number Diff line number Diff line change
@@ -1,70 +1,192 @@
import React from 'react';
import React, { useEffect, useState } from 'react';
import { StyleSheet, TextInput, TextInputBase } from 'react-native';
import { useTheme } from '../../styles/themes';

type FieldBaseProps = React.ComponentProps<typeof TextInputBase>;
interface NumberFieldProps extends FieldBaseProps {
state?: 'readonly' | 'filled' | 'prefilled' | 'filled-focused' | 'prefilled-focused' | 'error';
size?: 'm' | 's';
minValue?: number;
maxValue?: number;
}

export const NumberField = (props: NumberFieldProps) => {
const theme = useTheme();
export const NumberField = React.forwardRef<TextInput, NumberFieldProps>(
(
{
state = 'prefilled',
size = 'm',
minValue = 0,
maxValue = 999,
...props
}: NumberFieldProps,
ref,
) => {
const theme = useTheme();

let borderColor = undefined;
let textColor = undefined;
let backgroundColor = undefined;
switch (props.state) {
case 'prefilled':
textColor = theme.sw.colors.neutral[500];
backgroundColor = theme.sw.colors.neutral[500] + theme.sw.transparency[8];
break;
case 'filled-focused':
borderColor = theme.sw.colors.primary.main;
break;
case 'prefilled-focused':
textColor = theme.sw.colors.primary.main;
borderColor = theme.sw.colors.primary.main;
backgroundColor = theme.sw.colors.primary.main + theme.sw.transparency[16];
break;
case 'filled':
backgroundColor = theme.sw.colors.neutral[500] + theme.sw.transparency[8];
break;
case 'error':
textColor = theme.sw.colors.error.main;
backgroundColor = theme.sw.colors.error.main + theme.sw.transparency[8];
break;
case undefined:
break;
}
const [currentState, setCurrentState] = useState<string>(state);
const [filled, setFilled] = useState<boolean>(false);
const [error, setError] = useState<boolean>(false);
const [focused, setFocused] = useState<boolean>(false);
const [forcedState, setForcedState] = useState<boolean>(true);
const [firstContentChange, setFirstContentChange] = useState<boolean>(true);
const [firstValue, setFirstValue] = useState<string>();
const [value, setValue] = useState<string>(props.value ?? '');
const [lastValue, setLastValue] = useState<string>();

const style = StyleSheet.create({
input: {
borderRadius: props?.size === 's' ? 10 : 18,
height: props?.size === 's' ? 38 : 48,
borderWidth: borderColor ? 1 : 0,
borderColor: borderColor,
useEffect(() => {
if (forcedState) {
setForcedState(false);
return;
}
refreshCurrentState();
}, [error, filled, focused]);

width: props?.size === 's' ? 43 : 72,
useEffect(() => {
if (firstContentChange) {
setFirstValue(props.value);
setFirstContentChange(false);
return;
}
setFilled(props.value !== firstValue);
if (cleanContent(props.value) !== '') setLastValue(value);
checkContent(props.value);
}, [props.value]);

color: textColor,
fontStyle: 'normal',
fontFamily: 'PublicSans-Regular',
fontSize: props?.size === 's' ? 18 : 32,
lineHeight: props?.size === 's' ? 38 : 48,
backgroundColor: backgroundColor,
paddingVertical: 0,
marginVertical: 0,
},
});
return (
<TextInput
{...props}
style={[style.input, props.style]}
selectTextOnFocus={true}
selectionColor={theme.sw.colors.primary.main + theme.sw.transparency[16]}
keyboardType="number-pad"
textAlign={'center'}
/>
);
};
useEffect(() => {
if (firstContentChange) {
setFirstValue(value);
setFirstContentChange(false);
return;
}
setFilled(value !== firstValue);
if (cleanContent(value) !== '') {
setLastValue(value);
}
checkContent(value);
}, [value]);

const stateStyle = () => {
let borderColor = undefined;
let textColor = undefined;
let backgroundColor = undefined;
switch (currentState) {
case 'prefilled':
textColor = theme.sw.colors.neutral[500];
backgroundColor = theme.sw.colors.neutral[500] + theme.sw.transparency[8];
break;
case 'filled-focused':
textColor = theme.sw.colors.neutral[800];
borderColor = theme.sw.colors.primary.main;
break;
case 'prefilled-focused':
textColor = theme.sw.colors.primary.main;
borderColor = theme.sw.colors.primary.main;
backgroundColor = theme.sw.colors.primary.main + theme.sw.transparency[16];
break;
case 'filled':
textColor = theme.sw.colors.neutral[800];
backgroundColor = theme.sw.colors.neutral[500] + theme.sw.transparency[8];
break;
case 'error':
textColor = theme.sw.colors.error.main;
backgroundColor = theme.sw.colors.error.main + theme.sw.transparency[8];
break;
case undefined:
break;
}

const style = StyleSheet.create({
input: {
borderRadius: size === 's' ? 10 : 18,
height: size === 's' ? 38 : 48,
borderWidth: borderColor ? 1 : 0,
borderColor: borderColor,

width: size === 's' ? 43 : 72,

color: textColor,
fontStyle: 'normal',
fontFamily: 'PublicSans-Regular',
fontSize: size === 's' ? 18 : 32,
lineHeight: size === 's' ? 38 : 48,
backgroundColor: backgroundColor,
paddingVertical: 0,
marginVertical: 0,
},
});
return style.input;
};

const refreshCurrentState = () => {
if (currentState === 'readonly') return;
else if (error) setCurrentState('error');
else if (filled) {
if (focused) setCurrentState('filled-focused');
else setCurrentState('filled');
} else {
if (focused) setCurrentState('prefilled-focused');
else setCurrentState('prefilled');
}
};
let checkContent = (text: string | undefined) => {
if (text !== undefined && text !== '') {
const cleanNumber = text.replace(/[^0-9]/g, '');
const parsedValue = parseInt(cleanNumber);
if (parsedValue !== undefined) {
setError(
(minValue !== undefined && parsedValue < minValue) ||
(maxValue !== undefined && parsedValue > maxValue),
);
}
}
};
let cleanContent = (text: string | undefined) => {
if (text !== undefined && text !== '') {
const cleanNumber = text.replace(/[^0-9]/g, '');
const parsedValue = parseInt(cleanNumber);
return parsedValue.toString();
}
return '';
};
let onChangeText = (e: any) => {
if (props?.onChangeText !== undefined) {
props.onChangeText(e);
checkContent(props.value);
} else {
if (e == '') setValue('');
else if (cleanContent(e) != '') setValue(cleanContent(e));
checkContent(value);
}
};
let onFocus = (e: any) => {
setFocused(true);
if (props?.onFocus !== undefined) props.onFocus(e);
};
let onBlur = (e: any) => {
setFocused(false);
if ((value === '' || props.value === '') && firstValue !== '') onChangeText(firstValue);
else if ((value === '' || props.value === '') && lastValue !== '') {
onChangeText(lastValue);
}
if (props?.onBlur !== undefined) props.onBlur(e);
};

return (
<TextInput
{...props}
value={props.onChangeText ? props.value : value}
ref={ref ?? undefined}
style={[stateStyle(), props.style]}
selectTextOnFocus={false}
onChangeText={(e) => onChangeText(e)}
onBlur={(e) => onBlur(e)}
onFocus={(e) => onFocus(e)}
selectionColor={theme.sw.colors.primary.main + theme.sw.transparency[16]}
cursorColor={theme.sw.colors.primary.main}
keyboardType="number-pad"
editable={state !== 'readonly'}
textAlign={'center'}
/>
);
},
);
33 changes: 17 additions & 16 deletions src/components/numberSelector/NumberSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, { useRef, useState } from 'react';
import { StyleSheet, View, ViewStyle } from 'react-native';
import { IconButton } from '../buttons/IconButton';
import { NumberField } from '../numberField/NumberField';
Expand Down Expand Up @@ -26,32 +26,31 @@ export const NumberSelector = ({
minusIcon = 'minus',
plusIcon = 'add',
showSoftInputOnFocus = false,
variant = 'filled',
variant = 'outlined',
size = 'm',
}: Props) => {
let refInput = useRef<any>();

const [tempValue, setTempValue] = useState<string>(value.toString());

const onAdd = () => {
if (!addDisabled) {
onValueChange(value + 1);
setTempValue((value + 1).toString());
}
if (!addDisabled) onChangeText((value + 1).toString());
};
const onMinus = () => {
if (!minusDisabled) {
onValueChange(value - 1);
setTempValue((value - 1).toString());
}
if (!minusDisabled) onChangeText((value - 1).toString());
};
const onChangeText = (text: string) => {
if (text !== '') {
const parsedValue = parseInt(text);
if (parsedValue && parsedValue >= minValue && parsedValue <= maxValue) {
const cleanNumber = text.replace(/[^0-9]/g, '');
if (tempValue !== '') refInput?.current?.focus();
if (cleanNumber !== '') {
const parsedValue = parseInt(cleanNumber);
if (parsedValue !== undefined && parsedValue >= minValue && parsedValue <= maxValue) {
onValueChange(parsedValue);
setTempValue(text);
setTempValue(parsedValue.toString());
}
} else {
setTempValue('');
onValueChange(0);
setTempValue('');
}
};

Expand Down Expand Up @@ -81,14 +80,16 @@ export const NumberSelector = ({
disabled={minusDisabled}
/>
<NumberField
ref={refInput}
style={styles.inputContainer}
showSoftInputOnFocus={showSoftInputOnFocus}
keyboardType="number-pad"
value={getDisplayedValue()}
minValue={minValue}
maxValue={maxValue}
onChangeText={onChangeText}
selectTextOnFocus={showSoftInputOnFocus}
size={size}
state={'prefilled'}
/>
<IconButton
variant={variant}
Expand Down