@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.
- package/dist/index.cjs.js +21 -3
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +20 -4
- package/dist/index.esm.js.map +1 -1
- package/dist/src/components/Field/FormControlContext.d.ts +6 -0
- package/dist/src/components/Field/index.d.ts +1 -0
- package/package.json +1 -1
- package/src/components/Field/Field.tsx +46 -41
- package/src/components/Field/FormControlContext.tsx +11 -0
- package/src/components/Field/index.ts +1 -0
- package/src/components/Select/Select.tsx +3 -1
package/package.json
CHANGED
|
@@ -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
|
-
<
|
|
20
|
-
|
|
22
|
+
<FormControlContext.Provider value={fieldContextValue}>
|
|
23
|
+
<label className={classNames('Field', className)}>
|
|
24
|
+
{label && <span className='Field__label'>{label}</span>}
|
|
21
25
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
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
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
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
|
-
|
|
61
|
-
|
|
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);
|
|
@@ -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>
|