-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update timepicker styles (#741)
* WIP: re design TimePicker component affects: @medly-components/core, @medly-components/forms, @medly-components/theme * test: add unit test cases for timepicker affects: @medly-components/core, @medly-components/forms * refactor: timepicker styles affects: @medly-components/core
- Loading branch information
Showing
20 changed files
with
869 additions
and
638 deletions.
There are no files selected for viewing
17 changes: 12 additions & 5 deletions
17
packages/core/src/components/TextField/getMaskedValue.test.ts
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 |
---|---|---|
@@ -1,18 +1,25 @@ | ||
import getMaskedValue from './getMaskedValue'; | ||
|
||
describe('getMaskedValue function', () => { | ||
// @ts-ignore | ||
const maskedValue = (value: string, selectionStart?: number) => getMaskedValue({ target: { value, selectionStart } }, 'DD / MM / YYYY'); | ||
|
||
// @ts-expect-error | ||
const maskedValue = (value: string, selectionStart?: number, data?: string | null = null) => | ||
// @ts-expect-error | ||
getMaskedValue({ target: { value, selectionStart }, nativeEvent: { data } }, 'DD / MM / YYYY'); | ||
|
||
it('should return truncated value if selectionStart value is less then value length', () => { | ||
expect(maskedValue('11 / 11 / 1111', 6)).toEqual({ maskedValue: '11 / 1 1 / 1111', selectionStart: 6 }); | ||
it('should add blank space on deleting any non special character and move cursor after the blank space', () => { | ||
expect(maskedValue('11 / 1 / 1111', 5, null)).toEqual({ maskedValue: '11 / 1 / 1111', selectionStart: 6 }); | ||
}); | ||
|
||
it('should not move cursor on deleting any special character', () => { | ||
expect(maskedValue('11 / 1 / 1111', 4, null)).toEqual({ maskedValue: '11 / 1 / 1111', selectionStart: 4 }); | ||
}); | ||
|
||
it('should not add extra char if value length is equal to mask length', () => { | ||
expect(maskedValue('11 / 11 / 11111')).toEqual({ maskedValue: '11 / 11 / 1111', selectionStart: 14 }); | ||
}); | ||
|
||
it('should add special character in between', () => { | ||
it('should add special character automatically in between', () => { | ||
expect(maskedValue('11 / 111')).toEqual({ maskedValue: '11 / 11 / 1', selectionStart: 11 }); | ||
}); | ||
}); |
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
52 changes: 52 additions & 0 deletions
52
packages/core/src/components/TimePicker/TimeOptionList/TimeOptionList.styled.tsx
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,52 @@ | ||
import { centerAligned } from '@medly-components/utils'; | ||
import styled, { css } from 'styled-components'; | ||
|
||
export const TimePicker = styled.div` | ||
height: 100%; | ||
display: flex; | ||
flex-direction: column; | ||
gap: 2.4rem; | ||
align-items: center; | ||
justify-content: center; | ||
overflow: hidden; | ||
z-index: 1; | ||
`; | ||
|
||
export const TimeUList = styled.ul` | ||
height: 100%; | ||
padding: 0; | ||
margin: 0; | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
justify-content: flex-start; | ||
overflow: auto; | ||
scroll-snap-type: y mandatory; | ||
user-select: none; | ||
list-style: none; | ||
&::-webkit-scrollbar { | ||
display: none; | ||
} | ||
`; | ||
|
||
const getFontStyle = (style: 'selectedOption' | 'nonSelectedOption') => css` | ||
font-size: ${({ theme }) => theme.timePicker[style].fontSize}; | ||
font-weight: ${({ theme }) => theme.font.weights[theme.timePicker[style].fontWeight]}; | ||
line-height: ${({ theme }) => theme.timePicker[style].lineHeight}; | ||
letter-spacing: ${({ theme }) => theme.timePicker[style].LetterSpacing}; | ||
color: ${({ theme }) => theme.timePicker[style].color}; | ||
`; | ||
|
||
export const TimeItem = styled('li')<{ isSelected?: boolean }>` | ||
${centerAligned()} | ||
cursor: pointer; | ||
min-height: 4rem; | ||
scroll-snap-align: center; | ||
transition: all 200ms ease-in-ease-out; | ||
${({ isSelected }) => getFontStyle(isSelected ? 'selectedOption' : 'nonSelectedOption')}; | ||
&:hover { | ||
text-decoration: ${({ isSelected }) => (isSelected ? 'underline' : 'none')}; | ||
} | ||
`; |
46 changes: 46 additions & 0 deletions
46
packages/core/src/components/TimePicker/TimeOptionList/TimeOptionList.tsx
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,46 @@ | ||
import type { FC } from 'react'; | ||
import { forwardRef } from 'react'; | ||
import { TimeItem, TimePicker, TimeUList } from './TimeOptionList.styled'; | ||
import type { TimeOptionListProps } from './types'; | ||
|
||
const TIME_OPTIONS_LENGTH = { | ||
HOUR: 12, | ||
MINUTES: 60, | ||
PERIOD: 2 | ||
}; | ||
const PERIOD = ['AM', 'PM']; | ||
|
||
export const TimeOptionList: FC<TimeOptionListProps> = forwardRef<HTMLUListElement, TimeOptionListProps>( | ||
({ value, onChange, type }, ref) => { | ||
const isPeriod = type === 'PERIOD'; | ||
const handleScroll = (e: React.UIEvent<HTMLUListElement>) => { | ||
const height = e.currentTarget.scrollHeight / (TIME_OPTIONS_LENGTH[type] + 4); | ||
const value = Math.floor((e.currentTarget.scrollTop || 0) / height); | ||
onChange(type, value); | ||
}; | ||
|
||
const handleClick = (index: number) => () => (ref as any)?.current?.scrollTo({ top: index * 40, behavior: 'smooth' }); | ||
|
||
return ( | ||
<TimePicker> | ||
<TimeUList ref={ref} onScroll={handleScroll} aria-label={`${type} list`}> | ||
<TimeItem key="-2" /> | ||
<TimeItem key="-1" /> | ||
{Array.from({ length: TIME_OPTIONS_LENGTH[type] }, (_, index) => ( | ||
<TimeItem | ||
key={index} | ||
isSelected={index === value} | ||
onClick={handleClick(index)} | ||
aria-label={isPeriod ? PERIOD[index] : `${index} ${type}`} | ||
> | ||
{isPeriod ? PERIOD[index] : `0${index}`.slice(-2)} | ||
</TimeItem> | ||
))} | ||
<TimeItem key="+1" /> | ||
<TimeItem key="+2" /> | ||
</TimeUList> | ||
</TimePicker> | ||
); | ||
} | ||
); | ||
TimeOptionList.displayName = 'TimeOptionList'; |
1 change: 1 addition & 0 deletions
1
packages/core/src/components/TimePicker/TimeOptionList/index.ts
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 @@ | ||
export * from './TimeOptionList'; |
9 changes: 9 additions & 0 deletions
9
packages/core/src/components/TimePicker/TimeOptionList/types.ts
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,9 @@ | ||
import type { HTMLProps } from '@medly-components/utils'; | ||
|
||
export type TIME_OPTION_TYPE = 'HOUR' | 'MINUTES' | 'PERIOD'; | ||
|
||
export type TimeOptionListProps = Omit<HTMLProps<HTMLUListElement>, 'onChange'> & { | ||
type: TIME_OPTION_TYPE; | ||
value: number; | ||
onChange: (type: TIME_OPTION_TYPE, value: number) => void; | ||
}; |
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
Oops, something went wrong.