-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathDatepicker.stories.tsx
200 lines (183 loc) · 6.56 KB
/
Datepicker.stories.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import { SecondaryButton } from '@sb1/ffe-buttons-react';
import { Input, InputGroup } from '@sb1/ffe-form-react';
import type { Meta, StoryObj } from '@storybook/react';
import { default as React, useState } from 'react';
import { Datepicker, DatepickerProps } from './Datepicker';
const meta: Meta<typeof Datepicker> = {
title: 'Komponenter/Datepicker/Datepicker',
component: Datepicker,
};
export default meta;
type Story = StoryObj<typeof Datepicker>;
export const Standard: Story = {
args: {
locale: 'nb',
maxDate: '31.12.2025',
minDate: '01.01.2024',
labelId: 'datepicker-label',
},
render: function Render({ value, onChange, ...args }: DatepickerProps) {
const [date, setDate] = useState('01.12.2024');
return (
<InputGroup label="Dato" labelId={Standard?.args?.labelId}>
{inputProps => (
<Datepicker
value={value ?? date}
onChange={setDate}
{...inputProps}
{...args}
/>
)}
</InputGroup>
);
},
};
export const FieldMessageString: Story = {
args: {
...Standard.args,
},
render: function Render({ value, onChange, ...args }: DatepickerProps) {
const [date, setDate] = useState('01.12.2024');
return (
<InputGroup
label="Dato"
aria-invalid={true}
fieldMessage={'Ugyldig dato'}
labelId={Standard?.args?.labelId}
>
{inputProps => (
<Datepicker
value={value ?? date}
onChange={setDate}
{...inputProps}
{...args}
/>
)}
</InputGroup>
);
},
};
export const FullWidth: Story = {
args: {
...Standard.args,
fullWidth: true,
},
render: function Render({ value, onChange, ...args }: DatepickerProps) {
const [date, setDate] = useState('01.12.2024');
return (
<InputGroup label="Dato" labelId={Standard?.args?.labelId}>
<Datepicker
value={value ?? date}
onChange={setDate}
{...args}
/>
</InputGroup>
);
},
};
export const CalendarAbove: Story = {
args: {
...Standard.args,
calendarAbove: true,
},
render: function Render({ value, onChange, ...args }: DatepickerProps) {
const [date, setDate] = useState('01.12.2024');
return (
<InputGroup label="Dato" labelId={Standard?.args?.labelId}>
<Datepicker
value={value ?? date}
onChange={setDate}
{...args}
/>
</InputGroup>
);
},
};
export const TwoWayControlledComponent: Story = {
args: {
...Standard.args,
},
render: function Render({ ...args }: DatepickerProps) {
const [date, setDate] = useState('01.12.2024');
const [lastUpdateSource, setLastUpdateSource] = useState('initiell');
const handleDateChange = (newDate: string) => {
setDate(newDate);
setLastUpdateSource('datepicker');
};
const predefinedDates = [
{
label: 'I dag',
value: () => {
const today = new Date();
return `${String(today.getDate()).padStart(2, '0')}.${String(today.getMonth() + 1).padStart(2, '0')}.${today.getFullYear()}`;
},
},
{ label: 'Begynnelsen av året', value: '01.01.2024' },
{ label: 'Slutten av året', value: '31.12.2024' },
{ label: 'Midten av året', value: '01.07.2024' },
];
return (
<div>
<InputGroup label="Dato" labelId={Standard?.args?.labelId}>
<Datepicker
{...args}
value={date}
onChange={handleDateChange}
/>
</InputGroup>
<div style={{ marginTop: '20px' }}>
<div>Kontroller datepicker eksternt:</div>
<div
style={{
display: 'flex',
gap: '10px',
marginTop: '10px',
}}
>
{predefinedDates.map((presetDate, index) => (
<SecondaryButton
key={index}
onClick={() => {
const newValue =
typeof presetDate.value === 'function'
? presetDate.value()
: presetDate.value;
setDate(newValue);
setLastUpdateSource('ekstern');
}}
>
{presetDate.label}
</SecondaryButton>
))}
</div>
<div style={{ marginTop: '20px' }}>
<div>Manuell ekstern input:</div>
<div
style={{
display: 'flex',
gap: '10px',
maxWidth: '200px',
marginTop: '10px',
}}
>
<Input
type="text"
value={date}
onChange={e => {
setDate(e.target.value);
setLastUpdateSource('manuell');
}}
placeholder="dd.mm.åååå"
/>
</div>
</div>
<div style={{ marginTop: '20px' }}>
<div>Nåværende tilstand:</div>
<div>Dato: {date}</div>
<div>Sist oppdatert av: {lastUpdateSource}</div>
</div>
</div>
</div>
);
},
};