@transferwise/components 46.40.0 → 46.41.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/index.js +68 -74
- package/build/index.js.map +1 -1
- package/build/index.mjs +69 -75
- package/build/index.mjs.map +1 -1
- package/build/main.css +4 -2
- package/build/styles/main.css +4 -2
- package/build/styles/statusIcon/StatusIcon.css +4 -2
- package/build/types/dateInput/DateInput.d.ts.map +1 -1
- package/build/types/field/Field.d.ts +6 -1
- package/build/types/field/Field.d.ts.map +1 -1
- package/build/types/inlineAlert/InlineAlert.d.ts +2 -2
- package/build/types/inlineAlert/InlineAlert.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/dateInput/DateInput.spec.tsx +220 -0
- package/src/dateInput/DateInput.story.tsx +3 -76
- package/src/dateInput/DateInput.tests.story.tsx +238 -0
- package/src/dateInput/DateInput.tsx +50 -53
- package/src/field/Field.story.tsx +17 -36
- package/src/field/Field.tests.story.tsx +33 -0
- package/src/field/Field.tsx +23 -13
- package/src/inlineAlert/InlineAlert.story.tsx +13 -5
- package/src/inlineAlert/InlineAlert.tsx +14 -7
- package/src/main.css +4 -2
- package/src/statusIcon/StatusIcon.css +4 -2
- package/src/statusIcon/StatusIcon.less +4 -2
- package/src/statusIcon/StatusIcon.tsx +1 -1
- package/src/dateInput/DateInput.rtl.spec.tsx +0 -17
- package/src/dateInput/DateInput.spec.js +0 -477
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import { Meta, StoryObj } from '@storybook/react';
|
|
2
|
+
import { userEvent, within, fn } from '@storybook/test';
|
|
3
|
+
|
|
4
|
+
import { DateInput, DateMode, Info, InlineAlert, Title, Typography } from '..';
|
|
5
|
+
import { lorem10, storyConfig } from '../test-utils';
|
|
6
|
+
|
|
7
|
+
import Provider from '../provider/Provider';
|
|
8
|
+
import translations from '../i18n';
|
|
9
|
+
|
|
10
|
+
const meta = {
|
|
11
|
+
component: DateInput,
|
|
12
|
+
title: 'Forms/DateInput/Tests',
|
|
13
|
+
args: {
|
|
14
|
+
dayLabel: 'Day input',
|
|
15
|
+
dayAutoComplete: 'bday-day',
|
|
16
|
+
monthLabel: 'Month select',
|
|
17
|
+
yearLabel: 'Year input',
|
|
18
|
+
yearAutoComplete: 'bday-year',
|
|
19
|
+
},
|
|
20
|
+
} satisfies Meta<typeof DateInput>;
|
|
21
|
+
|
|
22
|
+
export default meta;
|
|
23
|
+
|
|
24
|
+
type Story = StoryObj<typeof meta>;
|
|
25
|
+
|
|
26
|
+
const Basic = {
|
|
27
|
+
args: {
|
|
28
|
+
onChange: fn(),
|
|
29
|
+
},
|
|
30
|
+
play: async ({ canvasElement }) => {
|
|
31
|
+
const canvas = within(canvasElement);
|
|
32
|
+
await userEvent.click(canvas.getByRole('button'));
|
|
33
|
+
},
|
|
34
|
+
} satisfies Story;
|
|
35
|
+
|
|
36
|
+
export const WithLabel = {
|
|
37
|
+
args: {
|
|
38
|
+
onChange: fn(),
|
|
39
|
+
},
|
|
40
|
+
render: (args) => {
|
|
41
|
+
const id1 = 'date-input-group-label-1';
|
|
42
|
+
const label = 'Date of Birth';
|
|
43
|
+
|
|
44
|
+
return (
|
|
45
|
+
<>
|
|
46
|
+
<Title type={Typography.TITLE_SUBSECTION}>
|
|
47
|
+
label (as <code>div</code> element) is linked with <code>DateInput</code> via{' '}
|
|
48
|
+
<code>aria-labelledby</code> prop
|
|
49
|
+
</Title>
|
|
50
|
+
<div className="control-label" id={id1}>
|
|
51
|
+
Date of Delivery
|
|
52
|
+
</div>
|
|
53
|
+
<DateInput {...args} aria-labelledby={id1} />
|
|
54
|
+
|
|
55
|
+
<br />
|
|
56
|
+
|
|
57
|
+
<Title type={Typography.TITLE_SUBSECTION}>
|
|
58
|
+
label (as <code>div</code> element) is detached but <code>DateInput</code> has same label
|
|
59
|
+
via <code>aria-label</code> attribute
|
|
60
|
+
</Title>
|
|
61
|
+
<div className="control-label">
|
|
62
|
+
{label}{' '}
|
|
63
|
+
<Info aria-label="Fast transfer hint" title="Fast transfer hint" content={lorem10} />
|
|
64
|
+
</div>
|
|
65
|
+
<DateInput {...args} aria-label={label} />
|
|
66
|
+
|
|
67
|
+
<br />
|
|
68
|
+
|
|
69
|
+
<Title type={Typography.TITLE_SUBSECTION}>
|
|
70
|
+
<code>DateInput</code> wrapped in <code>fieldset</code> + using <code>legend</code> as
|
|
71
|
+
label (rare use case)
|
|
72
|
+
</Title>
|
|
73
|
+
<fieldset>
|
|
74
|
+
<legend className="control-label">
|
|
75
|
+
Expiry Date for Credit Card (example of MONTH_YEAR mode)
|
|
76
|
+
</legend>
|
|
77
|
+
<DateInput {...args} mode={DateMode.MONTH_YEAR} />
|
|
78
|
+
</fieldset>
|
|
79
|
+
</>
|
|
80
|
+
);
|
|
81
|
+
},
|
|
82
|
+
} satisfies Story;
|
|
83
|
+
|
|
84
|
+
export const InputError = {
|
|
85
|
+
...Basic,
|
|
86
|
+
render: (args) => (
|
|
87
|
+
<div className="form-group has-error">
|
|
88
|
+
<DateInput {...args} value={new Date('2010-04-05')} />
|
|
89
|
+
<InlineAlert type="error">{lorem10}</InlineAlert>
|
|
90
|
+
</div>
|
|
91
|
+
),
|
|
92
|
+
} satisfies Story;
|
|
93
|
+
|
|
94
|
+
export const CustomPlaceholders = {
|
|
95
|
+
...Basic,
|
|
96
|
+
args: {
|
|
97
|
+
placeholders: {
|
|
98
|
+
day: 'D',
|
|
99
|
+
month: 'M',
|
|
100
|
+
year: 'YY',
|
|
101
|
+
},
|
|
102
|
+
},
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
export const SingleZero = storyConfig(Basic, {});
|
|
106
|
+
SingleZero.play = async ({ canvasElement, step }) => {
|
|
107
|
+
const canvas = within(canvasElement);
|
|
108
|
+
await step('can enter leading zero on day field', async () => {
|
|
109
|
+
const dayInput = await canvas.findByRole('textbox', { name: /day/i });
|
|
110
|
+
await userEvent.click(dayInput);
|
|
111
|
+
await userEvent.type(dayInput, '0');
|
|
112
|
+
});
|
|
113
|
+
await step('can enter 1 leading zero on year field', async () => {
|
|
114
|
+
const yearInput = await canvas.findByRole('textbox', { name: /year/i });
|
|
115
|
+
await userEvent.click(yearInput);
|
|
116
|
+
await userEvent.type(yearInput, '0');
|
|
117
|
+
});
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
export const ZeroesAsValue = storyConfig(Basic, {});
|
|
121
|
+
ZeroesAsValue.play = async ({ canvasElement, step }) => {
|
|
122
|
+
const canvas = within(canvasElement);
|
|
123
|
+
await step('can enter 2 leading zeroes on day field', async () => {
|
|
124
|
+
const dayInput = await canvas.findByRole('textbox', { name: /day/i });
|
|
125
|
+
await userEvent.click(dayInput);
|
|
126
|
+
await userEvent.type(dayInput, '00');
|
|
127
|
+
});
|
|
128
|
+
await step('can enter 4 leading zeroes on year field', async () => {
|
|
129
|
+
const yearInput = await canvas.findByRole('textbox', { name: /year/i });
|
|
130
|
+
await userEvent.click(yearInput);
|
|
131
|
+
await userEvent.type(yearInput, '0000');
|
|
132
|
+
});
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
export const ZeroesBeforeValue = storyConfig(Basic, {});
|
|
136
|
+
ZeroesBeforeValue.play = async ({ canvasElement, step }) => {
|
|
137
|
+
const canvas = within(canvasElement);
|
|
138
|
+
await step('can enter leading zeroes on day followed by day', async () => {
|
|
139
|
+
const dayInput = await canvas.findByRole('textbox', { name: /day/i });
|
|
140
|
+
await userEvent.click(dayInput);
|
|
141
|
+
await userEvent.type(dayInput, '01');
|
|
142
|
+
});
|
|
143
|
+
await step('can enter leading zeroes on year followed by year', async () => {
|
|
144
|
+
const yearInput = await canvas.findByRole('textbox', { name: /year/i });
|
|
145
|
+
await userEvent.click(yearInput);
|
|
146
|
+
await userEvent.type(yearInput, '0001');
|
|
147
|
+
});
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
export const MaxLengthRespected = storyConfig(Basic, {});
|
|
151
|
+
MaxLengthRespected.play = async ({ canvasElement, step }) => {
|
|
152
|
+
const canvas = within(canvasElement);
|
|
153
|
+
await step('can only enter 4 digits in the year field', async () => {
|
|
154
|
+
const yearInput = await canvas.findByRole('textbox', { name: /year/i });
|
|
155
|
+
await userEvent.click(yearInput);
|
|
156
|
+
await userEvent.type(yearInput, '11111111');
|
|
157
|
+
});
|
|
158
|
+
await step('can only enter 2 digits in the day field', async () => {
|
|
159
|
+
const dayInput = await canvas.findByRole('textbox', { name: /day/i });
|
|
160
|
+
await userEvent.click(dayInput);
|
|
161
|
+
await userEvent.type(dayInput, '11111111');
|
|
162
|
+
});
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
export const LocalizationWorks: Story = {
|
|
166
|
+
...Basic,
|
|
167
|
+
decorators: [
|
|
168
|
+
(Story) => {
|
|
169
|
+
const locale = 'zh-HK';
|
|
170
|
+
return (
|
|
171
|
+
<Provider i18n={{ locale, messages: translations[locale] }}>
|
|
172
|
+
<Story />
|
|
173
|
+
</Provider>
|
|
174
|
+
);
|
|
175
|
+
},
|
|
176
|
+
],
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
export const YearFirst: Story = {
|
|
180
|
+
...Basic,
|
|
181
|
+
decorators: [
|
|
182
|
+
(Story) => {
|
|
183
|
+
const locale = 'ja';
|
|
184
|
+
return (
|
|
185
|
+
<Provider i18n={{ locale, messages: translations[locale] }}>
|
|
186
|
+
<Story />
|
|
187
|
+
</Provider>
|
|
188
|
+
);
|
|
189
|
+
},
|
|
190
|
+
],
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
export const MonthFirst: Story = {
|
|
194
|
+
...Basic,
|
|
195
|
+
decorators: [
|
|
196
|
+
(Story) => {
|
|
197
|
+
const locale = 'en-US';
|
|
198
|
+
return (
|
|
199
|
+
<Provider i18n={{ locale, messages: translations[locale] }}>
|
|
200
|
+
<Story />
|
|
201
|
+
</Provider>
|
|
202
|
+
);
|
|
203
|
+
},
|
|
204
|
+
],
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
export const DayFirst: Story = {
|
|
208
|
+
...Basic,
|
|
209
|
+
decorators: [
|
|
210
|
+
(Story) => {
|
|
211
|
+
const locale = 'en';
|
|
212
|
+
return (
|
|
213
|
+
<Provider i18n={{ locale, messages: translations[locale] }}>
|
|
214
|
+
<Story />
|
|
215
|
+
</Provider>
|
|
216
|
+
);
|
|
217
|
+
},
|
|
218
|
+
],
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
export const NoNonDigitsAllowed = storyConfig(Basic, {});
|
|
222
|
+
NoNonDigitsAllowed.play = async ({ canvasElement, step }) => {
|
|
223
|
+
const canvas = within(canvasElement);
|
|
224
|
+
await step('can only enter digits in the day field', async () => {
|
|
225
|
+
const dayInput = await canvas.findByRole('textbox', { name: /day/i });
|
|
226
|
+
await userEvent.click(dayInput);
|
|
227
|
+
await userEvent.type(dayInput, 'abcd1');
|
|
228
|
+
});
|
|
229
|
+
await step('can only enter digits in the year field', async () => {
|
|
230
|
+
const yearInput = await canvas.findByRole('textbox', { name: /year/i });
|
|
231
|
+
await userEvent.click(yearInput);
|
|
232
|
+
await userEvent.type(yearInput, 'abcd2');
|
|
233
|
+
});
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
export const BasicMobile = storyConfig(Basic, { variants: ['mobile'] });
|
|
237
|
+
|
|
238
|
+
export const InputErrorMobile = storyConfig(InputError, { variants: ['mobile'] });
|
|
@@ -2,8 +2,16 @@ import classNames from 'classnames';
|
|
|
2
2
|
import { useState } from 'react';
|
|
3
3
|
import { useIntl } from 'react-intl';
|
|
4
4
|
|
|
5
|
-
import { Input, SelectInput, SelectInputOptionContent, SelectInputProps } from '..';
|
|
6
|
-
import {
|
|
5
|
+
import { Body, Input, SelectInput, SelectInputOptionContent, SelectInputProps } from '..';
|
|
6
|
+
import {
|
|
7
|
+
DateMode,
|
|
8
|
+
MonthFormat,
|
|
9
|
+
Size,
|
|
10
|
+
SizeLarge,
|
|
11
|
+
SizeMedium,
|
|
12
|
+
SizeSmall,
|
|
13
|
+
Typography,
|
|
14
|
+
} from '../common';
|
|
7
15
|
import { MDY, YMD, getMonthNames, isDateValid, isMonthAndYearFormat } from '../common/dateUtils';
|
|
8
16
|
import { useInputAttributes } from '../inputs/contexts';
|
|
9
17
|
import messages from './DateInput.messages';
|
|
@@ -93,8 +101,10 @@ const DateInput = ({
|
|
|
93
101
|
};
|
|
94
102
|
|
|
95
103
|
const [day, setDay] = useState(() => getInitialDate('day'));
|
|
104
|
+
const [displayDay, setDisplayDay] = useState(day?.toString());
|
|
96
105
|
const [month, setMonth] = useState(() => getInitialDate('month'));
|
|
97
106
|
const [year, setYear] = useState(() => getInitialDate('year'));
|
|
107
|
+
const [displayYear, setDisplayYear] = useState(year?.toString());
|
|
98
108
|
const [lastBroadcastedValue, setLastBroadcastedValue] = useState<Date | null | undefined>(
|
|
99
109
|
getDateObject,
|
|
100
110
|
);
|
|
@@ -129,7 +139,7 @@ const DateInput = ({
|
|
|
129
139
|
const getSelectElement = () => {
|
|
130
140
|
return (
|
|
131
141
|
<label className="d-flex flex-column">
|
|
132
|
-
<
|
|
142
|
+
<Body type={Typography.BODY_DEFAULT}>{monthLabel}</Body>
|
|
133
143
|
<SelectInput
|
|
134
144
|
name="month"
|
|
135
145
|
disabled={disabled}
|
|
@@ -147,13 +157,25 @@ const DateInput = ({
|
|
|
147
157
|
);
|
|
148
158
|
};
|
|
149
159
|
|
|
160
|
+
const isDayValid = (newDay: number, newMonth: number, newYear: number) => {
|
|
161
|
+
const maxDay = new Date(newYear, newMonth + 1, 0).getDate();
|
|
162
|
+
return newDay <= maxDay;
|
|
163
|
+
};
|
|
164
|
+
|
|
150
165
|
const handleInternalValue = (newDay = day, newMonth = month, newYear = year) => {
|
|
151
|
-
if (newDay == null || newMonth == null || newYear == null) {
|
|
166
|
+
if (newDay == null || newDay === 0 || newMonth == null || newYear == null || newYear === 0) {
|
|
167
|
+
broadcastNewValue(null);
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
if (!isDayValid(newDay, newMonth, newYear)) {
|
|
152
171
|
broadcastNewValue(null);
|
|
153
172
|
return;
|
|
154
173
|
}
|
|
155
174
|
|
|
156
175
|
const dateValue = new Date(newYear, newMonth, newDay);
|
|
176
|
+
if (newYear < 100) {
|
|
177
|
+
dateValue.setFullYear(newYear);
|
|
178
|
+
}
|
|
157
179
|
|
|
158
180
|
if (!isDateValid(dateValue)) {
|
|
159
181
|
broadcastNewValue(null);
|
|
@@ -170,9 +192,12 @@ const DateInput = ({
|
|
|
170
192
|
};
|
|
171
193
|
|
|
172
194
|
const handleDayChange = (event: React.ChangeEvent<HTMLInputElement>) => {
|
|
173
|
-
const
|
|
174
|
-
|
|
175
|
-
|
|
195
|
+
const newDayString = event.target.value.replace(/\D/g, '');
|
|
196
|
+
const newDayNumber = Number.parseInt(newDayString, 10);
|
|
197
|
+
|
|
198
|
+
setDay(newDayNumber);
|
|
199
|
+
setDisplayDay(newDayString);
|
|
200
|
+
handleInternalValue(newDayNumber, month, year);
|
|
176
201
|
};
|
|
177
202
|
|
|
178
203
|
const handleMonthChange = (selectedMonth: number | null) => {
|
|
@@ -181,30 +206,21 @@ const DateInput = ({
|
|
|
181
206
|
handleInternalValue(day, null, year);
|
|
182
207
|
return;
|
|
183
208
|
}
|
|
184
|
-
const { checkedDay } = checkDate(day, selectedMonth, year);
|
|
185
209
|
setMonth(selectedMonth);
|
|
186
|
-
|
|
187
|
-
setDay(checkedDay);
|
|
188
|
-
}
|
|
189
|
-
handleInternalValue(checkedDay, selectedMonth, year);
|
|
210
|
+
handleInternalValue(day, selectedMonth, year);
|
|
190
211
|
};
|
|
191
212
|
|
|
192
213
|
const handleYearChange = (event: React.ChangeEvent<HTMLInputElement>) => {
|
|
193
|
-
const
|
|
194
|
-
const
|
|
195
|
-
|
|
196
|
-
if (slicedYear.toString().length === 4) {
|
|
197
|
-
// Correct day based on year and month.
|
|
198
|
-
const { checkedDay } = checkDate(day, month, Number.parseInt(newValue, 10));
|
|
199
|
-
|
|
200
|
-
if (day && checkedDay !== day) {
|
|
201
|
-
setDay(checkedDay);
|
|
202
|
-
}
|
|
214
|
+
const newYearString = event.target.value.replace(/\D/g, '');
|
|
215
|
+
const newYearNumber = Number.parseInt(newYearString, 10);
|
|
203
216
|
|
|
204
|
-
|
|
205
|
-
|
|
217
|
+
if (newYearString.length >= 4 && newYearString.length <= 6) {
|
|
218
|
+
setYear(newYearNumber);
|
|
219
|
+
setDisplayYear(newYearString);
|
|
220
|
+
handleInternalValue(day, month, newYearNumber);
|
|
206
221
|
} else {
|
|
207
|
-
setYear(
|
|
222
|
+
setYear(null);
|
|
223
|
+
setDisplayYear(newYearString);
|
|
208
224
|
handleInternalValue(day, month, null);
|
|
209
225
|
}
|
|
210
226
|
};
|
|
@@ -216,29 +232,6 @@ const DateInput = ({
|
|
|
216
232
|
}
|
|
217
233
|
};
|
|
218
234
|
|
|
219
|
-
const checkDate = (
|
|
220
|
-
newDay: number | null = null,
|
|
221
|
-
newMonth: number | null = 0,
|
|
222
|
-
newYear: number | null = null,
|
|
223
|
-
) => {
|
|
224
|
-
let checkedDay = newDay;
|
|
225
|
-
const maxDay = new Date(newYear || 2000, newMonth != null ? newMonth + 1 : 1, 0).getDate();
|
|
226
|
-
|
|
227
|
-
if (!newDay) {
|
|
228
|
-
checkedDay = null;
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
if (newDay && newDay < 0) {
|
|
232
|
-
checkedDay = 1;
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
if ((newDay && newMonth) || (newDay && newDay > 31)) {
|
|
236
|
-
checkedDay = newDay > maxDay ? maxDay : newDay;
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
return { checkedDay, checkedMonth: newMonth, checkedYear: newYear };
|
|
240
|
-
};
|
|
241
|
-
|
|
242
235
|
const monthYearOnly = mode === DateMode.MONTH_YEAR;
|
|
243
236
|
|
|
244
237
|
const monthWidth = classNames({
|
|
@@ -254,7 +247,7 @@ const DateInput = ({
|
|
|
254
247
|
return (
|
|
255
248
|
<div className="col-sm-3">
|
|
256
249
|
<label>
|
|
257
|
-
<
|
|
250
|
+
<Body type={Typography.BODY_DEFAULT}>{dayLabel}</Body>
|
|
258
251
|
<div className={`input-group input-group-${size}`}>
|
|
259
252
|
<Input
|
|
260
253
|
type="text"
|
|
@@ -262,10 +255,12 @@ const DateInput = ({
|
|
|
262
255
|
pattern="[0-9]*"
|
|
263
256
|
name="day"
|
|
264
257
|
autoComplete={dayAutoComplete}
|
|
265
|
-
value={
|
|
258
|
+
value={displayDay || ''}
|
|
266
259
|
placeholder={placeholders?.day}
|
|
267
260
|
disabled={disabled}
|
|
268
261
|
min={1}
|
|
262
|
+
max={31}
|
|
263
|
+
maxLength={2}
|
|
269
264
|
onChange={(event) => handleDayChange(event)}
|
|
270
265
|
/>
|
|
271
266
|
</div>
|
|
@@ -278,7 +273,7 @@ const DateInput = ({
|
|
|
278
273
|
return (
|
|
279
274
|
<div className="col-sm-4">
|
|
280
275
|
<label>
|
|
281
|
-
<
|
|
276
|
+
<Body type={Typography.BODY_DEFAULT}>{yearLabel}</Body>
|
|
282
277
|
<div className={`input-group input-group-${size}`}>
|
|
283
278
|
<Input
|
|
284
279
|
type="text"
|
|
@@ -287,9 +282,11 @@ const DateInput = ({
|
|
|
287
282
|
name="year"
|
|
288
283
|
autoComplete={yearAutoComplete}
|
|
289
284
|
placeholder={placeholders?.year}
|
|
290
|
-
value={
|
|
285
|
+
value={displayYear || ''}
|
|
291
286
|
disabled={disabled}
|
|
292
|
-
min={
|
|
287
|
+
min={0}
|
|
288
|
+
max={9999}
|
|
289
|
+
maxLength={6}
|
|
293
290
|
onChange={(event) => handleYearChange(event)}
|
|
294
291
|
/>
|
|
295
292
|
</div>
|
|
@@ -2,10 +2,12 @@ import { useState } from 'react';
|
|
|
2
2
|
|
|
3
3
|
import { Input } from '../inputs/Input';
|
|
4
4
|
import { Field } from './Field';
|
|
5
|
+
import { Sentiment } from '../common';
|
|
5
6
|
|
|
6
7
|
export default {
|
|
7
8
|
component: Field,
|
|
8
9
|
title: 'Field',
|
|
10
|
+
tags: ['autodocs'],
|
|
9
11
|
};
|
|
10
12
|
|
|
11
13
|
export const Basic = () => {
|
|
@@ -17,43 +19,22 @@ export const Basic = () => {
|
|
|
17
19
|
);
|
|
18
20
|
};
|
|
19
21
|
|
|
20
|
-
export const
|
|
22
|
+
export const WithStatusMessages = () => {
|
|
21
23
|
const [value, setValue] = useState<string | undefined>('This is some text');
|
|
22
24
|
return (
|
|
23
|
-
<
|
|
24
|
-
<
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
<
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
export const WithHelpAndErrorOnBlur = () => {
|
|
39
|
-
const [value, setValue] = useState<string | undefined>('This is some text');
|
|
40
|
-
const [error, setError] = useState<string | undefined>(undefined);
|
|
41
|
-
return (
|
|
42
|
-
<Field label="Phone number" hint="Please include country code" error={error}>
|
|
43
|
-
<Input
|
|
44
|
-
value={value}
|
|
45
|
-
onChange={({ target }) => {
|
|
46
|
-
setValue(target.value);
|
|
47
|
-
setError(undefined);
|
|
48
|
-
}}
|
|
49
|
-
onBlur={() => {
|
|
50
|
-
if (!value) {
|
|
51
|
-
setError('This is a required field');
|
|
52
|
-
} else {
|
|
53
|
-
setError(undefined);
|
|
54
|
-
}
|
|
55
|
-
}}
|
|
56
|
-
/>
|
|
57
|
-
</Field>
|
|
25
|
+
<div>
|
|
26
|
+
<Field label="Phone number" sentiment={Sentiment.POSITIVE} message="Positive message">
|
|
27
|
+
<Input value={value} onChange={({ target }) => setValue(target.value)} />
|
|
28
|
+
</Field>
|
|
29
|
+
<Field label="Phone number" sentiment={Sentiment.WARNING} message="Warning message">
|
|
30
|
+
<Input value={value} onChange={({ target }) => setValue(target.value)} />
|
|
31
|
+
</Field>
|
|
32
|
+
<Field label="Phone number" sentiment={Sentiment.NEGATIVE} message="This is a required field">
|
|
33
|
+
<Input value={value} onChange={({ target }) => setValue(target.value)} />
|
|
34
|
+
</Field>
|
|
35
|
+
<Field label="Phone number" message="This is a helpful message">
|
|
36
|
+
<Input value={value} onChange={({ target }) => setValue(target.value)} />
|
|
37
|
+
</Field>
|
|
38
|
+
</div>
|
|
58
39
|
);
|
|
59
40
|
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { useState } from 'react';
|
|
2
|
+
|
|
3
|
+
import { Input } from '../inputs/Input';
|
|
4
|
+
import { Field } from './Field';
|
|
5
|
+
import { Sentiment } from '../common';
|
|
6
|
+
|
|
7
|
+
export default {
|
|
8
|
+
component: Field,
|
|
9
|
+
title: 'Field/Tests',
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export const WithHelpAndErrorOnBlur = () => {
|
|
13
|
+
const [value, setValue] = useState('This is some text');
|
|
14
|
+
const [error, setError] = useState<string | undefined>(undefined);
|
|
15
|
+
return (
|
|
16
|
+
<Field
|
|
17
|
+
label="Phone number"
|
|
18
|
+
sentiment={error ? Sentiment.NEGATIVE : Sentiment.NEUTRAL}
|
|
19
|
+
message={error || 'Please include country code'}
|
|
20
|
+
>
|
|
21
|
+
<Input
|
|
22
|
+
value={value}
|
|
23
|
+
onChange={({ target }) => {
|
|
24
|
+
setValue(target.value);
|
|
25
|
+
setError(undefined);
|
|
26
|
+
}}
|
|
27
|
+
onBlur={() => {
|
|
28
|
+
setError('Something went wrong');
|
|
29
|
+
}}
|
|
30
|
+
/>
|
|
31
|
+
</Field>
|
|
32
|
+
);
|
|
33
|
+
};
|
package/src/field/Field.tsx
CHANGED
|
@@ -15,15 +15,28 @@ export type FieldProps = {
|
|
|
15
15
|
/** `null` disables auto-generating the `id` attribute, falling back to nesting-based label association over setting `htmlFor` explicitly. */
|
|
16
16
|
id?: string | null;
|
|
17
17
|
label: React.ReactNode;
|
|
18
|
+
/** @deprecated use `message` and `type={Sentiment.NEUTRAL}` prop instead */
|
|
18
19
|
hint?: React.ReactNode;
|
|
20
|
+
message?: React.ReactNode;
|
|
21
|
+
/** @deprecated use `message` and `type={Sentiment.NEGATIVE}` prop instead */
|
|
19
22
|
error?: React.ReactNode;
|
|
23
|
+
sentiment?: `${Sentiment.NEGATIVE | Sentiment.NEUTRAL | Sentiment.POSITIVE | Sentiment.WARNING}`;
|
|
20
24
|
className?: string;
|
|
21
25
|
children?: React.ReactNode;
|
|
22
26
|
};
|
|
23
27
|
|
|
24
|
-
export const Field = ({
|
|
25
|
-
|
|
26
|
-
|
|
28
|
+
export const Field = ({
|
|
29
|
+
id,
|
|
30
|
+
label,
|
|
31
|
+
message: propMessage,
|
|
32
|
+
sentiment: propType = Sentiment.NEUTRAL,
|
|
33
|
+
className,
|
|
34
|
+
children,
|
|
35
|
+
...props
|
|
36
|
+
}: FieldProps) => {
|
|
37
|
+
const sentiment = props.error ? Sentiment.NEGATIVE : propType;
|
|
38
|
+
const message = props.error || props.hint || propMessage;
|
|
39
|
+
const hasError = sentiment === Sentiment.NEGATIVE;
|
|
27
40
|
|
|
28
41
|
const labelId = useId();
|
|
29
42
|
|
|
@@ -35,14 +48,16 @@ export const Field = ({ id, label, hint, error, className, children }: FieldProp
|
|
|
35
48
|
return (
|
|
36
49
|
<FieldLabelIdContextProvider value={labelId}>
|
|
37
50
|
<InputIdContextProvider value={inputId}>
|
|
38
|
-
<InputDescribedByProvider value={
|
|
51
|
+
<InputDescribedByProvider value={message ? descriptionId : undefined}>
|
|
39
52
|
<InputInvalidProvider value={hasError}>
|
|
40
53
|
<div
|
|
41
54
|
className={classNames(
|
|
42
55
|
'form-group d-block',
|
|
43
56
|
{
|
|
57
|
+
'has-success': sentiment === Sentiment.POSITIVE,
|
|
58
|
+
'has-warning': sentiment === Sentiment.WARNING,
|
|
44
59
|
'has-error': hasError,
|
|
45
|
-
'has-info':
|
|
60
|
+
'has-info': sentiment === Sentiment.NEUTRAL,
|
|
46
61
|
},
|
|
47
62
|
className,
|
|
48
63
|
)}
|
|
@@ -51,14 +66,9 @@ export const Field = ({ id, label, hint, error, className, children }: FieldProp
|
|
|
51
66
|
{label}
|
|
52
67
|
{children}
|
|
53
68
|
</Label>
|
|
54
|
-
{
|
|
55
|
-
<InlineAlert type={
|
|
56
|
-
{
|
|
57
|
-
</InlineAlert>
|
|
58
|
-
)}
|
|
59
|
-
{hasError && (
|
|
60
|
-
<InlineAlert type={Sentiment.NEGATIVE} id={descriptionId}>
|
|
61
|
-
{error}
|
|
69
|
+
{message && (
|
|
70
|
+
<InlineAlert type={sentiment} id={descriptionId}>
|
|
71
|
+
{message}
|
|
62
72
|
</InlineAlert>
|
|
63
73
|
)}
|
|
64
74
|
</div>
|
|
@@ -51,7 +51,7 @@ export const Basic = () => {
|
|
|
51
51
|
|
|
52
52
|
return (
|
|
53
53
|
<>
|
|
54
|
-
{/*eslint-disable-next-line react/no-adjacent-inline-elements */}
|
|
54
|
+
{/* eslint-disable-next-line react/no-adjacent-inline-elements */}
|
|
55
55
|
<p>
|
|
56
56
|
The styling for the input (the coloured border) and the visibility of the inline alert is
|
|
57
57
|
controlled through the use of <code>has-***</code> classes which are applied to the{' '}
|
|
@@ -60,7 +60,11 @@ export const Basic = () => {
|
|
|
60
60
|
element. The available classes are <code>has-error</code>, <code>has-info</code>,{' '}
|
|
61
61
|
<code>has-warning</code> and <code>has-success</code>.
|
|
62
62
|
</p>
|
|
63
|
-
<p>
|
|
63
|
+
<p>
|
|
64
|
+
Where possible consumers should use{' '}
|
|
65
|
+
<a href="https://storybook.wise.design/?path=/story/field--basic">Field</a> instead of doing
|
|
66
|
+
this manually.
|
|
67
|
+
</p>
|
|
64
68
|
<div className={`form-group ${typeClass}`}>
|
|
65
69
|
<label className="control-label" htmlFor="id0">
|
|
66
70
|
Toggleable
|
|
@@ -73,14 +77,18 @@ export const Basic = () => {
|
|
|
73
77
|
Negative
|
|
74
78
|
</label>
|
|
75
79
|
<Input id="id1" value="Neptune is cool" />
|
|
76
|
-
<InlineAlert type="
|
|
80
|
+
<InlineAlert type="negative">{message}</InlineAlert>
|
|
77
81
|
</div>
|
|
78
|
-
<div className="form-group has-
|
|
82
|
+
<div className="form-group has-success">
|
|
79
83
|
<label className="control-label" htmlFor="id2">
|
|
80
84
|
Positive
|
|
81
85
|
</label>
|
|
82
86
|
<Input id="id2" value="Neptune is cool" />
|
|
83
|
-
<InlineAlert type="positive">
|
|
87
|
+
<InlineAlert type="positive">
|
|
88
|
+
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt
|
|
89
|
+
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
|
|
90
|
+
ullamco laboris nisi ut aliquip ex ea commodo consequat.
|
|
91
|
+
</InlineAlert>
|
|
84
92
|
</div>
|
|
85
93
|
<div className="form-group has-neutral">
|
|
86
94
|
<label className="control-label" htmlFor="id3">
|