Skip to content

Commit 10c0222

Browse files
authored
feat(datepicker): update couple of components
* fix(datepicker): remove extra space after DatePicker * feat(date-picker): export datepicker props type * feat(datepicker): add sx prop to datepicker * feat(react-datepicker): react-datepicker v3->v4 * fix(datepicker): fix datepicker props typing * Fix story * Fix package.json - yarn.lock * fix(sticky-summary): fix spacing * fix(formtabs): fix formtabs * Make train spit customizable * Fix datepicker noDataMessage width * fix(datepicker): minor style fix * feat: Make toggleButton stylable * feat(splitgraph): Customized split graph
1 parent f16f0e1 commit 10c0222

File tree

12 files changed

+260
-225
lines changed

12 files changed

+260
-225
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"@types/ramda": "^0.28.1",
5757
"@types/rc-slider": "^8.6.6",
5858
"@types/react": "^16.14.0",
59-
"@types/react-datepicker": "^3.1.8",
59+
"@types/react-datepicker": "^4",
6060
"@types/react-dom": "^16.9.14",
6161
"@types/react-syntax-highlighter": "^15.4.3",
6262
"@types/rebass": "^4.0.10",
@@ -106,7 +106,7 @@
106106
"rc-slider": "^9.7.5",
107107
"rc-tooltip": "^4.2.3",
108108
"react-codemirror2": "^7.2.1",
109-
"react-datepicker": "^3.3.0",
109+
"react-datepicker": "4.8.0",
110110
"react-spring": "^8.0.27",
111111
"react-syntax-highlighter": "^15.4.3",
112112
"rebass": "^4.0.7",
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
11
import React, { useState } from 'react';
22
import { Story, Meta } from '@storybook/react/types-6-0';
3+
import { addDays, subDays } from 'date-fns/fp';
34

45
// Components
56
import DatePicker, { DatePickerProps } from './index';
67

78
import { DATE_TIME } from '../../constants';
9+
import { Flex } from '../flex';
810

911
export default {
1012
title: 'Quartz/DatePicker',
1113
component: DatePicker,
1214
} as Meta;
1315

14-
const Template: Story<DatePickerProps> = (props) => {
16+
const Template: Story<DatePickerProps> = ({ selectProps, ...restProps }) => {
1517
const [startDate, setStartDate] = useState(new Date());
1618

1719
return (
18-
<DatePicker
19-
{...props}
20-
selectProps={{
21-
...props.selectProps,
22-
value: [new Date(startDate).toISOString()],
23-
options: [],
24-
onChange: () => {},
25-
placeholder: 'to',
26-
noDataMessage: 'from',
27-
}}
28-
selected={startDate}
29-
onChange={(date) => setStartDate(date as Date)}
30-
/>
20+
<Flex height="300px">
21+
<DatePicker
22+
{...restProps}
23+
selectProps={{
24+
...selectProps,
25+
value: [new Date(startDate).toISOString()],
26+
noDataMessage: 'from',
27+
}}
28+
selected={startDate}
29+
onChange={(date) => setStartDate(date as Date)}
30+
excludeDateIntervals={[
31+
{ start: subDays(5, new Date()), end: addDays(5, new Date()) },
32+
]}
33+
/>
34+
</Flex>
3135
);
3236
};
3337

@@ -38,25 +42,3 @@ Default.args = {
3842
dateFormat: DATE_TIME.DATE_TIME_FULL_MONTH,
3943
excludeTimes: [new Date()],
4044
};
41-
42-
Default.argTypes = {
43-
selectProps: {
44-
type: {
45-
required: true,
46-
summary: 'Select props',
47-
},
48-
},
49-
datePickerAlign: {
50-
control: {
51-
type: 'select',
52-
default: 'right',
53-
options: ['left', 'right'],
54-
},
55-
type: {
56-
required: false,
57-
},
58-
defaultValue: {
59-
summary: 'datepicker align',
60-
},
61-
},
62-
};

src/components/datepicker/index.tsx

+16-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Box, Flex } from 'rebass';
1+
import { Box, Flex, SxStyleProp } from 'rebass';
22
import React, { FC, memo, useState } from 'react';
33
import ReactDatePicker, { ReactDatePickerProps } from 'react-datepicker';
44

@@ -10,22 +10,25 @@ import Labeling from '../typography/labeling';
1010
import Value from '../typography/value';
1111
import { GetIcon, IconName } from '../icon';
1212

13-
export interface DatePickerProps {
14-
selectProps: SelectProps;
13+
export interface DatePickerProps extends ReactDatePickerProps {
14+
selectProps: Pick<SelectProps, 'variant' | 'noDataMessage' | 'value'>;
1515
datePickerAlign?: 'left' | 'right';
16+
sx?: SxStyleProp;
1617
}
1718

18-
const DatePicker: FC<DatePickerProps & ReactDatePickerProps> = ({
19+
const DatePicker: FC<DatePickerProps> = ({
1920
selectProps,
2021
datePickerAlign = 'right',
22+
sx,
2123
...props
2224
}) => {
2325
const [open, setOpen] = useState(false);
2426

2527
return (
26-
<Flex flexDirection="column" sx={styles(datePickerAlign)}>
28+
<Flex flexDirection="column" sx={{ ...styles(datePickerAlign), ...sx }}>
2729
<Flex
2830
alignItems="center"
31+
flexShrink={0}
2932
onClick={() => setOpen(!open)}
3033
sx={{
3134
height: '32px',
@@ -37,18 +40,23 @@ const DatePicker: FC<DatePickerProps & ReactDatePickerProps> = ({
3740
variant={selectProps.variant ?? 'white'}
3841
tx="variants.select"
3942
>
40-
<Labeling gray mr="5px">
43+
<Labeling gray mr="5px" minWidth="unset">
4144
{selectProps.noDataMessage}
4245
</Labeling>
4346
<Value
4447
mr="5px"
4548
sx={{ fontSize: 'text', fontWeight: 'text', fontFamily: 'text' }}
49+
flexGrow={1}
50+
flexShrink={0}
4651
>
4752
{selectProps.value}
4853
</Value>
49-
<GetIcon icon={IconName.arrow_up_down} size="sm" />
54+
<GetIcon flexShrink={0} icon={IconName.arrow_up_down} size="sm" />
5055
</Flex>
51-
<Box alignSelf={datePickerAlign === 'right' ? 'flex-end' : 'flex-start'}>
56+
<Box
57+
height="0px"
58+
alignSelf={datePickerAlign === 'right' ? 'flex-end' : 'flex-start'}
59+
>
5260
<ReactDatePicker
5361
open={open}
5462
{...props}

src/components/form-tabs/index.tsx

+16-20
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import React, {
88
} from 'react';
99
import { Box, Flex } from 'rebass';
1010
import ResizeObserver from 'resize-observer-polyfill';
11+
import * as R from 'ramda';
12+
1113
import {
1214
summaryContainerStyles,
1315
containerStyles,
@@ -51,7 +53,7 @@ const FormTabs: FC<FormTabsProps> = ({
5153
hasScrollOnScreen = true,
5254
...props
5355
}: FormTabsProps) => {
54-
const initialActive = useMemo(() => {
56+
const active = useMemo(() => {
5557
const tabIdx = tabs.findIndex((t) => t.id === initialTab);
5658
return Math.max(tabIdx, 0);
5759
}, [initialTab, tabs]);
@@ -61,54 +63,47 @@ const FormTabs: FC<FormTabsProps> = ({
6163
const hasOverflow = useCallback(() => {
6264
const el = tabsContainerRef?.current;
6365
return el ? el.clientWidth !== el.scrollWidth : false;
64-
}, [tabsContainerRef?.current]);
66+
}, []);
6567

66-
const [active, setActive] = useState<number>(initialActive);
6768
const [tabArray, setTabArray] = useState<TabItem[]>(tabs);
6869
const [isOverflown, setIsOverflown] = useState<boolean>(false);
6970

7071
const currentTab = useMemo(() => tabArray[active], [tabArray, active]);
7172

7273
const handleGoForward = useCallback(async () => {
7374
const validation = await currentTab.validationFn();
74-
const copy = [...tabArray.map((x) => x)];
7575

7676
// VALID: move to next tab and show check mark.
7777
if (validation === ValidateOpts.valid) {
78-
copy[active] = { ...copy[active], state: TabState.valid };
79-
onTabChange(copy[active + 1].id);
80-
setActive((act) => act + 1);
78+
setTabArray(R.adjust(active, R.assoc('state', TabState.valid)));
79+
onTabChange(tabArray[active + 1].id);
8180
}
8281

8382
// UNTOUCHED: if optional, move to next tab. Otherwise show error.
8483
if (validation === ValidateOpts.untouched) {
8584
if (currentTab.optional) {
86-
copy[active] = { ...copy[active], state: TabState.optional };
87-
onTabChange(copy[active + 1].id);
88-
setActive((act) => act + 1);
85+
setTabArray(R.adjust(active, R.assoc('state', TabState.optional)));
86+
onTabChange(tabArray[active + 1].id);
8987
} else {
90-
copy[active] = { ...copy[active], state: TabState.error };
88+
setTabArray(R.adjust(active, R.assoc('state', TabState.error)));
9189
}
9290
}
9391

9492
// INVALID: don't move to next tab and show error.
9593
if (validation === ValidateOpts.error) {
96-
copy[active] = { ...copy[active], state: TabState.error };
94+
setTabArray(R.adjust(active, R.assoc('state', TabState.error)));
9795
}
98-
setTabArray(copy);
99-
}, [setActive, active, setTabArray, tabArray, onTabChange, currentTab]);
96+
}, [active, setTabArray, tabArray, onTabChange, currentTab]);
10097

10198
const handleGoBack = useCallback(() => {
10299
onTabChange(tabArray[active - 1].id);
103-
setActive((act) => act - 1);
104-
}, [active, onTabChange, setActive]);
100+
}, [active, onTabChange, tabArray]);
105101

106102
const handleTabClick = useCallback(
107103
(idx: number) => {
108-
setActive(idx);
109104
onTabChange(tabArray[idx].id);
110105
},
111-
[setActive, onTabChange],
106+
[onTabChange, tabArray],
112107
);
113108

114109
const handleSubmit = useCallback(async () => {
@@ -141,7 +136,7 @@ const FormTabs: FC<FormTabsProps> = ({
141136
);
142137
setTabArray(updated);
143138
return allValid;
144-
}, []);
139+
}, [tabArray]);
145140

146141
useEffect(() => {
147142
if (!tabsContainerRef.current) return;
@@ -150,9 +145,10 @@ const FormTabs: FC<FormTabsProps> = ({
150145
setIsOverflown(hasOverflow());
151146
});
152147
resizeObserver.observe(tabsContainerRef.current);
148+
153149
// eslint-disable-next-line consistent-return
154150
return () => resizeObserver.disconnect();
155-
}, [tabsContainerRef?.current]);
151+
}, [hasOverflow]);
156152

157153
return (
158154
<FormSummaryContainer

0 commit comments

Comments
 (0)