-
Notifications
You must be signed in to change notification settings - Fork 0
Components for DateRange filter #103
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| import React from 'react' | ||
| import { StyleProp, StyleSheet, Text, TouchableOpacity, View, ViewStyle } from 'react-native' | ||
|
|
||
| import { Icon } from '@observation.org/react-native-components' | ||
| import { rounded } from '@observation.org/react-native-components/styles' | ||
|
|
||
| import Log from '../lib/Log' | ||
| import { Theme, useStyles, useTheme } from '../theme' | ||
|
|
||
| const height = 48 | ||
|
|
||
| type Props = { | ||
| dateFrom: string | undefined | ||
| dateTo: string | undefined | ||
| emptyDateFromLabel: string | ||
| emptyDateToLabel: string | ||
| onPressFrom: () => void | ||
| onPressTo: () => void | ||
| containerStyle?: StyleProp<ViewStyle> | ||
| } | ||
|
|
||
| const DateFromToFilter = ({ | ||
| dateFrom, | ||
| dateTo, | ||
| emptyDateFromLabel, | ||
| emptyDateToLabel, | ||
| onPressFrom, | ||
| onPressTo, | ||
| }: Props) => { | ||
| Log.debug('DateFromToFilter:render') | ||
|
|
||
| const theme = useTheme() | ||
| const styles = useStyles(createStyles) | ||
|
|
||
| const fromIsActive = dateFrom !== undefined | ||
| const toIsActive = dateTo !== undefined | ||
|
|
||
| return ( | ||
| <View style={styles.dateContainer}> | ||
| <TouchableOpacity | ||
| onPress={onPressFrom} | ||
| style={styles.dateFieldContainer} | ||
| activeOpacity={0.5} | ||
| testID={'date-filter-field-from'} | ||
| > | ||
| <View style={styles.dateInputField}> | ||
| <Icon | ||
| name="calendar-day" | ||
| style="light" | ||
| size={theme.icon.size.m} | ||
| color={fromIsActive ? theme.color.icon.system.brand : theme.color.icon.system.subtle} | ||
| /> | ||
| <Text | ||
| style={[ | ||
| styles.dateInputText, | ||
| { color: fromIsActive ? theme.color.text.system.brand : theme.color.text.system.subtler }, | ||
| ]} | ||
| > | ||
| {dateFrom ? dateFrom : emptyDateFromLabel} | ||
| </Text> | ||
| </View> | ||
| </TouchableOpacity> | ||
|
|
||
| <View style={styles.divider}> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Foutje overgenomen van Observation: deze style had op de |
||
| <Text>–</Text> | ||
| </View> | ||
|
|
||
| <TouchableOpacity | ||
| onPress={onPressTo} | ||
| style={styles.dateFieldContainer} | ||
| activeOpacity={0.5} | ||
| testID={'date-filter-field-to'} | ||
| > | ||
| <View style={styles.dateInputField}> | ||
| <Icon | ||
| name="calendar-day" | ||
| style="light" | ||
| size={theme.icon.size.m} | ||
| color={toIsActive ? theme.color.icon.system.brand : theme.color.icon.system.subtle} | ||
| /> | ||
| <Text | ||
| style={[ | ||
| styles.dateInputText, | ||
| { color: toIsActive ? theme.color.text.system.brand : theme.color.text.system.subtler }, | ||
| ]} | ||
| > | ||
| {dateTo ? dateTo : emptyDateToLabel} | ||
| </Text> | ||
| </View> | ||
| </TouchableOpacity> | ||
| </View> | ||
| ) | ||
| } | ||
|
|
||
| export default DateFromToFilter | ||
|
|
||
| const createStyles = (theme: Theme) => { | ||
| const styles = StyleSheet.create({ | ||
| dateContainer: { | ||
| flexDirection: 'row', | ||
| alignItems: 'center', | ||
| }, | ||
| dateFieldContainer: { | ||
| flex: 0.5, | ||
| }, | ||
| divider: { | ||
| marginHorizontal: theme.margin.half, | ||
| color: theme.color.text.system.subtler, | ||
| }, | ||
| dateInputField: { | ||
| ...rounded.large, | ||
| height: height, | ||
| backgroundColor: theme.color.background.system.surfaceRaised, | ||
| paddingLeft: theme.margin.common, | ||
| flexDirection: 'row', | ||
| alignItems: 'center', | ||
| }, | ||
| dateInputText: { | ||
| ...theme.text.body, | ||
| marginLeft: theme.margin.half, | ||
| }, | ||
| }) | ||
| return styles | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import React from 'react' | ||
| import { Dimensions, StyleSheet, View } from 'react-native' | ||
|
|
||
| import CalendarPicker from 'react-native-calendar-picker' | ||
|
|
||
| import Log from '../lib/Log' | ||
| import { useStyles, useTheme } from '../theme' | ||
|
|
||
| type Props = { | ||
| selectedDate: Date | ||
| onDateChange: (date: Date) => void | ||
| minimumDate?: Date | ||
| maximumDate?: Date | ||
| labelNext: string | ||
| labelPrevious: string | ||
| labelSelectMonth: string | ||
| labelSelectYear: string | ||
| labelsShortWeekdays: string[] | ||
| labelsMonths: string[] | ||
| } | ||
|
|
||
| const DatePicker = ({ | ||
| selectedDate, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Claude moppert wat over Zonder hier serieus in te duiken kan ik niet overzien of dit in de praktijk ook wat betekent. Kun jij checken of hier wat te verbeteren valt? |
||
| onDateChange, | ||
| minimumDate, | ||
| maximumDate, | ||
| labelNext, | ||
| labelPrevious, | ||
| labelSelectMonth, | ||
| labelSelectYear, | ||
| labelsShortWeekdays, | ||
| labelsMonths, | ||
| }: Props) => { | ||
| Log.debug('DatePicker:render') | ||
|
|
||
| const theme = useTheme() | ||
| const styles = useStyles(createStyles) | ||
|
|
||
| return ( | ||
| <View style={styles.container}> | ||
| <CalendarPicker | ||
| // @ts-expect-error The component doesn't allow property testID | ||
| testID="calendar-picker" | ||
| display={'inline'} | ||
| onDateChange={onDateChange} | ||
| minDate={minimumDate} | ||
| maxDate={maximumDate} | ||
| restrictMonthNavigation={true} | ||
| selectedStartDate={selectedDate} | ||
| initialDate={selectedDate} | ||
| textStyle={{ fontFamily: 'Ubuntu' }} | ||
| selectedDayColor={theme.color.background.system.brand} | ||
| selectedDayTextColor={theme.color.text.system.staticWhite} | ||
| nextTitle={labelNext} | ||
| nextTitleStyle={{ color: theme.color.text.system.brand }} | ||
| previousTitle={labelPrevious} | ||
| previousTitleStyle={{ color: theme.color.text.system.brand }} | ||
| selectMonthTitle={labelSelectMonth + ' '} | ||
| selectYearTitle={labelSelectYear} | ||
| monthTitleStyle={{ color: theme.color.text.system.brand }} | ||
| yearTitleStyle={{ color: theme.color.text.system.brand }} | ||
| weekdays={labelsShortWeekdays} | ||
| months={labelsMonths} | ||
| /> | ||
| </View> | ||
| ) | ||
| } | ||
|
|
||
| export default DatePicker | ||
|
|
||
| // The height of the calendar is not constant, but depends on the number of weeks in a month. | ||
| // To prevent the popup to jump up and down, use a fixed height for the calendar, which is always | ||
| // large enough to hold 6 week rows. 90% of the screen width seems to work in all aspect ratio's. | ||
| export const calendarHeight = Dimensions.get('window').width * 0.9 | ||
|
|
||
| const createStyles = () => { | ||
| const styles = StyleSheet.create({ | ||
| container: { | ||
| height: calendarHeight, | ||
| }, | ||
| }) | ||
| return styles | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| import React from 'react' | ||
|
|
||
| import { describe, expect, jest, test } from '@jest/globals' | ||
| import { fireEvent, render } from '@testing-library/react-native' | ||
|
|
||
| import DateFromToFilter from '../DateFromToFilter' | ||
|
|
||
| describe('DateFromToFilter', () => { | ||
| const onPressFrom = jest.fn() | ||
| const onPressTo = jest.fn() | ||
|
|
||
| const dateFromLabel = 'Start date' | ||
| const dateToLabel = 'End date' | ||
|
|
||
| describe('Rendering', () => { | ||
| test('No date selected', () => { | ||
| const { queryByText, toJSON } = render( | ||
| <DateFromToFilter | ||
| dateFrom={undefined} | ||
| dateTo={undefined} | ||
| emptyDateFromLabel={dateFromLabel} | ||
| emptyDateToLabel={dateToLabel} | ||
| onPressFrom={onPressFrom} | ||
| onPressTo={onPressTo} | ||
| />, | ||
| ) | ||
|
|
||
| expect(queryByText('Start date')).toBeTruthy() | ||
| expect(queryByText('End date')).toBeTruthy() | ||
| expect(toJSON()).toMatchSnapshot() | ||
| }) | ||
|
|
||
| test('From date selected', () => { | ||
| const { queryByText, toJSON } = render( | ||
| <DateFromToFilter | ||
| dateFrom="2026-01-31" | ||
| dateTo={undefined} | ||
| emptyDateFromLabel={dateFromLabel} | ||
| emptyDateToLabel={dateToLabel} | ||
| onPressFrom={onPressFrom} | ||
| onPressTo={onPressTo} | ||
| />, | ||
| ) | ||
|
|
||
| expect(queryByText('2026-01-31')).toBeTruthy() | ||
| expect(queryByText('End date')).toBeTruthy() | ||
| expect(toJSON()).toMatchSnapshot() | ||
| }) | ||
|
|
||
| test('To date selected', () => { | ||
| const { queryByText, toJSON } = render( | ||
| <DateFromToFilter | ||
| dateFrom={undefined} | ||
| dateTo="2025-02-15" | ||
| emptyDateFromLabel={dateFromLabel} | ||
| emptyDateToLabel={dateToLabel} | ||
| onPressFrom={onPressFrom} | ||
| onPressTo={onPressTo} | ||
| />, | ||
| ) | ||
|
|
||
| expect(queryByText('Start date')).toBeTruthy() | ||
| expect(queryByText('2025-02-15')).toBeTruthy() | ||
| expect(toJSON()).toMatchSnapshot() | ||
| }) | ||
|
|
||
| test('From and date selected', () => { | ||
| const { queryByText, toJSON } = render( | ||
| <DateFromToFilter | ||
| dateFrom="2026-01-31" | ||
| dateTo="2025-02-15" | ||
| emptyDateFromLabel={dateFromLabel} | ||
| emptyDateToLabel={dateToLabel} | ||
| onPressFrom={onPressFrom} | ||
| onPressTo={onPressTo} | ||
| />, | ||
| ) | ||
|
|
||
| expect(queryByText('2026-01-31')).toBeTruthy() | ||
| expect(queryByText('2025-02-15')).toBeTruthy() | ||
| expect(queryByText('Start date')).toBeFalsy() | ||
| expect(queryByText('End date')).toBeFalsy() | ||
| expect(toJSON()).toMatchSnapshot() | ||
| }) | ||
| }) | ||
|
|
||
| describe('Interaction', () => { | ||
| test('Clicking on the from date will call the callback', async () => { | ||
| // GIVEN | ||
| const { getByTestId } = render( | ||
| <DateFromToFilter | ||
| dateFrom="2026-01-31" | ||
| dateTo="2025-02-15" | ||
| emptyDateFromLabel={dateFromLabel} | ||
| emptyDateToLabel={dateToLabel} | ||
| onPressFrom={onPressFrom} | ||
| onPressTo={onPressTo} | ||
| />, | ||
| ) | ||
|
|
||
| // WHEN | ||
| await fireEvent.press(getByTestId('date-filter-field-from')) | ||
|
|
||
| // THEN | ||
| expect(onPressFrom).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| test('Clicking on the to date will call the callback', async () => { | ||
| // GIVEN | ||
| const { getByTestId } = render( | ||
| <DateFromToFilter | ||
| dateFrom="2026-01-31" | ||
| dateTo="2025-02-15" | ||
| emptyDateFromLabel={dateFromLabel} | ||
| emptyDateToLabel={dateToLabel} | ||
| onPressFrom={onPressFrom} | ||
| onPressTo={onPressTo} | ||
| />, | ||
| ) | ||
|
|
||
| // WHEN | ||
| await fireEvent.press(getByTestId('date-filter-field-to')) | ||
|
|
||
| // THEN | ||
| expect(onPressTo).toHaveBeenCalled() | ||
| }) | ||
| }) | ||
| }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ik zou liever een
Date | undefineddoorgeven maar dan moeten we de formatting hier doen. Het formatten geef ik liever niet uit handen dus daarom heb ik het toch maar zo gelaten.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lijkt me dat schermen, locale-afhankelijke (date)formattering en i18n beter in de apps kan blijven. Wellicht dat we voor het formatteren van dates een
useLocale()/LocaleProviderkunnen toevoegen, analoog aan hoe we theme doen, maar dat levert niet heel veel op. Dus prima zo.