@sanity/ui 5.0.0-alpha.25 → 5.0.0-alpha.27

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.
@@ -118,18 +118,58 @@ describe('getProps', () => {
118
118
  expect(result.style).toEqual({})
119
119
  })
120
120
 
121
+ it('does not forward undefined known layout props', () => {
122
+ const result = getProps({width: undefined}, layoutProps)
123
+ expect(result).not.toHaveProperty('width')
124
+ expect(result.className).toBe('')
125
+ expect(result.style).toEqual({})
126
+ })
127
+
128
+ it('does not forward null known layout props', () => {
129
+ const result = getProps({width: null}, layoutProps)
130
+ expect(result).not.toHaveProperty('width')
131
+ expect(result.className).toBe('')
132
+ expect(result.style).toEqual({})
133
+ })
134
+
135
+ it('forwards keys without a prop def', () => {
136
+ const result = getProps({'id': 'test', 'data-testid': 'grid'}, layoutProps)
137
+ expect(result['id']).toEqual('test')
138
+ expect(result['data-testid']).toEqual('grid')
139
+ expect(result.className).toBe('')
140
+ expect(result.style).toEqual({})
141
+ })
142
+
121
143
  it('generates responsive className with undefined values', () => {
122
144
  const result = getProps({padding: [1, 2, undefined, 3]}, layoutProps)
123
145
  expect(result.className).toBe('sui-p1 sui-p2-bp-1 sui-p3-bp-3')
124
146
  expect(result.style).toEqual({})
125
147
  })
126
148
 
149
+ it('generates responsive className with undefined values and style based on strings', () => {
150
+ const result = getProps({width: ['100px', undefined, '300px']}, layoutProps)
151
+ expect(result.className).toBe('sui-width sui-width-bp-2')
152
+ expect(result.style).toEqual({
153
+ '--width': '100px',
154
+ '--width-bp-2': '300px',
155
+ })
156
+ })
157
+
127
158
  it('generates responsive className with null values', () => {
128
159
  const result = getProps({padding: [1, 2, null, 3]}, layoutProps)
129
160
  expect(result.className).toBe('sui-p1 sui-p2-bp-1 sui-p3-bp-3')
130
161
  expect(result.style).toEqual({})
131
162
  })
132
163
 
164
+ it('generates responsive className with null values and style based on strings', () => {
165
+ const result = getProps({width: ['100px', null, '300px']}, layoutProps)
166
+ expect(result.className).toBe('sui-width sui-width-bp-2')
167
+ expect(result.style).toEqual({
168
+ '--width': '100px',
169
+ '--width-bp-2': '300px',
170
+ })
171
+ })
172
+
133
173
  it('generates responsive className without unsupported values', () => {
134
174
  const result = getProps({padding: [1, 2, 10, 3]}, layoutProps)
135
175
  expect(result.className).toBe('sui-p1 sui-p2-bp-1 sui-p3-bp-3')
@@ -17,8 +17,9 @@ export function getProps(
17
17
  ): ComponentProps {
18
18
  const {allComponentProps, allPropDefs} = flattenCompositeProps(componentProps, propDefs)
19
19
  const restProps: ComponentProps = {}
20
- let className = componentProps?.className || ''
21
- let style = componentProps?.style || {}
20
+ const classNames: string[] = []
21
+ const style = (componentProps?.style ? {...componentProps.style} : {}) as React.CSSProperties &
22
+ Record<string, string | number | undefined>
22
23
 
23
24
  for (const key in allComponentProps) {
24
25
  const propDef = allPropDefs?.[key]
@@ -32,19 +33,28 @@ export function getProps(
32
33
  }
33
34
 
34
35
  if (Array.isArray(propValue)) {
35
- // @TODO: consider fixing this O(n^2) time complexity
36
- // oxlint-disable-next-line no-accumulating-spread
37
36
  for (let i = 0, len = Math.min(propValue.length, BREAKPOINTS_LENGTH); i < len; i++) {
38
37
  const resolvedPropDef = getResolvedPropDef(propValue[i], propDef)
39
38
 
40
- if (!resolvedPropDef) {
39
+ if (!resolvedPropDef || propValue[i] === null || propValue[i] === undefined) {
41
40
  continue
42
41
  }
43
42
 
44
- className = clsx(className, getClassName(propValue[i], resolvedPropDef, i))
45
- style = {...style, ...getStyle(propValue[i], resolvedPropDef, i)}
43
+ const className = getClassName(propValue[i], resolvedPropDef, i)
44
+ if (className) {
45
+ classNames.push(className)
46
+ }
47
+
48
+ const styleKey = getStyleKey(resolvedPropDef, i)
49
+ if (styleKey) {
50
+ style[styleKey] = propValue[i] as string | number
51
+ }
46
52
  }
47
53
  } else {
54
+ if (propValue === null || propValue === undefined) {
55
+ continue
56
+ }
57
+
48
58
  const resolvedPropDef = getResolvedPropDef(propValue, propDef)
49
59
 
50
60
  if (!resolvedPropDef) {
@@ -52,12 +62,23 @@ export function getProps(
52
62
  continue
53
63
  }
54
64
 
55
- className = clsx(className, getClassName(propValue, resolvedPropDef))
56
- style = {...style, ...getStyle(propValue, resolvedPropDef)}
65
+ const className = getClassName(propValue, resolvedPropDef)
66
+ if (className) {
67
+ classNames.push(className)
68
+ }
69
+
70
+ const styleKey = getStyleKey(resolvedPropDef)
71
+ if (styleKey) {
72
+ style[styleKey] = propValue as string | number
73
+ }
57
74
  }
58
75
  }
59
76
 
60
- return {...restProps, className, style}
77
+ return {
78
+ ...restProps,
79
+ className: clsx(componentProps?.className, ...classNames),
80
+ style,
81
+ }
61
82
  }
62
83
 
63
84
  function getResolvedPropDef(propValue: any, propDef?: PropDef) {
@@ -97,14 +118,12 @@ function getClassName(propValue: any, propDef: PropDef, bp?: number) {
97
118
  return ''
98
119
  }
99
120
 
100
- function getStyle(propValue: any, propDef: PropDef, bp?: number) {
121
+ function getStyleKey(propDef: PropDef, bp?: number): string | undefined {
101
122
  if ((propDef.type === 'string' || propDef.type === 'number') && propDef.variable) {
102
- return {
103
- [`${propDef.variable}${bp ? `-bp-${bp}` : ''}`]: propValue,
104
- }
123
+ return `${propDef.variable}${bp ? `-bp-${bp}` : ''}`
105
124
  }
106
125
 
107
- return {}
126
+ return undefined
108
127
  }
109
128
 
110
129
  export function flattenCompositeProps(
package/src/version.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  // This file is generated by scripts/write-version.js. Do not edit.
2
- export const VERSION = '500alpha25'
3
- export const HASH = '13d200'
2
+ export const VERSION = '500alpha27'
3
+ export const HASH = '711625'