Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Commit

Permalink
Merge pull request #77 from navikt/feat/allow-navigating-to-disabled-…
Browse files Browse the repository at this point in the history
…months

Add prop for allowing navigation outside limitation range
  • Loading branch information
frodehansen2 committed Jun 16, 2021
2 parents 5503073 + 1827a3b commit a6e2fca
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,4 +1,8 @@
# Changelog

## [11.2.0] - 2021-06-11
Add ability to navigate outside restricted range via prop

## [9.0.0] - 2021-01-04
Refactor locale code. Set 'nb' as default locale (was 'en').

Expand Down
5 changes: 3 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "nav-datovelger",
"version": "11.1.2",
"version": "11.2.0",
"description": "Datepicker with input and calendar",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down Expand Up @@ -92,13 +92,13 @@
},
"peerDependencies": {
"classnames": "^2.2.x",
"dayjs": "^1.10.x",
"nav-frontend-core": "^5.0.x",
"nav-frontend-js-utils": "^1.0.x",
"nav-frontend-skjema": "^3.1.x",
"nav-frontend-typografi": "^3.0.x",
"react": "17",
"react-day-picker": "^7.4.x",
"dayjs": "^1.10.x"
"react-day-picker": "^7.4.x"
},
"eslintConfig": {
"extends": "react-app"
Expand Down
3 changes: 3 additions & 0 deletions src/datepicker/Datepicker.tsx
Expand Up @@ -32,6 +32,7 @@ export interface DatepickerProps {
showYearSelector?: boolean;
dayPickerProps?: DayPickerProps;
setFocusOnDateWhenOpened?: boolean;
allowNavigationToDisabledMonths?: boolean;
}

const Datepicker = ({
Expand All @@ -47,6 +48,7 @@ const Datepicker = ({
onChange,
dayPickerProps,
setFocusOnDateWhenOpened,
allowNavigationToDisabledMonths = false,
}: DatepickerProps) => {
const [activeMonth, setActiveMonth] = useState<Date>(getDefaultMonth(value, limitations, dayPickerProps));
const [calendarIsVisible, setCalendarIsVisible] = useState<boolean>(false);
Expand Down Expand Up @@ -109,6 +111,7 @@ const Datepicker = ({
dayPickerProps={dayPickerProps}
showYearSelector={showYearSelector}
setFocusOnDateWhenOpened={setFocusOnDateWhenOpened}
allowNavigationToDisabledMonths={allowNavigationToDisabledMonths}
/>
</CalendarPortal>
)}
Expand Down
3 changes: 3 additions & 0 deletions src/datepicker/calendar/Calendar.tsx
Expand Up @@ -31,6 +31,7 @@ interface Props {
locale: DatepickerLocales;
dayPickerProps?: DayPickerProps;
setFocusOnDateWhenOpened?: boolean;
allowNavigationToDisabledMonths: boolean;
}

export type NavigationFocusElement = 'nextMonth' | 'previousMonth' | 'year' | 'month';
Expand All @@ -52,6 +53,7 @@ const Calendar = React.forwardRef(function Calendar(props: Props, ref: React.Ref
onSelect,
setFocusOnDateWhenOpened,
dayPickerProps,
allowNavigationToDisabledMonths,
} = props;

const onSelectDate = (date: Date, modifiers: DayModifiers) => {
Expand Down Expand Up @@ -94,6 +96,7 @@ const Calendar = React.forwardRef(function Calendar(props: Props, ref: React.Ref
...localeUtils,
}}
showYearSelector={showYearSelector}
allowNavigationToDisabledMonths={allowNavigationToDisabledMonths}
/>
);
},
Expand Down
17 changes: 14 additions & 3 deletions src/datepicker/calendar/Navbar.tsx
Expand Up @@ -17,6 +17,7 @@ interface Props {
maxDate?: Date;
showYearSelector?: boolean;
localeUtils: LocaleUtils;
allowNavigationToDisabledMonths: boolean;
}

interface NavbarButtonProps {
Expand Down Expand Up @@ -55,13 +56,23 @@ class Navbar extends React.Component<Props> {
}

render() {
const { defaultMonth, onChangeMonth, minDate, maxDate, showYearSelector, localeUtils } = this.props;
const {
defaultMonth,
onChangeMonth,
minDate,
maxDate,
showYearSelector,
localeUtils,
allowNavigationToDisabledMonths,
} = this.props;

const previousMonth = dayjs(defaultMonth).subtract(1, 'month');
const nextMonth = dayjs(defaultMonth).add(1, 'month');

const lastMonthIsDisabled = minDate ? dayjs(minDate).isAfter(previousMonth.endOf('month')) : false;
const nextMonthIsDisabled = maxDate ? dayjs(maxDate).isBefore(nextMonth.startOf('month')) : false;
const lastMonthIsDisabled =
!allowNavigationToDisabledMonths && minDate ? dayjs(minDate).isAfter(previousMonth.endOf('month')) : false;
const nextMonthIsDisabled =
!allowNavigationToDisabledMonths && maxDate ? dayjs(maxDate).isBefore(nextMonth.startOf('month')) : false;

const onClick = (
evt: React.MouseEvent<HTMLButtonElement>,
Expand Down
10 changes: 9 additions & 1 deletion src/datepicker/calendar/__tests__/Calendar.test.tsx
Expand Up @@ -5,7 +5,15 @@ import Calendar from '../Calendar';
describe('Calendar', () => {
it('Should be defined', () => {
expect(
shallow(<Calendar onClose={jest.fn()} onSelect={jest.fn()} locale="nb" month={new Date()} />)
shallow(
<Calendar
onClose={jest.fn()}
onSelect={jest.fn()}
locale="nb"
month={new Date()}
allowNavigationToDisabledMonths={false}
/>
)
).toBeDefined();
});
});
36 changes: 34 additions & 2 deletions src/dev/examples/DatepickerExample.tsx
Expand Up @@ -29,6 +29,10 @@ const DatepickerExample: React.FunctionComponent = () => {
const [initialMonth, setInitialMonth] = useState<Date | undefined>();
const [locale, setLocale] = useState<DatepickerLocales>('nb');

const [minDate, setMinDate] = useState<DatepickerValue>('2000-04-03');
const [maxDate, setMaxDate] = useState<DatepickerValue>('2025-12-12');
const [allowNavigatingToDisabledMonths, setAllowNavigatingToDisabledMonths] = useState(false);

const takenRange: DatepickerDateRange = {
from: '2021-04-10',
to: '2021-04-11',
Expand All @@ -55,8 +59,8 @@ const DatepickerExample: React.FunctionComponent = () => {
limitations={{
weekendsNotSelectable: false,
invalidDateRanges: [takenRange],
minDate: '2000-04-03',
maxDate: '2025-12-12',
minDate: minDate.length > 0 ? minDate : undefined,
maxDate: maxDate.length > 0 ? maxDate : undefined,
}}
dayPickerProps={{
initialMonth,
Expand All @@ -65,6 +69,7 @@ const DatepickerExample: React.FunctionComponent = () => {
console.log(month);
},
}}
allowNavigationToDisabledMonths={allowNavigatingToDisabledMonths}
/>
<Box margin="l">Chosen date: {renderDate(date)}</Box>
<Box margin="m">
Expand Down Expand Up @@ -113,6 +118,19 @@ const DatepickerExample: React.FunctionComponent = () => {
</Knapp>
</Box>

<Box margin="xl">Restrictions</Box>
<Box margin="m">
<div style={{ display: 'inline-block' }}>
<label htmlFor={'date-range-from'}>First pickable date</label>
<Datepicker inputId={'date-range-from'} onChange={(e) => setMinDate(e)} value={minDate} />
</div>
{' - '}
<div style={{ display: 'inline-block' }}>
<label htmlFor={'date-range-to'}>Last pickable date</label>
<Datepicker inputId={'date-range-to'} onChange={(e) => setMaxDate(e)} value={maxDate} />
</div>
</Box>

<Box margin="xl">
<fieldset>
<legend>Presentation properties</legend>
Expand Down Expand Up @@ -159,6 +177,20 @@ const DatepickerExample: React.FunctionComponent = () => {
}
/>
</Box>
<Box margin={'l'}>
<Checkbox
label={
<>
<div>
<code>allowNavigationToDisabledMonths</code>
</div>
Allow navigating outside restricted range
</>
}
checked={allowNavigatingToDisabledMonths}
onChange={(event) => setAllowNavigatingToDisabledMonths(event.target.checked)}
/>
</Box>
</div>
</fieldset>
</Box>
Expand Down

0 comments on commit a6e2fca

Please sign in to comment.