@transferwise/components 0.0.0-experimental-0400704 → 0.0.0-experimental-c09cb11

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.
Files changed (121) hide show
  1. package/build/dateLookup/dateTrigger/DateTrigger.js +8 -4
  2. package/build/dateLookup/dateTrigger/DateTrigger.js.map +1 -1
  3. package/build/dateLookup/dateTrigger/DateTrigger.mjs +8 -4
  4. package/build/dateLookup/dateTrigger/DateTrigger.mjs.map +1 -1
  5. package/build/field/Field.js +34 -7
  6. package/build/field/Field.js.map +1 -1
  7. package/build/field/Field.mjs +34 -7
  8. package/build/field/Field.mjs.map +1 -1
  9. package/build/i18n/en.json +1 -0
  10. package/build/i18n/en.json.js +1 -0
  11. package/build/i18n/en.json.js.map +1 -1
  12. package/build/i18n/en.json.mjs +1 -0
  13. package/build/i18n/en.json.mjs.map +1 -1
  14. package/build/index.js +2 -2
  15. package/build/index.mjs +1 -1
  16. package/build/inlineAlert/InlineAlert.js +13 -6
  17. package/build/inlineAlert/InlineAlert.js.map +1 -1
  18. package/build/inlineAlert/InlineAlert.mjs +13 -6
  19. package/build/inlineAlert/InlineAlert.mjs.map +1 -1
  20. package/build/label/Label.js +49 -5
  21. package/build/label/Label.js.map +1 -1
  22. package/build/label/Label.messages.js +12 -0
  23. package/build/label/Label.messages.js.map +1 -0
  24. package/build/label/Label.messages.mjs +10 -0
  25. package/build/label/Label.messages.mjs.map +1 -0
  26. package/build/label/Label.mjs +50 -6
  27. package/build/label/Label.mjs.map +1 -1
  28. package/build/main.css +4 -8
  29. package/build/styles/dateLookup/dateTrigger/DateTrigger.css +0 -8
  30. package/build/styles/field/Field.css +4 -0
  31. package/build/styles/main.css +4 -8
  32. package/build/tabs/Tab.js +38 -13
  33. package/build/tabs/Tab.js.map +1 -1
  34. package/build/tabs/Tab.mjs +34 -13
  35. package/build/tabs/Tab.mjs.map +1 -1
  36. package/build/tabs/TabList.js +11 -3
  37. package/build/tabs/TabList.js.map +1 -1
  38. package/build/tabs/TabList.mjs +7 -3
  39. package/build/tabs/TabList.mjs.map +1 -1
  40. package/build/tabs/TabPanel.js +16 -3
  41. package/build/tabs/TabPanel.js.map +1 -1
  42. package/build/tabs/TabPanel.mjs +12 -3
  43. package/build/tabs/TabPanel.mjs.map +1 -1
  44. package/build/tabs/Tabs.js +48 -24
  45. package/build/tabs/Tabs.js.map +1 -1
  46. package/build/tabs/Tabs.mjs +47 -24
  47. package/build/tabs/Tabs.mjs.map +1 -1
  48. package/build/tabs/utils.js +1 -0
  49. package/build/tabs/utils.js.map +1 -1
  50. package/build/tabs/utils.mjs +1 -0
  51. package/build/tabs/utils.mjs.map +1 -1
  52. package/build/types/dateLookup/dateTrigger/DateTrigger.d.ts.map +1 -1
  53. package/build/types/field/Field.d.ts +4 -2
  54. package/build/types/field/Field.d.ts.map +1 -1
  55. package/build/types/index.d.ts +1 -2
  56. package/build/types/index.d.ts.map +1 -1
  57. package/build/types/inlineAlert/InlineAlert.d.ts +9 -0
  58. package/build/types/inlineAlert/InlineAlert.d.ts.map +1 -1
  59. package/build/types/label/Label.d.ts +39 -3
  60. package/build/types/label/Label.d.ts.map +1 -1
  61. package/build/types/label/Label.messages.d.ts +8 -0
  62. package/build/types/label/Label.messages.d.ts.map +1 -0
  63. package/build/types/label/index.d.ts +3 -0
  64. package/build/types/label/index.d.ts.map +1 -0
  65. package/build/types/tabs/Tab.d.ts +1 -12
  66. package/build/types/tabs/Tab.d.ts.map +1 -1
  67. package/build/types/tabs/TabList.d.ts +8 -3
  68. package/build/types/tabs/TabList.d.ts.map +1 -1
  69. package/build/types/tabs/TabPanel.d.ts +14 -6
  70. package/build/types/tabs/TabPanel.d.ts.map +1 -1
  71. package/build/types/tabs/Tabs.d.ts +30 -83
  72. package/build/types/tabs/Tabs.d.ts.map +1 -1
  73. package/build/types/tabs/index.d.ts +1 -2
  74. package/build/types/tabs/index.d.ts.map +1 -1
  75. package/build/types/tabs/utils.d.ts +7 -12
  76. package/build/types/tabs/utils.d.ts.map +1 -1
  77. package/package.json +4 -4
  78. package/src/dateInput/DateInput.tests.story.tsx +6 -41
  79. package/src/dateLookup/DateLookup.rtl.spec.tsx +1 -1
  80. package/src/dateLookup/dateTrigger/DateTrigger.css +0 -8
  81. package/src/dateLookup/dateTrigger/DateTrigger.less +0 -8
  82. package/src/dateLookup/dateTrigger/DateTrigger.spec.js +1 -1
  83. package/src/dateLookup/dateTrigger/DateTrigger.tsx +9 -4
  84. package/src/field/Field.css +4 -0
  85. package/src/field/Field.less +5 -0
  86. package/src/field/Field.spec.tsx +41 -5
  87. package/src/field/Field.story.tsx +105 -7
  88. package/src/field/Field.tsx +33 -9
  89. package/src/i18n/en.json +1 -0
  90. package/src/index.ts +1 -2
  91. package/src/inlineAlert/InlineAlert.story.tsx +7 -72
  92. package/src/inlineAlert/InlineAlert.tsx +14 -3
  93. package/src/inputWithDisplayFormat/InputWithDisplayFormat.story.js +5 -10
  94. package/src/inputs/InputGroup.spec.tsx +1 -1
  95. package/src/inputs/SearchInput.spec.tsx +1 -1
  96. package/src/inputs/SelectInput.spec.tsx +1 -1
  97. package/src/label/Label.messages.tsx +8 -0
  98. package/src/label/Label.spec.tsx +42 -7
  99. package/src/label/Label.story.tsx +30 -25
  100. package/src/label/Label.tsx +66 -8
  101. package/src/label/index.ts +2 -0
  102. package/src/main.css +4 -8
  103. package/src/main.less +1 -0
  104. package/src/moneyInput/MoneyInput.story.tsx +11 -11
  105. package/src/radioGroup/RadioGroup.rtl.spec.tsx +1 -1
  106. package/src/select/Select.rtl.spec.tsx +1 -1
  107. package/src/switch/Switch.spec.tsx +1 -1
  108. package/src/switch/Switch.story.tsx +19 -21
  109. package/src/tabs/Tab.js +71 -0
  110. package/src/tabs/TabList.js +15 -0
  111. package/src/tabs/TabPanel.js +20 -0
  112. package/src/tabs/{Tabs.tsx → Tabs.js} +74 -111
  113. package/src/tabs/{Tabs.story.tsx → Tabs.story.js} +1 -1
  114. package/src/tabs/index.js +1 -0
  115. package/src/tabs/{utils.ts → utils.js} +9 -15
  116. package/src/tabs/{utils.spec.ts → utils.spec.js} +21 -24
  117. package/src/field/Field.tests.story.tsx +0 -33
  118. package/src/tabs/Tab.tsx +0 -72
  119. package/src/tabs/TabList.tsx +0 -11
  120. package/src/tabs/TabPanel.tsx +0 -14
  121. package/src/tabs/index.ts +0 -2
@@ -9,16 +9,18 @@ import {
9
9
  InputIdContextProvider,
10
10
  InputInvalidProvider,
11
11
  } from '../inputs/contexts';
12
- import { Label } from '../label/Label';
12
+ import { Label } from '../label';
13
13
 
14
14
  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
  /** Should be specified unless the wrapped control has its own labeling mechanism, e.g. `Checkbox`. */
18
18
  label?: React.ReactNode;
19
- /** @deprecated use `message` and `type={Sentiment.NEUTRAL}` prop instead */
19
+ required?: boolean;
20
+ /** @deprecated use `description` prop instead */
20
21
  hint?: React.ReactNode;
21
22
  message?: React.ReactNode;
23
+ description?: React.ReactNode;
22
24
  /** @deprecated use `message` and `type={Sentiment.NEGATIVE}` prop instead */
23
25
  error?: React.ReactNode;
24
26
  sentiment?: `${Sentiment.NEGATIVE | Sentiment.NEUTRAL | Sentiment.POSITIVE | Sentiment.WARNING}`;
@@ -29,14 +31,17 @@ export type FieldProps = {
29
31
  export const Field = ({
30
32
  id,
31
33
  label,
34
+ required = false,
32
35
  message: propMessage,
36
+ hint,
37
+ description = hint,
33
38
  sentiment: propType = Sentiment.NEUTRAL,
34
39
  className,
35
40
  children,
36
41
  ...props
37
42
  }: FieldProps) => {
38
43
  const sentiment = props.error ? Sentiment.NEGATIVE : propType;
39
- const message = props.error || props.hint || propMessage;
44
+ const message = propMessage || props.error;
40
45
  const hasError = sentiment === Sentiment.NEGATIVE;
41
46
 
42
47
  const labelId = useId();
@@ -44,16 +49,32 @@ export const Field = ({
44
49
  const fallbackInputId = useId();
45
50
  const inputId = id !== null ? (id ?? fallbackInputId) : undefined;
46
51
 
52
+ const messageId = useId();
47
53
  const descriptionId = useId();
48
54
 
55
+ /**
56
+ * form control can have multiple messages to describe it,
57
+ * e.g the description underneath the label and inline alert
58
+ */
59
+ function ariaDescribedbyByIds() {
60
+ const messageIds = [];
61
+ if (description) {
62
+ messageIds.push(descriptionId);
63
+ }
64
+ if (message) {
65
+ messageIds.push(messageId);
66
+ }
67
+ return messageIds.length > 0 ? messageIds.join(' ') : undefined;
68
+ }
69
+
49
70
  return (
50
71
  <FieldLabelIdContextProvider value={labelId}>
51
72
  <InputIdContextProvider value={inputId}>
52
- <InputDescribedByProvider value={message ? descriptionId : undefined}>
73
+ <InputDescribedByProvider value={ariaDescribedbyByIds()}>
53
74
  <InputInvalidProvider value={hasError}>
54
75
  <div
55
76
  className={clsx(
56
- 'form-group d-block',
77
+ 'np-field form-group d-block',
57
78
  {
58
79
  'has-success': sentiment === Sentiment.POSITIVE,
59
80
  'has-warning': sentiment === Sentiment.WARNING,
@@ -64,16 +85,19 @@ export const Field = ({
64
85
  )}
65
86
  >
66
87
  {label != null ? (
67
- <Label id={labelId} htmlFor={inputId}>
68
- {label}
69
- {children}
88
+ <Label>
89
+ <Label.Text id={labelId} htmlFor={inputId} required={required}>
90
+ {label}
91
+ </Label.Text>
92
+ <Label.Description id={descriptionId}>{description}</Label.Description>
93
+ <div className="np-field-control">{children}</div>
70
94
  </Label>
71
95
  ) : (
72
96
  children
73
97
  )}
74
98
 
75
99
  {message && (
76
- <InlineAlert type={sentiment} id={descriptionId}>
100
+ <InlineAlert type={sentiment} id={messageId}>
77
101
  {message}
78
102
  </InlineAlert>
79
103
  )}
package/src/i18n/en.json CHANGED
@@ -18,6 +18,7 @@
18
18
  "neptune.DateLookup.year": "year",
19
19
  "neptune.FlowNavigation.back": "back to previous step",
20
20
  "neptune.Info.ariaLabel": "More information",
21
+ "neptune.Label.optional": "(Optional)",
21
22
  "neptune.Link.opensInNewTab": "(opens in new tab)",
22
23
  "neptune.MoneyInput.Select.placeholder": "Select an option...",
23
24
  "neptune.PhoneNumberInput.SelectInput.placeholder": "Select an option...",
package/src/index.ts CHANGED
@@ -44,7 +44,7 @@ export type {
44
44
  } from './inputs/SelectInput';
45
45
  export type { TextAreaProps } from './inputs/TextArea';
46
46
  export type { InstructionsListProps } from './instructionsList';
47
- export type { LabelProps } from './label/Label';
47
+ export type { LabelProps, LabelOptionalProps, LabelDescriptionProps } from './label/Label';
48
48
  export type { LoaderProps } from './loader';
49
49
  export type { MarkdownProps } from './markdown';
50
50
  export type { ModalProps } from './modal';
@@ -75,7 +75,6 @@ export type { StickyProps } from './sticky';
75
75
  export type { SummaryProps } from './summary';
76
76
  export type { SwitchProps } from './switch';
77
77
  export type { SwitchOptionProps } from './switchOption';
78
- export type { TabItem, TabsProps } from './tabs';
79
78
  export type { TextareaWithDisplayFormatProps } from './textareaWithDisplayFormat';
80
79
  export type { TooltipProps } from './tooltip';
81
80
  export type { TypeaheadOption, TypeaheadProps } from './typeahead';
@@ -2,9 +2,9 @@ import { select, text } from '@storybook/addon-knobs';
2
2
  import { Meta } from '@storybook/react';
3
3
 
4
4
  import { Sentiment } from '../common';
5
- import { Input } from '../inputs/Input';
6
5
 
7
6
  import InlineAlert, { InlineAlertProps } from './InlineAlert';
7
+ import { lorem40 } from '../test-utils';
8
8
 
9
9
  export default {
10
10
  component: InlineAlert,
@@ -29,81 +29,16 @@ export const Basic = () => {
29
29
 
30
30
  const message = text('message', 'Please enter a password over 5 characters');
31
31
 
32
- let typeClass = '';
33
- switch (type) {
34
- case Sentiment.ERROR:
35
- case Sentiment.NEGATIVE:
36
- typeClass = 'has-error';
37
- break;
38
- case Sentiment.SUCCESS:
39
- case Sentiment.POSITIVE:
40
- typeClass = 'has-success';
41
- break;
42
- case Sentiment.INFO:
43
- case Sentiment.NEUTRAL:
44
- typeClass = 'has-info';
45
- break;
46
- case Sentiment.WARNING:
47
- typeClass = 'has-warning';
48
- break;
49
- case Sentiment.PENDING:
50
- }
51
-
52
32
  return (
53
33
  <>
54
- {/* eslint-disable-next-line react/no-adjacent-inline-elements */}
55
- <p>
56
- The styling for the input (the coloured border) and the visibility of the inline alert is
57
- controlled through the use of <code>has-***</code> classes which are applied to the{' '}
58
- <code>form-group</code> element. For example, to display an inline alert of type error, you
59
- must also apply the class <code>has-error</code> to the parent <code>form-group</code>{' '}
60
- element. The available classes are <code>has-error</code>, <code>has-info</code>,{' '}
61
- <code>has-warning</code> and <code>has-success</code>.
62
- </p>
63
34
  <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.
35
+ Avoid using <code>InlineAlert</code> directly (unless for some custom use cases), for form
36
+ control validation, info messaging please use <code>Field</code> component
37
+ <pre>{'<Field sentiment={..} message={..}>'}</pre>
67
38
  </p>
68
- <div className={`form-group ${typeClass}`}>
69
- <label className="control-label" htmlFor="id0">
70
- Toggleable
71
- </label>
72
- <Input id="id0" value="Neptune is cool" />
73
- <InlineAlert type={type}>{message}</InlineAlert>
74
- </div>
75
- <div className="form-group has-error">
76
- <label className="control-label" htmlFor="id1">
77
- Negative
78
- </label>
79
- <Input id="id1" value="Neptune is cool" />
80
- <InlineAlert type="negative">{message}</InlineAlert>
81
- </div>
82
- <div className="form-group has-success">
83
- <label className="control-label" htmlFor="id2">
84
- Positive
85
- </label>
86
- <Input id="id2" value="Neptune is cool" />
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>
92
- </div>
93
- <div className="form-group has-neutral">
94
- <label className="control-label" htmlFor="id3">
95
- Neutral
96
- </label>
97
- <Input id="id3" value="Neptune is cool" />
98
- <InlineAlert type="neutral">{message}</InlineAlert>
99
- </div>
100
- <div className="form-group">
101
- <label className="control-label" htmlFor="id4">
102
- No has-* class
103
- </label>
104
- <Input id="id4" value="Neptune is cool" />
105
- <InlineAlert type="negative">{message}</InlineAlert>
106
- </div>
39
+ <InlineAlert type={type}>{message}</InlineAlert>
40
+ <InlineAlert type="negative">{message}</InlineAlert>
41
+ <InlineAlert type="positive">{lorem40}</InlineAlert>
107
42
  </>
108
43
  );
109
44
  };
@@ -3,6 +3,7 @@ import { ReactNode } from 'react';
3
3
 
4
4
  import { Sentiment, Size } from '../common';
5
5
  import StatusIcon from '../statusIcon';
6
+ import Body from '../body';
6
7
 
7
8
  export interface InlineAlertProps {
8
9
  id?: string;
@@ -19,6 +20,15 @@ const iconTypes = new Set<NonNullable<InlineAlertProps['type']>>([
19
20
  Sentiment.WARNING,
20
21
  ]);
21
22
 
23
+ /**
24
+ * Avoid using `<InlineAlert>` component directly
25
+ * it's for edge cases when `<Field />` isn't suitable for some reasons.
26
+ *
27
+ * Example:
28
+ * ```
29
+ * <Field sentiment={..} message={..}>..</Field>
30
+ * ```
31
+ */
22
32
  export default function InlineAlert({
23
33
  id,
24
34
  type = 'neutral',
@@ -26,17 +36,18 @@ export default function InlineAlert({
26
36
  children,
27
37
  }: InlineAlertProps) {
28
38
  return (
29
- <div
39
+ <Body
30
40
  role="alert"
31
41
  id={id}
32
42
  className={clsx(
33
43
  'alert alert-detach',
34
44
  `alert-${type === Sentiment.NEGATIVE || type === Sentiment.ERROR ? 'danger' : type}`,
45
+ 'd-flex',
35
46
  className,
36
47
  )}
37
48
  >
38
49
  {iconTypes.has(type) && <StatusIcon sentiment={type} size={Size.SMALL} />}
39
- <div className="np-text-body-default">{children}</div>
40
- </div>
50
+ {children}
51
+ </Body>
41
52
  );
42
53
  }
@@ -2,6 +2,7 @@ import { text } from '@storybook/addon-knobs';
2
2
  import { userEvent, within } from '@storybook/test';
3
3
 
4
4
  import InputWithDisplayFormat from '.';
5
+ import { Field } from '../field/Field';
5
6
 
6
7
  export default {
7
8
  component: InputWithDisplayFormat,
@@ -13,10 +14,7 @@ export const Basic = () => {
13
14
  const displayPattern = text('DisplayPattern', '**-**-**');
14
15
 
15
16
  return (
16
- <>
17
- <label id="template" htmlFor="Basic">
18
- Display pattern is controlled via knobs
19
- </label>
17
+ <Field label="Display pattern is controlled via knobs" id="Basic">
20
18
  <InputWithDisplayFormat
21
19
  id="Basic"
22
20
  placeholder={placeholder}
@@ -26,7 +24,7 @@ export const Basic = () => {
26
24
  onBlur={(v) => console.log(v)}
27
25
  onFocus={(v) => console.log(v)}
28
26
  />
29
- </>
27
+ </Field>
30
28
  );
31
29
  };
32
30
 
@@ -37,10 +35,7 @@ const Template = (args) => {
37
35
  const id = label.replaceAll(' ', '-').toLowerCase();
38
36
 
39
37
  return (
40
- <>
41
- <label id="template" htmlFor={id}>
42
- {label}
43
- </label>
38
+ <Field label={label} id={id}>
44
39
  <InputWithDisplayFormat
45
40
  id={id}
46
41
  placeholder={placeholder}
@@ -50,7 +45,7 @@ const Template = (args) => {
50
45
  onBlur={(v) => console.log(v)}
51
46
  onFocus={(v) => console.log(v)}
52
47
  />
53
- </>
48
+ </Field>
54
49
  );
55
50
  };
56
51
 
@@ -26,6 +26,6 @@ describe('InputGroup', () => {
26
26
  </InputGroup>
27
27
  </Field>,
28
28
  );
29
- expect(screen.getByLabelText('Search…')).toHaveRole('textbox');
29
+ expect(screen.getByLabelText(/Search…/)).toHaveRole('textbox');
30
30
  });
31
31
  });
@@ -11,6 +11,6 @@ describe('SearchInput', () => {
11
11
  <SearchInput />
12
12
  </Field>,
13
13
  );
14
- expect(screen.getByLabelText('Search…')).toHaveRole('searchbox');
14
+ expect(screen.getByLabelText(/Search…/)).toHaveRole('searchbox');
15
15
  });
16
16
  });
@@ -214,6 +214,6 @@ describe('SelectInput', () => {
214
214
  <SelectInput items={[{ type: 'option', value: 'USD' }]} value="USD" />
215
215
  </Field>,
216
216
  );
217
- expect(screen.getByLabelText('Currency')).toHaveAttribute('aria-haspopup');
217
+ expect(screen.getByLabelText(/Currency/)).toHaveAttribute('aria-haspopup');
218
218
  });
219
219
  });
@@ -0,0 +1,8 @@
1
+ import { defineMessages } from 'react-intl';
2
+
3
+ export default defineMessages({
4
+ optionalLabel: {
5
+ id: 'neptune.Label.optional',
6
+ defaultMessage: '(Optional)',
7
+ },
8
+ });
@@ -1,26 +1,61 @@
1
1
  import { Input } from '../inputs/Input';
2
- import { render, screen } from '../test-utils';
2
+ import { lorem10, render, screen } from '../test-utils';
3
3
  import { Label } from './Label';
4
4
 
5
5
  describe('Label', () => {
6
6
  it('renders string labels', () => {
7
7
  render(
8
8
  <Label>
9
- Phone number
10
- <Input readOnly />
9
+ <Label.Text htmlFor="test" required>
10
+ Phone number
11
+ </Label.Text>
12
+ <Input id="test" readOnly />
11
13
  </Label>,
12
14
  );
13
15
 
14
16
  expect(screen.getByLabelText('Phone number')).toBeInTheDocument();
15
17
  });
16
- it('renders node type labels', () => {
18
+
19
+ it('renders string labels with (Optional) suffix', () => {
17
20
  render(
18
21
  <Label>
19
- <span>Phone number</span>
20
- <Input readOnly />
22
+ <Label.Text htmlFor="test">Phone number</Label.Text>
23
+ <Input id="test" readOnly />
21
24
  </Label>,
22
25
  );
23
26
 
24
- expect(screen.getByLabelText('Phone number')).toBeInTheDocument();
27
+ expect(screen.getByLabelText(/Phone number/)).toBeInTheDocument();
28
+ expect(screen.getByLabelText(/(Optional)/)).toBeInTheDocument();
29
+ });
30
+
31
+ it('renders description', () => {
32
+ const descriptionId = 'desc-test';
33
+ render(
34
+ <Label>
35
+ <Label.Text htmlFor="test">Phone number</Label.Text>
36
+ <Label.Description id={descriptionId}>{lorem10}</Label.Description>
37
+ <Input id="test" readOnly aria-describedby={descriptionId} />
38
+ </Label>,
39
+ );
40
+
41
+ expect(screen.getByLabelText(/Phone number/)).toBeInTheDocument();
42
+ expect(screen.getByText(lorem10)).toBeInTheDocument();
43
+ expect(screen.getByText(lorem10)).toHaveAttribute('id', descriptionId);
44
+ });
45
+
46
+ it('connects label with form control via `id` + `aria-labelledby`', () => {
47
+ const labelId = 'label-id';
48
+ render(
49
+ <Label>
50
+ <Label.Text id={labelId} required>
51
+ Phone number
52
+ </Label.Text>
53
+ <div role="group" aria-labelledby={labelId}>
54
+ Custom complex component
55
+ </div>
56
+ </Label>,
57
+ );
58
+
59
+ expect(screen.getByLabelText('Phone number')).toHaveAttribute('aria-labelledby', labelId);
25
60
  });
26
61
  });
@@ -1,37 +1,42 @@
1
- import { useState } from 'react';
2
-
3
- import Info from '../info/Info';
4
- import { Input } from '../inputs/Input';
1
+ import Info from '../info';
5
2
  import { Label } from './Label';
6
3
 
7
4
  export default {
8
5
  component: Label,
9
6
  title: 'Label',
7
+ tags: ['autodocs'],
10
8
  };
11
9
 
12
10
  export const Basic = () => {
13
- const [value, setValue] = useState<string | undefined>('This is some text');
14
11
  return (
15
- <Label>
16
- Phone number
17
- <Input value={value} id="input" onChange={({ target }) => setValue(target.value)} />
18
- </Label>
19
- );
20
- };
12
+ <>
13
+ <p>
14
+ Avoid using <code>Label</code> component directly (unless for some custom use cases), for
15
+ form control labels and descriptions / info messages, please use <code>Field</code>{' '}
16
+ component
17
+ <pre>{'<Field label={..} description={..} required={..}>'}</pre>
18
+ </p>
21
19
 
22
- export const WithInfo = () => {
23
- const [value, setValue] = useState<string | undefined>('This is some text');
24
- return (
25
- <Label>
26
- <span className="d-flex">
27
- Phone number{' '}
28
- <Info
29
- content="This is some help in popover"
30
- aria-label="The aria label"
31
- className="m-l-1"
32
- />
33
- </span>
34
- <Input value={value} id="input" onChange={({ target }) => setValue(target.value)} />
35
- </Label>
20
+ <Label className="m-b-2">
21
+ <Label.Text required>Text Input</Label.Text>
22
+ </Label>
23
+
24
+ <Label className="m-b-2">
25
+ <Label.Text>Text Input</Label.Text>
26
+ </Label>
27
+
28
+ <Label className="m-b-2">
29
+ <Label.Text>Text Input with Description</Label.Text>
30
+ <Label.Description>This a field Description</Label.Description>
31
+ </Label>
32
+
33
+ <Label className="m-b-2">
34
+ <Label.Text>
35
+ Text Input with Description{' '}
36
+ <Info content="This is some help in popover" aria-label="The aria label" />{' '}
37
+ </Label.Text>
38
+ <Label.Description>This a field Description</Label.Description>
39
+ </Label>
40
+ </>
36
41
  );
37
42
  };
@@ -1,20 +1,78 @@
1
1
  import { clsx } from 'clsx';
2
+ import messages from './Label.messages';
3
+ import { useIntl } from 'react-intl';
4
+ import Body from '../body';
5
+ import { CommonProps } from '../common';
6
+ import { PropsWithChildren } from 'react';
2
7
 
3
8
  export type LabelProps = {
4
- id?: string;
5
- htmlFor?: string;
6
9
  className?: string;
7
10
  children?: React.ReactNode;
8
11
  };
9
12
 
10
- export const Label = ({ id, htmlFor, className, children }: LabelProps) => {
13
+ /**
14
+ * Avoid using `<Label>` component directly
15
+ * it's for edge cases when `<Field />` isn't suitable for some reasons.
16
+ *
17
+ * Example:
18
+ * ```
19
+ * <Field label={..} description={..} required={..}>..</Field>
20
+ * ```
21
+ */
22
+ const Label = ({ className, children }: LabelProps) => {
23
+ return <div className={clsx('np-label d-flex flex-column', className)}>{children}</div>;
24
+ };
25
+
26
+ export type LabelOptionalProps = PropsWithChildren<CommonProps> & {
27
+ id?: string;
28
+ required?: boolean;
29
+ /**
30
+ * Connect label with form control via `htmlFor` prop, or `id` + `aria-labelledby`
31
+ */
32
+ htmlFor?: string | undefined;
33
+ };
34
+
35
+ /**
36
+ * Avoid wrapping form control with `<Label.Text >`, always connect label and control via `htmlFor` + `id` props
37
+ *
38
+ * Example:
39
+ * ```
40
+ * <Label>
41
+ * <Label.Text htmlFor="foo">Money input with label</Label.Text>
42
+ * <CustomComponent id="foo" />
43
+ * </Label>
44
+ * ```
45
+ */
46
+ // eslint-disable-next-line functional/immutable-data
47
+ Label.Text = function Text({
48
+ id,
49
+ children,
50
+ required = false,
51
+ htmlFor,
52
+ className,
53
+ }: LabelOptionalProps) {
54
+ const { formatMessage } = useIntl();
11
55
  return (
12
- <label
13
- id={id}
14
- htmlFor={htmlFor}
15
- className={clsx('control-label d-flex flex-column gap-y-1 m-b-0', className)}
16
- >
56
+ <label id={id} htmlFor={htmlFor} className="np-text-body-default-bold text-primary m-b-0">
17
57
  {children}
58
+ {required ? null : (
59
+ <Body as="span" className={clsx('text-secondary', 'm-l-1', className)}>
60
+ {formatMessage(messages.optionalLabel)}
61
+ </Body>
62
+ )}
18
63
  </label>
19
64
  );
20
65
  };
66
+
67
+ export type LabelDescriptionProps = PropsWithChildren<CommonProps> & { id?: string };
68
+
69
+ // eslint-disable-next-line functional/immutable-data
70
+ Label.Description = function Description({ id, children, className }: LabelDescriptionProps) {
71
+ return children ? (
72
+ <Body id={id} className={clsx('text-secondary', className)}>
73
+ {children}
74
+ </Body>
75
+ ) : null;
76
+ };
77
+
78
+ export { Label };
@@ -0,0 +1,2 @@
1
+ export { Label } from './Label';
2
+ export type { LabelProps, LabelOptionalProps, LabelDescriptionProps } from './Label';
package/src/main.css CHANGED
@@ -1719,18 +1719,10 @@ button.np-option {
1719
1719
  white-space: nowrap;
1720
1720
  width: 100%;
1721
1721
  }
1722
- .np-date-trigger .control-label {
1723
- font-weight: 400;
1724
- font-weight: var(--font-weight-regular);
1725
- }
1726
1722
  .np-theme-personal .np-date-trigger {
1727
1723
  padding-left: 16px;
1728
1724
  padding-left: var(--size-16);
1729
1725
  }
1730
- .np-theme-personal .np-date-trigger .control-label + span {
1731
- font-weight: 400;
1732
- font-weight: var(--font-weight-regular);
1733
- }
1734
1726
  .clear-btn {
1735
1727
  transition: color 0.15s ease-in-out;
1736
1728
  color: #c9cbce;
@@ -2385,6 +2377,10 @@ html:not([dir="rtl"]) .np-flow-navigation--sm .np-flow-navigation__stepper {
2385
2377
  border-radius: 9999px !important;
2386
2378
  border-radius: var(--radius-full) !important;
2387
2379
  }
2380
+ .np-field-control {
2381
+ margin-top: 4px;
2382
+ margin-top: var(--size-4);
2383
+ }
2388
2384
  .np-input-group {
2389
2385
  display: inline-grid;
2390
2386
  width: 100%;
package/src/main.less CHANGED
@@ -32,6 +32,7 @@
32
32
  @import "./image/Image.less";
33
33
  @import "./info/Info.less";
34
34
  @import "./inputs/Input.less";
35
+ @import "./field/Field.less";
35
36
  @import "./inputs/InputGroup.less";
36
37
  @import "./inputs/SelectInput.less";
37
38
  @import "./inputs/TextArea.less";