Skip to content

Commit

Permalink
fix: change value in input
Browse files Browse the repository at this point in the history
  • Loading branch information
lisalupi committed Nov 12, 2024
1 parent f65d464 commit 9617345
Show file tree
Hide file tree
Showing 41 changed files with 8,049 additions and 17,665 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createContext } from 'react'
import type { Dispatch, SetStateAction } from 'react'
import { DAYS_DEFAULT, MONTHS_ARR_DEFAULT, MONTHS_DEFAULT } from './constants'

export type ContextProps = {
showMonthYearPicker?: boolean
Expand All @@ -23,6 +24,7 @@ export type ContextProps = {
DAYS: Record<string, string>
MONTHS_ARR: string[]
setValue: Dispatch<SetStateAction<Date | null>>
setInputValue: Dispatch<SetStateAction<string | undefined>>
value?: Date | null
range?: {
start: Date | null
Expand All @@ -36,6 +38,7 @@ export type ContextProps = {
>
startDate?: Date | null
endDate?: Date | null
format?: (value?: Date) => string | undefined
} & (
| {
selectsRange: true
Expand All @@ -52,51 +55,11 @@ export type ContextProps = {
) => void
}
)
const DAYS_DEFAULT = {
Sunday: 'Su',
Monday: 'Mo',
Tuesday: 'Tu',
Wednesday: 'We',
Thursday: 'Th',
Friday: 'Fr',
Saturday: 'Sa',
}

// Calendar months names and short names
const MONTHS_DEFAULT = {
January: 'Jan',
February: 'Feb',
March: 'Mar',
April: 'Apr',
May: 'May',
June: 'Jun',
July: 'Jul',
August: 'Aug',
September: 'Sep',
October: 'Oct',
November: 'Nov',
December: 'Dec',
}

export const MONTHS_ARR_DEFAULT = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
]

export const DateInputContext = createContext<ContextProps>({
showMonthYearPicker: false,
disabled: false,
setValue: () => null,
setInputValue: () => null,
monthToShow: 1,
yearToShow: 2000,
setMonthToShow: () => null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,27 @@ import type { StoryFn } from '@storybook/react'
import type { ComponentProps } from 'react'
import { useState } from 'react'
import { DateInput } from '..'
import { Button } from '../../Button'
import { Stack } from '../../Stack'

export const Controlled: StoryFn<ComponentProps<typeof DateInput>> = args => {
const [value, setValue] = useState<
Date | Date[] | [Date | null, Date | null] | null
>(new Date('December 17, 1995 03:24:00'))

return (
<DateInput
{...args}
label="Date"
value={value as Date}
onChange={setValue}
/>
<Stack gap={2}>
<DateInput
{...args}
label="Date"
value={value as Date}
onChange={setValue}
/>
Selected date : {value?.toString()}
<Button onClick={() => setValue(new Date('December 26, 1995 03:24:00'))}>
Set date to 1995-12-26
</Button>
</Stack>
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,11 @@ Disabled.args = {
label: 'Disabled',
disabled: true,
}

Disabled.decorators = [
StoryComponent => (
<div style={{ height: '350px' }}>
<StoryComponent />
</div>
),
]
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,11 @@ Error.args = {
label: 'Error',
error: 'This is an error',
}

Error.decorators = [
StoryComponent => (
<div style={{ height: '350px' }}>
<StoryComponent />
</div>
),
]
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
import { Template } from './Template'

export const Exclude = Template.bind({})
import type { StoryFn } from '@storybook/react'
import type { ComponentProps } from 'react'
import { DateInput } from '..'
import { Stack } from '../../Stack'

export const Exclude: StoryFn<ComponentProps<typeof DateInput>> = args => (
<Stack gap={3}>
<DateInput {...args} />
<DateInput
{...args}
showMonthYearPicker
label="With months"
excludeDates={[
new Date('November 1, 1995 03:24:00'),
new Date('January 14, 1995 03:24:00'),
new Date('March 22, 1995 03:24:00'),
]}
/>
</Stack>
)
Exclude.parameters = {
docs: {
description: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export const I18n: StoryFn<ComponentProps<typeof DateInput>> = args => {
setValue(v as Date)
}}
label={currentLocale}
selectsRange={false}
/>
</div>
)
Expand Down
43 changes: 13 additions & 30 deletions packages/ui/src/components/DateInput/__stories__/Months.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,17 @@
import type { StoryFn } from '@storybook/react'
import { useState } from 'react'
import { DateInput } from '..'
import { Template } from './Template'

export const Months: StoryFn = args => {
const [startDate, setStartDate] = useState<Date | null>(null)
const [endDate, setEndDate] = useState<Date | null>(null)
const onChange = (dates: [Date | null, Date | null]) => {
const [start, end] = dates
setStartDate(start)
setEndDate(end)
}
export const Months = Template.bind({})

return (
<DateInput
label="Date"
startDate={startDate}
endDate={endDate}
onChange={onChange}
showMonthYearPicker
selectsRange
{...args}
/>
)
Months.args = {
label: 'Months',
showMonthYearPicker: true,
disabled: false,
}

Months.parameters = {
docs: {
description: {
story:
'Using prop `showMonthYearPicker` you can select only months and years.',
},
},
}
Months.decorators = [
StoryComponent => (
<div style={{ height: '350px' }}>
<StoryComponent />
</div>
),
]
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,14 @@ import { Template } from './Template'

export const Playground = Template.bind({})

Playground.args = Template.args
Playground.args = {
placeholder: 'YYYY-MM-DD',
}

Playground.decorators = [
StoryComponent => (
<div style={{ height: '350px' }}>
<StoryComponent />
</div>
),
]
64 changes: 54 additions & 10 deletions packages/ui/src/components/DateInput/__stories__/Range.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,69 @@
import type { StoryFn } from '@storybook/react'
import type { ComponentProps } from 'react'
import { useState } from 'react'
import { DateInput } from '..'
import { Stack } from '../../Stack'

export const Range: StoryFn = args => {
const months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
]
export const Range: StoryFn<ComponentProps<typeof DateInput>> = args => {
const [startDate, setStartDate] = useState<Date | null>(null)
const [endDate, setEndDate] = useState<Date | null>(null)
const onChange = (dates: [Date | null, Date | null]) => {

const [startMonth, setStartMonth] = useState<Date | null>(
new Date('March 2024'),
)
const [endMonth, setEndMonth] = useState<Date | null>(null)

const onChange = (dates: [Date | null, Date | null] | Date[]) => {
const [start, end] = dates
setStartDate(start)
setEndDate(end)
}

const onChangeMonth = (dates: [Date | null, Date | null] | Date[]) => {
const [start, end] = dates
setStartMonth(start)
setEndMonth(end)
}

return (
<DateInput
label="Date"
onChange={onChange}
startDate={startDate}
endDate={endDate}
selectsRange
{...args}
/>
<Stack>
<DateInput
label="Date"
onChange={onChange}
startDate={startDate}
endDate={endDate}
selectsRange
{...args}
/>
Selected dates : {startDate?.toDateString()} - {endDate?.toDateString()}
<DateInput
label="Month"
onChange={onChangeMonth}
startDate={startMonth}
endDate={endMonth}
selectsRange
showMonthYearPicker
{...args}
/>
Selected months : {startMonth ? months[startMonth.getMonth()] : null}
{startMonth?.getFullYear()} -
{endMonth ? months[endMonth.getMonth()] : null}
{endMonth?.getFullYear()}
</Stack>
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ export const Size: StoryFn<typeof DateInput> = args => (
))}
</Stack>
)

Size.args = {
value: 'Sat 24 Dec 2024',
}
4 changes: 2 additions & 2 deletions packages/ui/src/components/DateInput/__stories__/Template.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { StoryFn } from '@storybook/react'
import type { ComponentProps } from 'react'
import { DateInput } from '..'

export const Template: StoryFn<ComponentProps<typeof DateInput>> = props => (
export const Template: StoryFn<typeof DateInput> = props => (
<DateInput {...props} />
)

Template.args = {
label: 'Date Input',
required: true,
placeholder: 'YYYY-MM-DD',
}

This file was deleted.

17 changes: 4 additions & 13 deletions packages/ui/src/components/DateInput/__stories__/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,17 @@ import { DateInput } from '..'

export default {
component: DateInput,
decorators: [
Stories => (
<div style={{ height: 360 }}>
<Stories />
</div>
),
],
title: 'Components/Data Entry/DateInput',
} as Meta

export { Playground } from './Playground.stories'
export { Size } from './Size.stories'
export { Exclude } from './Exclude.stories'
export { Range } from './Range.stories'
export { Uncontrolled } from './Uncontrolled.stories'
export { Controlled } from './Controlled.stories'
export { Error } from './Error.stories'
export { Disabled } from './Disabled.stories'
export { Required } from './Required.stories'
export { Months } from './Months.stories'
export { Exclude } from './Exclude.stories'
export { MinMax } from './MinMax.stories'
export { Localized } from './Localized.stories'
export { Controlled } from './Controlled.stories'
export { Range } from './Range.stories'
export { I18n } from './I18n.stories'
export { MinMax } from './MinMax.stories'
Loading

0 comments on commit 9617345

Please sign in to comment.