Skip to content

Commit cd42a3a

Browse files
authored
ExpandableCalendar - fix calendar position with 'numberOfDays' (#2217)
* ExpandableCalendar - fix calendar position when numberOfDays defined and greater than 1 * close calendar on one day view as well
1 parent 3f1348c commit cd42a3a

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

src/expandableCalendar/__test__/index.spec.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ describe('ExpandableCalendar', () => {
8080
});
8181

8282
describe('Init', () => {
83-
8483
beforeEach(() => {
8584
driver.render();
8685
});
@@ -126,15 +125,38 @@ describe('ExpandableCalendar', () => {
126125
});
127126

128127
it('should not close expandable header on day press when closeOnDayPress is false', () => {
129-
const aDriver = new ExpandableCalendarDriver(testIdExpandableCalendar, TestCase({expandableCalendarProps: {closeOnDayPress: false}}));
130-
aDriver.toggleKnob();
128+
const driver = new ExpandableCalendarDriver(testIdExpandableCalendar, TestCase({expandableCalendarProps: {closeOnDayPress: false}}));
129+
driver.toggleKnob();
131130
jest.runAllTimers();
132-
aDriver.selectDay(dashedToday);
131+
driver.selectDay(dashedToday);
132+
jest.runAllTimers();
133+
expect(driver.isCalendarExpanded()).toBe(true);
134+
});
135+
});
136+
137+
describe('numberOfDays', () => {
138+
beforeEach(() => {
139+
driver.render();
140+
});
141+
142+
it('should be closed when numberOfDays is defined (> 0) ', () => {
143+
const driver = new ExpandableCalendarDriver(testIdExpandableCalendar, TestCase({calendarContextProps: {numberOfDays: 3}, expandableCalendarProps: {initialPosition: Positions.OPEN}}));
133144
jest.runAllTimers();
134-
expect(aDriver.isCalendarExpanded()).toBe(true);
145+
expect(driver.isCalendarExpanded()).toBe(false);
146+
});
147+
148+
it('should hide Knob when numberOfDays > 1', () => {
149+
const driver = new ExpandableCalendarDriver(testIdExpandableCalendar, TestCase({calendarContextProps: {numberOfDays: 3}}));
150+
expect(driver.getKnob()).toBeNull();
151+
});
152+
153+
it('should hide Knob when numberOfDays === 1', () => {
154+
const driver = new ExpandableCalendarDriver(testIdExpandableCalendar, TestCase({calendarContextProps: {numberOfDays: 1}}));
155+
expect(driver.getKnob()).not.toBeNull();
135156
});
136157
});
137158

159+
138160
describe('CalendarList updates', () => {
139161
describe('Day Press', () => {
140162
beforeEach(() => {

src/expandableCalendar/index.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,7 @@ const ExpandableCalendar = (props: ExpandableCalendarProps) => {
163163
const numberOfWeeks = useRef(getNumberOfWeeksInMonth(date));
164164

165165
/** Position */
166-
167-
const [position, setPosition] = useState(initialPosition);
166+
const [position, setPosition] = useState(numberOfDays ? Positions.CLOSED : initialPosition);
168167
const isOpen = position === Positions.OPEN;
169168

170169
const getOpenHeight = () => {
@@ -187,6 +186,12 @@ const ExpandableCalendar = (props: ExpandableCalendarProps) => {
187186

188187
const headerDeltaY = useRef(new Animated.Value(isOpen ? -HEADER_HEIGHT : 0));
189188

189+
useEffect(() => {
190+
if (numberOfDays) {
191+
setPosition(Positions.CLOSED);
192+
}
193+
}, [numberOfDays]);
194+
190195
/** Components' refs */
191196

192197
const wrapper = useRef<any>();

0 commit comments

Comments
 (0)