Skip to content

Commit

Permalink
Merge pull request #130 from ZeroGachis/task/add-a-placeholder-for-un…
Browse files Browse the repository at this point in the history
…defined-value

Task/add a placeholder for undefined value
  • Loading branch information
ulricden authored Nov 28, 2023
2 parents caeb5f5 + 25ff428 commit 9162314
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,12 @@ export const Default: Story = {
return NumberSelectorTester(args);
},
};

export const undefinedValue: Story = {
render: (args) => {
args.value = undefined;
return NumberSelectorTester(args);
},
};

Default.parameters = { noSafeArea: false };
1 change: 0 additions & 1 deletion src/components/numberField/NumberField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ export const NumberField = React.forwardRef<TextInput, NumberFieldProps>(
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)}
Expand Down
24 changes: 13 additions & 11 deletions src/components/numberSelector/NumberSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { NumberField } from '../numberField/NumberField';
import type { IconName } from '../icons/IconProps';

export interface Props {
value: number;
onValueChange: (value: number) => void;
value: number | undefined;
onValueChange?: (value: number) => void;
onEndEditing?: ((value: string | undefined) => void) | undefined
minValue: number;
maxValue: number;
Expand All @@ -18,11 +18,12 @@ export interface Props {
variant?: 'filled' | 'outlined';
size?: 'm' | 's';
decimal?: boolean;
placeholder?: string;
}

export const NumberSelector = ({
value,
onValueChange,
onValueChange= undefined,
onEndEditing = undefined,
minValue = 0,
maxValue,
Expand All @@ -33,10 +34,11 @@ export const NumberSelector = ({
variant = 'outlined',
decimal = false,
size = 'm',
placeholder = '--',
}: Props) => {
const refInput = useRef<any>();
const parser = decimal ? parseFloat : parseInt;
const [tempValue, setTempValue] = useState<string>(value.toString());
const [tempValue, setTempValue] = useState<string>(value?.toString() ?? placeholder);
const [softInputOnFocus, setSoftInputOnFocus] = useState(false);
const allowedMinus = (): boolean => {
return minValue !== undefined && minValue < 0;
Expand All @@ -47,23 +49,23 @@ export const NumberSelector = ({
const onAdd = () => {
Keyboard.dismiss();
if (!addDisabled) {
const newValue = (
const newValue =value? (
Math.round(
(value + 1 < maxValue ? value + 1 : maxValue) * 10
) / 10
).toString();
).toString() : "1";
onChangeText(newValue);
onEndEditing && onEndEditing(newValue);
}
};
const onMinus = () => {
Keyboard.dismiss();
if (!minusDisabled){
const newValue = (
const newValue = value ? (
Math.round(
(value - 1 > minValue ? value - 1 : minValue) * 10
) / 10
).toString();
).toString() : "0";
onChangeText(newValue);
onEndEditing && onEndEditing(newValue);
}
Expand All @@ -79,14 +81,14 @@ export const NumberSelector = ({
(minValue === undefined || parsedValue >= minValue) &&
(maxValue === undefined || parsedValue <= maxValue)
) {
onValueChange(parsedValue);
onValueChange && onValueChange(parsedValue);
setTempValue(text);
}
}
};

const minusDisabled = minValue >= value;
const addDisabled = maxValue <= value;
const minusDisabled = minValue >= (value ?? 0) ;
const addDisabled = maxValue <= (value ?? 0);

const styles = StyleSheet.create({
container: {
Expand Down

0 comments on commit 9162314

Please sign in to comment.