@instructure/ui-form-field 9.11.0 → 9.11.1

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 (61) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/es/FormField/index.js +5 -1
  3. package/es/FormFieldGroup/__new-tests__/FormFieldGroup.test.js +4 -4
  4. package/es/FormFieldGroup/index.js +18 -4
  5. package/es/FormFieldLabel/index.js +6 -6
  6. package/es/FormFieldLayout/__new-tests__/FormFieldLayout.test.js +10 -8
  7. package/es/FormFieldLayout/index.js +79 -58
  8. package/es/FormFieldLayout/props.js +1 -6
  9. package/es/FormFieldLayout/styles.js +105 -9
  10. package/es/FormFieldLayout/theme.js +56 -0
  11. package/es/FormFieldMessages/props.js +3 -2
  12. package/es/FormFieldMessages/styles.js +5 -3
  13. package/es/FormPropTypes.js +6 -0
  14. package/lib/FormField/index.js +5 -1
  15. package/lib/FormFieldGroup/__new-tests__/FormFieldGroup.test.js +4 -4
  16. package/lib/FormFieldGroup/index.js +18 -4
  17. package/lib/FormFieldLabel/index.js +6 -5
  18. package/lib/FormFieldLayout/__new-tests__/FormFieldLayout.test.js +9 -7
  19. package/lib/FormFieldLayout/index.js +77 -58
  20. package/lib/FormFieldLayout/props.js +1 -6
  21. package/lib/FormFieldLayout/styles.js +105 -9
  22. package/lib/FormFieldLayout/theme.js +62 -0
  23. package/lib/FormFieldMessages/props.js +3 -2
  24. package/lib/FormFieldMessages/styles.js +5 -3
  25. package/lib/FormPropTypes.js +6 -0
  26. package/package.json +15 -15
  27. package/src/FormField/README.md +31 -3
  28. package/src/FormField/index.tsx +3 -0
  29. package/src/FormFieldGroup/__new-tests__/FormFieldGroup.test.tsx +4 -6
  30. package/src/FormFieldGroup/index.tsx +41 -6
  31. package/src/FormFieldLabel/index.tsx +8 -3
  32. package/src/FormFieldLayout/__new-tests__/FormFieldLayout.test.tsx +6 -8
  33. package/src/FormFieldLayout/index.tsx +83 -100
  34. package/src/FormFieldLayout/props.ts +30 -7
  35. package/src/FormFieldLayout/styles.ts +124 -12
  36. package/src/FormFieldLayout/theme.ts +59 -0
  37. package/src/FormFieldMessages/props.ts +8 -2
  38. package/src/FormFieldMessages/styles.ts +5 -4
  39. package/src/FormPropTypes.ts +4 -0
  40. package/tsconfig.build.tsbuildinfo +1 -1
  41. package/types/FormField/index.d.ts.map +1 -1
  42. package/types/FormFieldGroup/index.d.ts +1 -0
  43. package/types/FormFieldGroup/index.d.ts.map +1 -1
  44. package/types/FormFieldLabel/index.d.ts +2 -2
  45. package/types/FormFieldLabel/index.d.ts.map +1 -1
  46. package/types/FormFieldLayout/index.d.ts +8 -7
  47. package/types/FormFieldLayout/index.d.ts.map +1 -1
  48. package/types/FormFieldLayout/props.d.ts +27 -3
  49. package/types/FormFieldLayout/props.d.ts.map +1 -1
  50. package/types/FormFieldLayout/styles.d.ts +4 -3
  51. package/types/FormFieldLayout/styles.d.ts.map +1 -1
  52. package/types/FormFieldLayout/theme.d.ts +10 -0
  53. package/types/FormFieldLayout/theme.d.ts.map +1 -0
  54. package/types/FormFieldMessages/index.d.ts +8 -2
  55. package/types/FormFieldMessages/index.d.ts.map +1 -1
  56. package/types/FormFieldMessages/props.d.ts +5 -0
  57. package/types/FormFieldMessages/props.d.ts.map +1 -1
  58. package/types/FormFieldMessages/styles.d.ts +2 -3
  59. package/types/FormFieldMessages/styles.d.ts.map +1 -1
  60. package/types/FormPropTypes.d.ts +3 -0
  61. package/types/FormPropTypes.d.ts.map +1 -1
@@ -22,8 +22,43 @@
22
22
  * SOFTWARE.
23
23
  */
24
24
 
25
- import type { FormFieldLayoutProps, FormFieldLayoutStyle } from './props'
25
+ import type {
26
+ FormFieldLayoutProps,
27
+ FormFieldLayoutStyle,
28
+ FormFieldStyleProps
29
+ } from './props'
30
+ import type { FormFieldLayoutTheme } from '@instructure/shared-types'
26
31
 
32
+ const generateGridLayout = (
33
+ isInlineLayout: boolean,
34
+ hasNewErrorMsgAndIsGroup: boolean,
35
+ hasVisibleLabel: boolean,
36
+ hasMessages: boolean
37
+ ) => {
38
+ if (isInlineLayout) {
39
+ if (hasNewErrorMsgAndIsGroup) {
40
+ if (hasMessages) {
41
+ return `${hasVisibleLabel ? ' "label messages"' : '. messages'}
42
+ ". controls"`
43
+ } else {
44
+ return `${hasVisibleLabel ? ' "label controls"' : '. controls'}`
45
+ }
46
+ } else {
47
+ return `${hasVisibleLabel ? ' "label controls"' : '. controls'}
48
+ ${hasMessages ? ' ". messages"' : ''}`
49
+ }
50
+ }
51
+ // stacked layout -- in this case we could use a simple `Flex`
52
+ if (hasNewErrorMsgAndIsGroup) {
53
+ return `${hasVisibleLabel ? ' "label"' : ''}
54
+ ${hasMessages ? ' "messages"' : ''}
55
+ "controls"`
56
+ } else {
57
+ return `${hasVisibleLabel ? ' "label"' : ''}
58
+ "controls"
59
+ ${hasMessages ? ' "messages"' : ''}`
60
+ }
61
+ }
27
62
  /**
28
63
  * ---
29
64
  * private: true
@@ -31,19 +66,61 @@ import type { FormFieldLayoutProps, FormFieldLayoutStyle } from './props'
31
66
  * Generates the style object from the theme and provided additional information
32
67
  * @param {Object} componentTheme The theme variable object.
33
68
  * @param {Object} props the props of the component, the style is applied to
34
- * @param {Object} state the state of the component, the style is applied to
69
+ * @param {Object} styleProps
35
70
  * @return {Object} The final style object, which will be used in the component
36
71
  */
37
72
  const generateStyle = (
38
- _componentTheme: null,
39
- props: FormFieldLayoutProps
73
+ componentTheme: FormFieldLayoutTheme,
74
+ props: FormFieldLayoutProps,
75
+ styleProps: FormFieldStyleProps
40
76
  ): FormFieldLayoutStyle => {
41
- const { inline } = props
77
+ const { inline, layout, vAlign, labelAlign } = props
78
+ const { hasMessages, hasVisibleLabel, hasNewErrorMsgAndIsGroup } = styleProps
79
+ const isInlineLayout = layout === 'inline'
80
+ // This is quite ugly, we should simplify it
81
+ const gridTemplateAreas = generateGridLayout(
82
+ isInlineLayout,
83
+ hasNewErrorMsgAndIsGroup,
84
+ hasVisibleLabel,
85
+ hasMessages
86
+ )
87
+ let gridTemplateColumns = '100%' // stacked layout
88
+ if (isInlineLayout) {
89
+ gridTemplateColumns = '1fr 3fr'
90
+ if (inline) {
91
+ gridTemplateColumns = 'auto 3fr'
92
+ }
93
+ }
94
+ const labelStyles = {
95
+ all: 'initial',
96
+ display: 'block',
97
+ gridArea: 'label',
98
+ color: componentTheme.color,
99
+ fontFamily: componentTheme.fontFamily,
100
+ fontWeight: componentTheme.fontWeight,
101
+ fontSize: componentTheme.fontSize,
102
+ lineHeight: componentTheme.lineHeight,
103
+ ...(isInlineLayout && {
104
+ margin: '0',
105
+ // when inline add a small padding between the label and the control
106
+ paddingRight: componentTheme.inlinePadding,
107
+ // and use the horizontal alignment prop
108
+ [`@media screen and (width >= ${componentTheme.stackedOrInlineBreakpoint})`]:
109
+ {
110
+ textAlign: labelAlign
111
+ }
112
+ })
113
+ }
42
114
 
115
+ let alignItems = 'start'
116
+ if (vAlign == 'top') {
117
+ alignItems = 'start'
118
+ } else if (vAlign == 'middle') {
119
+ alignItems = 'center'
120
+ } else if (vAlign == 'bottom') {
121
+ alignItems = 'end'
122
+ }
43
123
  return {
44
- groupErrorMessage: {
45
- margin: '0.5rem 0',
46
- },
47
124
  formFieldLayout: {
48
125
  label: 'formFieldLayout',
49
126
  all: 'initial',
@@ -54,14 +131,49 @@ const generateStyle = (
54
131
  direction: 'inherit',
55
132
  textAlign: 'start',
56
133
  opacity: 'inherit',
57
- display: 'block',
134
+ display: 'grid',
135
+ alignItems: alignItems,
136
+ verticalAlign: 'middle', // removes margin in inline layouts
137
+ gridTemplateColumns: gridTemplateColumns,
138
+ gridTemplateAreas: gridTemplateAreas,
139
+ [`@media screen and (width < ${componentTheme.stackedOrInlineBreakpoint})`]:
140
+ {
141
+ // for small screens use the stacked layout
142
+ gridTemplateColumns: '100%',
143
+ gridTemplateAreas: generateGridLayout(
144
+ false,
145
+ hasNewErrorMsgAndIsGroup,
146
+ hasVisibleLabel,
147
+ hasMessages
148
+ )
149
+ },
150
+ columnGap: '0.375rem',
151
+ rowGap: '0.75rem',
58
152
  width: '100%',
59
-
60
153
  ...(inline && {
61
- display: 'inline-block',
62
- verticalAlign: 'middle',
154
+ display: 'inline-grid',
63
155
  width: 'auto'
64
156
  })
157
+ },
158
+ formFieldLabel: {
159
+ label: 'formFieldLayout__label',
160
+ ...labelStyles,
161
+ // NOTE: needs separate groups for `:is()` and `:-webkit-any()` because of css selector group validation (see https://www.w3.org/TR/selectors-3/#grouping)
162
+ '&:is(label)': labelStyles,
163
+ '&:-webkit-any(label)': labelStyles
164
+ },
165
+ formFieldChildren: {
166
+ label: 'formFieldLayout__children',
167
+ gridArea: 'controls',
168
+ // add a small margin between the message and the controls
169
+ ...(hasMessages && hasNewErrorMsgAndIsGroup && { marginTop: '0.375rem' }),
170
+ ...(isInlineLayout &&
171
+ inline && {
172
+ [`@media screen and (width >= ${componentTheme.stackedOrInlineBreakpoint})`]:
173
+ {
174
+ justifySelf: 'start'
175
+ }
176
+ })
65
177
  }
66
178
  }
67
179
  }
@@ -0,0 +1,59 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ import type { Theme, ThemeSpecificStyle } from '@instructure/ui-themes'
26
+ import { FormFieldLayoutTheme } from '@instructure/shared-types'
27
+
28
+ /**
29
+ * Generates the theme object for the component from the theme and provided additional information
30
+ * @param {Object} theme The actual theme object.
31
+ * @return {Object} The final theme object with the overrides and component variables
32
+ */
33
+ const generateComponentTheme = (theme: Theme): FormFieldLayoutTheme => {
34
+ const { colors, typography, spacing, breakpoints, key: themeName } = theme
35
+
36
+ const themeSpecificStyle: ThemeSpecificStyle<FormFieldLayoutTheme> = {
37
+ canvas: {
38
+ color: theme['ic-brand-font-color-dark']
39
+ }
40
+ }
41
+
42
+ const componentVariables: FormFieldLayoutTheme = {
43
+ color: colors?.licorice,
44
+ fontFamily: typography?.fontFamily,
45
+ fontWeight: typography?.fontWeightBold,
46
+ fontSize: typography?.fontSizeMedium,
47
+ lineHeight: typography?.lineHeightFit,
48
+ inlinePadding: spacing?.xxSmall,
49
+ stackedOrInlineBreakpoint: breakpoints?.medium,
50
+ spacing: theme.spacing
51
+ }
52
+
53
+ return {
54
+ ...componentVariables,
55
+ ...themeSpecificStyle[themeName]
56
+ }
57
+ }
58
+
59
+ export default generateComponentTheme
@@ -42,6 +42,11 @@ type FormFieldMessagesOwnProps = {
42
42
  * }`
43
43
  */
44
44
  messages?: FormMessage[]
45
+ /**
46
+ * Specifies the size and location if inside a CSS grid, see
47
+ * https://developer.mozilla.org/en-US/docs/Web/CSS/grid-area
48
+ */
49
+ gridArea?: string
45
50
  }
46
51
 
47
52
  type PropKeys = keyof FormFieldMessagesOwnProps
@@ -55,10 +60,11 @@ type FormFieldMessagesProps = FormFieldMessagesOwnProps &
55
60
  type FormFieldMessagesStyle = ComponentStyle<'formFieldMessages' | 'message'>
56
61
 
57
62
  const propTypes: PropValidators<PropKeys> = {
58
- messages: PropTypes.arrayOf(FormPropTypes.message)
63
+ messages: PropTypes.arrayOf(FormPropTypes.message),
64
+ gridArea: PropTypes.string
59
65
  }
60
66
 
61
- const allowedProps: AllowedPropKeys = ['messages']
67
+ const allowedProps: AllowedPropKeys = ['messages', 'gridArea']
62
68
 
63
69
  export type { FormFieldMessagesProps, FormFieldMessagesStyle }
64
70
  export { propTypes, allowedProps }
@@ -23,7 +23,7 @@
23
23
  */
24
24
 
25
25
  import type { FormFieldMessagesTheme } from '@instructure/shared-types'
26
- import type { FormFieldMessagesStyle } from './props'
26
+ import type { FormFieldMessagesProps, FormFieldMessagesStyle } from './props'
27
27
 
28
28
  /**
29
29
  * ---
@@ -32,18 +32,19 @@ import type { FormFieldMessagesStyle } from './props'
32
32
  * Generates the style object from the theme and provided additional information
33
33
  * @param {Object} componentTheme The theme variable object.
34
34
  * @param {Object} props the props of the component, the style is applied to
35
- * @param {Object} state the state of the component, the style is applied to
36
35
  * @return {Object} The final style object, which will be used in the component
37
36
  */
38
37
  const generateStyle = (
39
- componentTheme: FormFieldMessagesTheme
38
+ componentTheme: FormFieldMessagesTheme,
39
+ props: FormFieldMessagesProps
40
40
  ): FormFieldMessagesStyle => {
41
41
  return {
42
42
  formFieldMessages: {
43
43
  label: 'formFieldMessages',
44
44
  padding: 0,
45
45
  display: 'block',
46
- margin: `calc(-1 * ${componentTheme.topMargin}) 0 0 0`
46
+ margin: `calc(-${componentTheme.topMargin}) 0 0 0`,
47
+ ...(props.gridArea && { gridArea: props.gridArea })
47
48
  },
48
49
  message: {
49
50
  label: 'formFieldMessages__message',
@@ -40,6 +40,10 @@ type FormMessageType =
40
40
  | 'hint'
41
41
  | 'success'
42
42
  | 'screenreader-only'
43
+ // TODO it will be easier if this would be just a string
44
+ /**
45
+ * The text to display in the form message
46
+ */
43
47
  type FormMessageChild = React.ReactNode
44
48
 
45
49
  /**