Skip to content

Commit 1e0895a

Browse files
authored
Merge pull request #11 from BeeInventor/develop
feat: custom date format
2 parents ac5b25b + 8fd7fe3 commit 1e0895a

File tree

5 files changed

+47
-21
lines changed

5 files changed

+47
-21
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@beeinventor/dasiot-react-component-lib",
3-
"version": "1.0.1",
3+
"version": "1.1.0",
44
"module": "lib/index.js",
55
"types": "lib/index.d.ts",
66
"files": [

src/components/DatePicker/DatePicker.stories.tsx

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import React, { useState } from 'react';
2-
import { Meta, Story } from '@storybook/react';
3-
import DatePicker from '.';
4-
import { DatePickerProps } from './DatePicker.types';
52
import localZhTw from 'date-fns/locale/zh-TW';
3+
import { styled } from '@mui/material/styles';
4+
import { Meta, Story } from '@storybook/react';
65
import { Dialog, DialogContent } from '@mui/material';
6+
7+
import DatePicker from './DatePicker';
8+
import { DatePickerProps } from './DatePicker.types';
79
import Button from '../Button';
810

911
export default {
@@ -142,3 +144,16 @@ export const ExternalReset: Story<DatePickerProps> = (args) => {
142144
ExternalReset.args = {
143145
...Default.args,
144146
};
147+
148+
const StyledDatePicker = styled(DatePicker)(() => ({
149+
width: '100%',
150+
}));
151+
152+
export const FullWidth: Story<DatePickerProps> = (args) => {
153+
return <StyledDatePicker {...args} />;
154+
};
155+
156+
FullWidth.args = {
157+
dateFormat: 'yyyy-MM-dd',
158+
startDate: new Date('2021-09-15T00:00:00+08:00'),
159+
};

src/components/DatePicker/DatePicker.tsx

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ const Root = styled(Box)<StyledRootProps>(({ theme, disabled }) => ({
3838
...(disabled
3939
? { pointerEvents: 'none', backgroundColor: theme.color.secondary.$40 }
4040
: {}),
41+
'& > .DatePicker-operation': {
42+
display: 'flex',
43+
justifyContent: 'flex-end',
44+
},
4145
}));
4246

4347
const Placeholder = styled('span')(({ theme }) => ({
@@ -133,6 +137,7 @@ const Item = styled(Box)(({}) => ({
133137

134138
const DatePicker: VFC<DatePickerProps> = (props) => {
135139
const {
140+
className,
136141
type = 'date',
137142
locale,
138143
startDate,
@@ -143,6 +148,7 @@ const DatePicker: VFC<DatePickerProps> = (props) => {
143148
limitTo,
144149
popperProps,
145150
disabled,
151+
dateFormat,
146152
...otherProps
147153
} = props;
148154
const containerRef = useRef(null);
@@ -310,13 +316,14 @@ const DatePicker: VFC<DatePickerProps> = (props) => {
310316
return (
311317
<>
312318
<Root
319+
className={`DatePicker${className ? ' ' + className : ''}`}
313320
ref={containerRef}
314321
disabled={disabled}
315322
onClick={() => setIsOpen(true)}
316323
{...otherProps}
317324
>
318325
{localStartDate ? (
319-
format(localStartDate, 'MM/dd', {
326+
format(localStartDate, dateFormat ?? 'MM/dd', {
320327
locale,
321328
})
322329
) : (
@@ -326,28 +333,30 @@ const DatePicker: VFC<DatePickerProps> = (props) => {
326333
<>
327334
{' - '}
328335
{localEndDate ? (
329-
format(localEndDate, 'MM/dd', {
336+
format(localEndDate, dateFormat ?? 'MM/dd', {
330337
locale,
331338
})
332339
) : (
333340
<Placeholder>{placeholder}</Placeholder>
334341
)}
335342
</>
336343
)}
337-
{(localStartDate || localEndDate) && (
338-
<Icon onClick={handleClearDates}>
339-
<img src={cancelCircleSVG} />
344+
<div className="DatePicker-operation">
345+
{(localStartDate || localEndDate) && (
346+
<Icon onClick={handleClearDates}>
347+
<img src={cancelCircleSVG} />
348+
</Icon>
349+
)}
350+
<Icon>
351+
<BtnIcCalendar
352+
sx={{
353+
'& path': {
354+
stroke: disabled ? theme.color.secondary.$60 : '#606060',
355+
},
356+
}}
357+
/>
340358
</Icon>
341-
)}
342-
<Icon>
343-
<BtnIcCalendar
344-
sx={{
345-
'& path': {
346-
stroke: disabled ? theme.color.secondary.$60 : '#606060',
347-
},
348-
}}
349-
/>
350-
</Icon>
359+
</div>
351360
</Root>
352361
<Popper
353362
open={disabled ? false : isOpen}

src/components/DatePicker/DatePicker.types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export interface StyledRootProps {
66
}
77

88
export interface DatePickerProps extends Omit<BoxProps, 'onSelect'> {
9+
className: string;
910
type?: 'date' | 'range';
1011
placeholder?: string;
1112
locale?: Locale;
@@ -14,6 +15,7 @@ export interface DatePickerProps extends Omit<BoxProps, 'onSelect'> {
1415
limitFrom?: Date;
1516
limitTo?: Date;
1617
disabled?: boolean;
18+
dateFormat?: string;
1719
onSelect: (
1820
value: Date | undefined | [Date | undefined, Date | undefined],
1921
) => void;

0 commit comments

Comments
 (0)