-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement date picker component
- Loading branch information
1 parent
24b67d5
commit c6c192d
Showing
7 changed files
with
229 additions
and
0 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
16 changes: 16 additions & 0 deletions
16
packages/lsd-react/src/components/DatePicker/DatePicker.classes.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,16 @@ | ||
export const datePickerClasses = { | ||
root: `lsd-date-picker`, | ||
|
||
inputContainer: `lsd-date-picker-input-container`, | ||
input: `lsd-date-picker-input-container__input`, | ||
icon: `lsd-date-picker-input-container__icon`, | ||
clearButton: `lsd-date-picker-input-container__clear-button`, | ||
|
||
supportingText: 'lsd-date-picker__supporting-text', | ||
|
||
disabled: `lsd-date-picker--disabled`, | ||
error: 'lsd-date-picker--error', | ||
|
||
large: `lsd-date-picker--large`, | ||
medium: `lsd-date-picker--medium`, | ||
} |
30 changes: 30 additions & 0 deletions
30
packages/lsd-react/src/components/DatePicker/DatePicker.stories.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,30 @@ | ||
import { Meta, Story } from '@storybook/react' | ||
import { DatePicker, DatePickerProps } from './DatePicker' | ||
|
||
export default { | ||
title: 'DatePicker', | ||
component: DatePicker, | ||
argTypes: { | ||
size: { | ||
type: { | ||
name: 'enum', | ||
value: ['medium', 'large'], | ||
}, | ||
defaultValue: 'large', | ||
}, | ||
}, | ||
} as Meta | ||
|
||
export const Root: Story<DatePickerProps> = ({ ...args }) => { | ||
return <DatePicker {...args}>DatePicker</DatePicker> | ||
} | ||
|
||
Root.args = { | ||
size: 'large', | ||
supportingText: 'Supporting text', | ||
disabled: false, | ||
error: false, | ||
errorIcon: false, | ||
clearButton: true, | ||
onChange: undefined, | ||
} |
82 changes: 82 additions & 0 deletions
82
packages/lsd-react/src/components/DatePicker/DatePicker.styles.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,82 @@ | ||
import { css } from '@emotion/react' | ||
import { datePickerClasses } from './DatePicker.classes' | ||
|
||
export const DatePickerStyles = css` | ||
.${datePickerClasses.root} { | ||
width: auto; | ||
border-bottom: 1px solid rgb(var(--lsd-border-primary)); | ||
box-sizing: border-box; | ||
} | ||
.${datePickerClasses.inputContainer} { | ||
display: flex; | ||
align-items: center; | ||
justify-content: space-between; | ||
} | ||
.${datePickerClasses.disabled} { | ||
opacity: 0.34; | ||
} | ||
.${datePickerClasses.input} { | ||
border: none; | ||
outline: none; | ||
font-size: 14px; | ||
color: rgb(var(--lsd-text-primary)); | ||
background: none; | ||
width: 100%; | ||
} | ||
.${datePickerClasses.input}::-webkit-inner-spin-button, | ||
.${datePickerClasses.input}::-webkit-calendar-picker-indicator { | ||
display: none; | ||
-webkit-appearance: none; | ||
} | ||
.${datePickerClasses.input}:hover { | ||
outline: none; | ||
} | ||
.${datePickerClasses.input}:focus::-webkit-datetime-edit { | ||
color: rgb(var(--lsd-text-primary)); | ||
opacity: 0.3; | ||
} | ||
.${datePickerClasses.error} | ||
.${datePickerClasses.input}::-webkit-datetime-edit-fields-wrapper { | ||
text-decoration: line-through; | ||
} | ||
.${datePickerClasses.supportingText} { | ||
width: fit-content; | ||
margin-top: 20px; | ||
} | ||
.${datePickerClasses.large} { | ||
width: 208px; | ||
height: 40px; | ||
padding: 10px 14px; | ||
} | ||
.${datePickerClasses.medium} { | ||
width: 188px; | ||
height: 32px; | ||
padding: 6px 12px; | ||
} | ||
.${datePickerClasses.clearButton} { | ||
padding: 0; | ||
width: auto; | ||
height: auto; | ||
margin: 0; | ||
border: 0; | ||
svg { | ||
width: 100%; | ||
height: auto; | ||
} | ||
} | ||
.${datePickerClasses.icon} { | ||
} | ||
` |
97 changes: 97 additions & 0 deletions
97
packages/lsd-react/src/components/DatePicker/DatePicker.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,97 @@ | ||
import clsx from 'clsx' | ||
import React, { useRef } from 'react' | ||
import { useInput } from '../../utils/useInput' | ||
import { IconButton } from '../IconButton' | ||
import { CloseIcon, ErrorIcon } from '../Icons' | ||
import { Typography } from '../Typography' | ||
import { datePickerClasses } from './DatePicker.classes' | ||
|
||
export type DatePickerProps = Omit< | ||
React.HTMLAttributes<HTMLDivElement>, | ||
'onChange' | 'value' | ||
> & | ||
Pick<React.InputHTMLAttributes<HTMLInputElement>, 'onChange'> & { | ||
size?: 'large' | 'medium' | ||
error?: boolean | ||
errorIcon?: boolean | ||
clearButton?: boolean | ||
disabled?: boolean | ||
supportingText?: string | ||
value?: string | ||
defaultValue?: string | ||
placeholder?: string | ||
inputProps?: React.InputHTMLAttributes<HTMLInputElement> | ||
} | ||
|
||
export const DatePicker: React.FC<DatePickerProps> & { | ||
classes: typeof datePickerClasses | ||
} = ({ | ||
size = 'large', | ||
error = false, | ||
errorIcon = false, | ||
clearButton, | ||
supportingText, | ||
children, | ||
value, | ||
placeholder, | ||
defaultValue, | ||
disabled, | ||
onChange, | ||
inputProps = {}, | ||
...props | ||
}) => { | ||
const ref = useRef<HTMLInputElement>(null) | ||
const input = useInput({ defaultValue, value, onChange, ref }) | ||
|
||
const onCancel = () => input.setValue('') | ||
|
||
return ( | ||
<div | ||
aria-disabled={disabled ? 'true' : 'false'} | ||
{...props} | ||
className={clsx( | ||
props.className, | ||
datePickerClasses.root, | ||
datePickerClasses[size], | ||
disabled && datePickerClasses.disabled, | ||
error && datePickerClasses.error, | ||
)} | ||
> | ||
<div className={datePickerClasses.inputContainer}> | ||
<input | ||
type="date" | ||
placeholder={placeholder} | ||
{...inputProps} | ||
ref={ref} | ||
value={input.value} | ||
onChange={input.onChange} | ||
className={clsx(inputProps.className, datePickerClasses.input)} | ||
/> | ||
{error && errorIcon ? ( | ||
<ErrorIcon color="primary" className={datePickerClasses.icon} /> | ||
) : clearButton && input.filled ? ( | ||
<IconButton | ||
disabled={disabled} | ||
onClick={() => !disabled && onCancel()} | ||
aria-label="clear" | ||
className={datePickerClasses.clearButton} | ||
> | ||
<CloseIcon color="primary" className={datePickerClasses.icon} /> | ||
</IconButton> | ||
) : null} | ||
</div> | ||
{supportingText && ( | ||
<div className={clsx(datePickerClasses.supportingText)}> | ||
<Typography | ||
variant={size === 'large' ? 'label1' : 'label2'} | ||
component="p" | ||
> | ||
{supportingText} | ||
</Typography> | ||
</div> | ||
)} | ||
</div> | ||
) | ||
} | ||
|
||
DatePicker.classes = datePickerClasses |
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 './DatePicker' |
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