@moda/om 21.6.10 → 21.6.11

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.
@@ -0,0 +1,6 @@
1
+ type FormControlContextValue = {
2
+ displaysError: boolean;
3
+ };
4
+ export declare const FormControlContext: import("react").Context<FormControlContextValue>;
5
+ export declare const useFormControlContext: () => FormControlContextValue;
6
+ export {};
@@ -1,3 +1,4 @@
1
1
  import { Field } from './Field';
2
2
  export { Field as Input };
3
3
  export * from './Field';
4
+ export * from './FormControlContext';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moda/om",
3
- "version": "21.6.10",
3
+ "version": "21.6.11",
4
4
  "description": "Moda Operandi design system",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.esm.js",
@@ -1,8 +1,9 @@
1
- import React, { JSX } from 'react';
1
+ import React, { JSX, useMemo } from 'react';
2
2
  import classNames from 'classnames';
3
3
  import WarningIcon from '@moda/icons/warning-16';
4
4
  import { TextInput, TextInputProps } from '../TextInput';
5
5
  import { Select } from '../Select';
6
+ import { FormControlContext } from './FormControlContext';
6
7
 
7
8
  import './Field.scss';
8
9
 
@@ -15,50 +16,54 @@ export const Field = React.forwardRef(
15
16
  { className, children, error, label, placeholder, ...rest }: FieldProps,
16
17
  ref: React.Ref<HTMLInputElement>
17
18
  ) => {
19
+ const fieldContextValue = useMemo(() => ({ displaysError: Boolean(error) }), [error]);
20
+
18
21
  return (
19
- <label className={classNames('Field', className)}>
20
- {label && <span className='Field__label'>{label}</span>}
22
+ <FormControlContext.Provider value={fieldContextValue}>
23
+ <label className={classNames('Field', className)}>
24
+ {label && <span className='Field__label'>{label}</span>}
21
25
 
22
- {/* TODO: Codify some kind of IconWrapper component */}
23
- <span className='Field__context'>
24
- {children ? (
25
- <>
26
- {React.cloneElement(children, {
27
- ref,
28
- error,
29
- placeholder,
30
- label,
31
- shiftIconLeftwards: error && children.type === Select,
32
- ...rest,
33
- ...children.props
34
- })}
26
+ {/* TODO: Codify some kind of IconWrapper component */}
27
+ <span className='Field__context'>
28
+ {children ? (
29
+ <>
30
+ {React.cloneElement(children, {
31
+ ref,
32
+ error,
33
+ placeholder,
34
+ label,
35
+ shiftIconLeftwards: error && children.type === Select,
36
+ ...rest,
37
+ ...children.props
38
+ })}
35
39
 
36
- {error && (
37
- <span className='Field__icon'>
38
- <WarningIcon />
39
- </span>
40
- )}
41
- </>
42
- ) : (
43
- <>
44
- <TextInput
45
- ref={ref}
46
- placeholder={placeholder}
47
- label={label}
48
- error={error}
49
- {...rest}
50
- />
51
- {error && (
52
- <span className='Field__icon'>
53
- <WarningIcon />
54
- </span>
55
- )}
56
- </>
57
- )}
58
- </span>
40
+ {error && (
41
+ <span className='Field__icon'>
42
+ <WarningIcon />
43
+ </span>
44
+ )}
45
+ </>
46
+ ) : (
47
+ <>
48
+ <TextInput
49
+ ref={ref}
50
+ placeholder={placeholder}
51
+ label={label}
52
+ error={error}
53
+ {...rest}
54
+ />
55
+ {error && (
56
+ <span className='Field__icon'>
57
+ <WarningIcon />
58
+ </span>
59
+ )}
60
+ </>
61
+ )}
62
+ </span>
59
63
 
60
- {error && <span className='Field__error'>{error}</span>}
61
- </label>
64
+ {error && <span className='Field__error'>{error}</span>}
65
+ </label>
66
+ </FormControlContext.Provider>
62
67
  );
63
68
  }
64
69
  );
@@ -0,0 +1,11 @@
1
+ import { createContext, useContext } from 'react';
2
+
3
+ type FormControlContextValue = {
4
+ displaysError: boolean;
5
+ };
6
+
7
+ export const FormControlContext = createContext<FormControlContextValue>({
8
+ displaysError: false
9
+ });
10
+
11
+ export const useFormControlContext = () => useContext(FormControlContext);
@@ -2,3 +2,4 @@ import { Field } from './Field';
2
2
 
3
3
  export { Field as Input };
4
4
  export * from './Field';
5
+ export * from './FormControlContext';
@@ -5,6 +5,7 @@ import ChevronUpIcon from '@moda/icons/chevron-up-12';
5
5
  import { colors } from '@moda/tokens';
6
6
  import { Clickable } from '../Clickable';
7
7
  import { TextColor } from '../Text';
8
+ import { useFormControlContext } from '../Field/FormControlContext';
8
9
  import { SelectOptions } from './SelectOptions';
9
10
  import { SelectLabel } from './SelectLabel';
10
11
  import { useSelect } from './useSelect';
@@ -75,6 +76,7 @@ export const Select: React.FC<SelectProps> = ({
75
76
  smallMobileText = false,
76
77
  ...rest
77
78
  }) => {
79
+ const { displaysError: errorDisplayedByParent } = useFormControlContext();
78
80
  const { state, dispatch, Mode, selectRef, buttonRef } = useSelect({
79
81
  value,
80
82
  defaultValue
@@ -207,7 +209,7 @@ export const Select: React.FC<SelectProps> = ({
207
209
  )}
208
210
  </div>
209
211
 
210
- {typeof error === 'string' && (
212
+ {typeof error === 'string' && !errorDisplayedByParent && (
211
213
  <div role='status' aria-live='polite' aria-atomic='true' className='Select__live-region'>
212
214
  {error}
213
215
  </div>