-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IOPLT-171] Introduces
CodeInput
component (#143)
## Short description This PR introduces `CodeInput` component ## How to test Check NumberPad screen to view new `CodeInput` component features https://github.com/pagopa/io-app-design-system/assets/3959405/ae9893d9-1629-4f1f-b0cf-5806dd74a9e3 --------- Co-authored-by: Damiano Plebani <[email protected]>
- Loading branch information
1 parent
1c3f287
commit 0ac642b
Showing
6 changed files
with
194 additions
and
23 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
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
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,121 @@ | ||
import React, { useEffect, useMemo } from "react"; | ||
import { StyleSheet, View } from "react-native"; | ||
import Animated, { | ||
Easing, | ||
useAnimatedStyle, | ||
useSharedValue, | ||
withSequence, | ||
withTiming | ||
} from "react-native-reanimated"; | ||
import { IOColors, IOStyles } from "../../core"; | ||
import { triggerHaptic } from "../../functions"; | ||
|
||
type CodeInputProps = { | ||
value: string; | ||
onValueChange: (value: string) => void; | ||
length: number; | ||
onValidate: (value: string) => boolean; | ||
variant?: "light" | "dark"; | ||
}; | ||
|
||
const DOT_SIZE = 16; | ||
|
||
const styles = StyleSheet.create({ | ||
dotShape: { | ||
width: DOT_SIZE, | ||
height: DOT_SIZE, | ||
borderRadius: 8, | ||
borderWidth: 2 | ||
}, | ||
dotEmpty: { | ||
borderColor: IOColors["grey-200"] | ||
}, | ||
wrapper: { justifyContent: "center", gap: DOT_SIZE } | ||
}); | ||
|
||
const EmptyDot = () => <View style={[styles.dotShape, styles.dotEmpty]} />; | ||
|
||
const FilletDot = ({ color }: { color: IOColors }) => ( | ||
<View | ||
style={[ | ||
styles.dotShape, | ||
{ backgroundColor: IOColors[color], borderColor: IOColors[color] } | ||
]} | ||
/> | ||
); | ||
|
||
export const CodeInput = ({ | ||
length, | ||
value, | ||
onValueChange, | ||
variant = "light", | ||
onValidate | ||
}: CodeInputProps) => { | ||
const [status, setStatus] = React.useState<"default" | "error">("default"); | ||
|
||
const translate = useSharedValue(0); | ||
const shakeOffset: number = 8; | ||
|
||
const animatedStyle = useAnimatedStyle(() => ({ | ||
transform: [{ translateX: translate.value }] | ||
})); | ||
|
||
const fillColor = useMemo( | ||
() => | ||
status === "error" | ||
? "error-600" | ||
: variant === "light" | ||
? "white" | ||
: "black", | ||
[variant, status] | ||
); | ||
|
||
useEffect(() => { | ||
if (onValidate && value.length === length) { | ||
const isValid = onValidate(value); | ||
|
||
if (!isValid) { | ||
setStatus("error"); | ||
triggerHaptic("notificationError"); | ||
|
||
// eslint-disable-next-line functional/immutable-data | ||
translate.value = withSequence( | ||
withTiming(shakeOffset, { | ||
duration: 75, | ||
easing: Easing.inOut(Easing.cubic) | ||
}), | ||
withTiming(-shakeOffset, { | ||
duration: 75, | ||
easing: Easing.inOut(Easing.cubic) | ||
}), | ||
withTiming(shakeOffset / 2, { | ||
duration: 75, | ||
easing: Easing.inOut(Easing.cubic) | ||
}), | ||
withTiming(-shakeOffset / 2, { | ||
duration: 75, | ||
easing: Easing.inOut(Easing.cubic) | ||
}), | ||
withTiming(0, { duration: 75, easing: Easing.inOut(Easing.cubic) }) | ||
); | ||
|
||
const timer = setTimeout(() => { | ||
setStatus("default"); | ||
onValueChange(""); | ||
}, 500); | ||
return () => clearTimeout(timer); | ||
} | ||
} | ||
return; | ||
}, [value, onValidate, length, onValueChange, translate]); | ||
|
||
return ( | ||
<Animated.View style={[IOStyles.row, styles.wrapper, animatedStyle]}> | ||
{[...Array(length)].map((_, i) => ( | ||
<React.Fragment key={i}> | ||
{value[i] ? <FilletDot color={fillColor} /> : <EmptyDot />} | ||
</React.Fragment> | ||
))} | ||
</Animated.View> | ||
); | ||
}; |
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 @@ | ||
export * from "./CodeInput"; |
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
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