Skip to content

Instantly share code, notes, and snippets.

@sturmenta
Created June 12, 2023 03:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sturmenta/45414d93d3e56c6ec18e82451296b64f to your computer and use it in GitHub Desktop.
Save sturmenta/45414d93d3e56c6ec18e82451296b64f to your computer and use it in GitHub Desktop.
react native WeekdaysSelector
import { TouchableOpacity, View } from 'react-native';
import { withLightHapticFeedback } from '_utils';
import { C_Text } from './C_Text';
const weekdays = ['L', 'M', 'X', 'J', 'V', 'S', 'D'];
export const WeekdaysSelector = ({
selectedDays,
onChangeValue,
fieldWithError,
}: {
selectedDays: string;
onChangeValue: (text: string) => void;
fieldWithError?: boolean;
}) => {
// L-M-X -> ['L', 'M', 'X']
const parsedSelectedDays = selectedDays.replaceAll('-', '').split('');
const toggleWeekdaySelection = (weekdayLabel) => {
let newSelectedDays;
if (parsedSelectedDays.includes(weekdayLabel)) {
newSelectedDays = parsedSelectedDays.filter((day) => day !== weekdayLabel);
} else {
newSelectedDays = [...parsedSelectedDays, weekdayLabel];
}
onChangeValue(newSelectedDays.join('-'));
};
return (
<View
className={`flex items-center justify-center space-x-2 flex-row py-2 ${
fieldWithError ? 'border border-color-error' : ''
}`}>
{weekdays.map((weekDay) => (
<TouchableOpacity
key={weekDay}
onPress={() => withLightHapticFeedback(() => toggleWeekdaySelection(weekDay))}>
<View
className={`flex items-center justify-center w-8 h-8 rounded-full cursor-pointer ${
parsedSelectedDays.includes(weekDay) ? 'bg-color-primary' : 'bg-gray-800'
}`}>
<C_Text
className={`${!parsedSelectedDays.includes(weekDay) ? 'text-color-text2' : ''}`}>
{weekDay}
</C_Text>
</View>
</TouchableOpacity>
))}
</View>
);
};
export default WeekdaysSelector;
@sturmenta
Copy link
Author

a.MP4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment