-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from solved-ac/release/v0.6.0
Release
- Loading branch information
Showing
13 changed files
with
788 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { Button, Centering, Dropdown } from '@solved-ac/ui-react' | ||
import { Meta, StoryFn } from '@storybook/react' | ||
import React from 'react' | ||
|
||
import TooltipStories from './Tooltip.stories' | ||
|
||
export default { | ||
title: 'Components/Dropdown', | ||
component: Dropdown, | ||
argTypes: { | ||
...TooltipStories.argTypes, | ||
interactive: { | ||
...(TooltipStories.argTypes?.interactive || {}), | ||
defaultValue: true, | ||
}, | ||
activateOnHover: { | ||
...(TooltipStories.argTypes?.activateOnHover || {}), | ||
defaultValue: false, | ||
}, | ||
activateOnClick: { | ||
...(TooltipStories.argTypes?.activateOnClick || {}), | ||
defaultValue: true, | ||
}, | ||
noThemeChange: { | ||
...(TooltipStories.argTypes?.noThemeChange || {}), | ||
defaultValue: true, | ||
}, | ||
}, | ||
} as Meta<typeof Dropdown> | ||
|
||
const Template: StoryFn<typeof Dropdown> = (args) => ( | ||
<Centering | ||
style={{ | ||
height: 200, | ||
}} | ||
> | ||
<Dropdown {...args} /> | ||
</Centering> | ||
) | ||
|
||
export const Default = Template.bind({}) | ||
Default.args = { | ||
children: <Button>Click me!</Button>, | ||
title: 'Dropdown', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import styled from '@emotion/styled' | ||
import React, { ElementType, useEffect, useState } from 'react' | ||
import { PP, PR } from '../../types/PolymorphicElementProps' | ||
import { forwardRefWithGenerics } from '../../utils/ref' | ||
import { DateSelectContext } from './DateSelectContext' | ||
import { DateSelectMonthView } from './DateSelectMonthView' | ||
|
||
export interface DateRange { | ||
start: string | ||
end: string | ||
} | ||
|
||
export type DateSelectValues = | ||
| { | ||
type: 'date' | ||
value: string | ||
onChange?: (value: string) => void | ||
} | ||
| { | ||
type: 'date-range' | ||
value: DateRange | ||
onChange?: (value: DateRange) => void | ||
} | ||
|
||
export interface DateSelectAnnotation extends DateRange { | ||
title?: string | ||
color?: string | ||
} | ||
|
||
export type DateSelectProps = DateSelectValues & { | ||
annotations?: DateSelectAnnotation[] | ||
maxAnnotationsPerDay?: number | ||
weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | ||
locale?: string | ||
chunks?: number | ||
} | ||
|
||
export type DateSelectMode = 'year' | 'month' | 'date' | ||
export type CursorMode = 'select' | 'selectStart' | 'selectEnd' | ||
|
||
export type DateSelectCursor = | ||
| { | ||
mode: 'select' | ||
hover: Date | null | ||
} | ||
| { | ||
mode: 'selectStart' | ||
hover: Date | null | ||
} | ||
| { | ||
mode: 'selectEnd' | ||
valueStart: Date | ||
hover: Date | null | ||
} | ||
|
||
// const toDateString = (date: Date): string => { | ||
// return date.toISOString().split('T')[0] | ||
// } | ||
|
||
// const fromDateString = (date: string): Date => { | ||
// return new Date(date) | ||
// } | ||
|
||
const DateSelectContainer = styled.div` | ||
user-select: none; | ||
display: flex; | ||
gap: 1em; | ||
` | ||
|
||
export const DateSelect = forwardRefWithGenerics( | ||
<T extends ElementType>(props: PP<T, DateSelectProps>, ref?: PR<T>) => { | ||
const { | ||
type, | ||
value, | ||
onChange, | ||
annotations = [], | ||
maxAnnotationsPerDay = annotations.length ? 3 : 0, | ||
weekStartsOn = 0, | ||
locale, | ||
chunks = 1, | ||
...rest | ||
} = props | ||
// const theme = useTheme() | ||
|
||
const [currentMode, setCurrentMode] = useState<DateSelectMode>('date') | ||
const [selectState, setSelectState] = useState<DateSelectCursor>({ | ||
mode: type === 'date' ? ('select' as const) : ('selectStart' as const), | ||
hover: null, | ||
}) | ||
const [cursorDate, setCursorDate] = useState<Date>(new Date()) | ||
|
||
useEffect(() => { | ||
setSelectState({ | ||
mode: type === 'date' ? ('select' as const) : ('selectStart' as const), | ||
hover: null, | ||
}) | ||
}, [type]) | ||
|
||
// const selectedYear = selectedDate.getFullYear() | ||
// const selectedMonth = selectedDate.getMonth() | ||
|
||
return ( | ||
<DateSelectContext.Provider value={props}> | ||
<DateSelectContainer {...rest} ref={ref}> | ||
{currentMode === 'date' && | ||
new Array(chunks).fill(0).map((_, index) => ( | ||
<DateSelectMonthView | ||
// eslint-disable-next-line react/no-array-index-key | ||
key={index} | ||
offset={index - Math.floor(chunks / 2)} | ||
cursorDate={cursorDate} | ||
setCursorDate={setCursorDate} | ||
selectState={selectState} | ||
setSelectState={setSelectState} | ||
setModeToMonth={() => setCurrentMode('month')} | ||
firstMonth={index === 0} | ||
lastMonth={index === chunks - 1} | ||
/> | ||
))} | ||
</DateSelectContainer> | ||
</DateSelectContext.Provider> | ||
) | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import React, { useContext } from 'react' | ||
import { DateSelectProps } from './DateSelect' | ||
|
||
export const DateSelectContext = React.createContext<DateSelectProps>({ | ||
type: 'date', | ||
value: '2020-06-05', | ||
onChange: () => { | ||
/* no-op */ | ||
}, | ||
}) | ||
|
||
export const useDateSelectContext = (): DateSelectProps => | ||
useContext(DateSelectContext) |
Oops, something went wrong.