@mpxjs/webpack-plugin 2.9.62 → 2.9.65

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 (95) hide show
  1. package/lib/config.js +38 -10
  2. package/lib/index.js +1 -3
  3. package/lib/platform/style/wx/index.js +115 -66
  4. package/lib/platform/template/wx/index.js +12 -8
  5. package/lib/react/processStyles.js +1 -0
  6. package/lib/react/processTemplate.js +2 -3
  7. package/lib/react/style-helper.js +9 -7
  8. package/lib/runtime/components/react/context.ts +9 -7
  9. package/lib/runtime/components/react/dist/context.js +1 -0
  10. package/lib/runtime/components/react/dist/getInnerListeners.js +12 -1
  11. package/lib/runtime/components/react/dist/mpx-button.jsx +53 -74
  12. package/lib/runtime/components/react/dist/mpx-checkbox-group.jsx +20 -18
  13. package/lib/runtime/components/react/dist/mpx-checkbox.jsx +30 -42
  14. package/lib/runtime/components/react/dist/mpx-form.jsx +18 -15
  15. package/lib/runtime/components/react/dist/mpx-icon.jsx +15 -17
  16. package/lib/runtime/components/react/dist/mpx-image/index.jsx +36 -34
  17. package/lib/runtime/components/react/dist/mpx-image/svg.jsx +3 -1
  18. package/lib/runtime/components/react/dist/mpx-input.jsx +36 -31
  19. package/lib/runtime/components/react/dist/mpx-label.jsx +30 -37
  20. package/lib/runtime/components/react/dist/mpx-movable-area.jsx +15 -19
  21. package/lib/runtime/components/react/dist/mpx-movable-view.jsx +10 -9
  22. package/lib/runtime/components/react/dist/mpx-picker/date.jsx +2 -1
  23. package/lib/runtime/components/react/dist/mpx-picker/index.jsx +11 -10
  24. package/lib/runtime/components/react/dist/mpx-picker/multiSelector.jsx +9 -5
  25. package/lib/runtime/components/react/dist/mpx-picker/region.jsx +13 -8
  26. package/lib/runtime/components/react/dist/mpx-picker/selector.jsx +3 -2
  27. package/lib/runtime/components/react/dist/mpx-picker/time.jsx +22 -20
  28. package/lib/runtime/components/react/dist/mpx-picker-view-column.jsx +103 -10
  29. package/lib/runtime/components/react/dist/mpx-picker-view.jsx +149 -54
  30. package/lib/runtime/components/react/dist/mpx-radio-group.jsx +20 -18
  31. package/lib/runtime/components/react/dist/mpx-radio.jsx +29 -43
  32. package/lib/runtime/components/react/dist/mpx-root-portal.jsx +8 -4
  33. package/lib/runtime/components/react/dist/mpx-scroll-view.jsx +36 -27
  34. package/lib/runtime/components/react/dist/mpx-swiper/carouse.jsx +141 -75
  35. package/lib/runtime/components/react/dist/mpx-swiper/index.jsx +16 -7
  36. package/lib/runtime/components/react/dist/mpx-swiper-item.jsx +20 -11
  37. package/lib/runtime/components/react/dist/mpx-switch.jsx +18 -14
  38. package/lib/runtime/components/react/dist/mpx-text.jsx +20 -35
  39. package/lib/runtime/components/react/dist/mpx-textarea.jsx +1 -1
  40. package/lib/runtime/components/react/dist/mpx-view.jsx +296 -210
  41. package/lib/runtime/components/react/dist/mpx-web-view.jsx +11 -7
  42. package/lib/runtime/components/react/dist/parser.js +218 -0
  43. package/lib/runtime/components/react/dist/useNodesRef.js +1 -5
  44. package/lib/runtime/components/react/dist/utils.jsx +445 -0
  45. package/lib/runtime/components/react/getInnerListeners.ts +18 -8
  46. package/lib/runtime/components/react/mpx-button.tsx +83 -91
  47. package/lib/runtime/components/react/mpx-checkbox-group.tsx +50 -43
  48. package/lib/runtime/components/react/mpx-checkbox.tsx +56 -64
  49. package/lib/runtime/components/react/mpx-form.tsx +51 -22
  50. package/lib/runtime/components/react/mpx-icon.tsx +31 -27
  51. package/lib/runtime/components/react/mpx-image/index.tsx +54 -47
  52. package/lib/runtime/components/react/mpx-image/svg.tsx +5 -3
  53. package/lib/runtime/components/react/mpx-input.tsx +59 -38
  54. package/lib/runtime/components/react/mpx-label.tsx +55 -59
  55. package/lib/runtime/components/react/mpx-movable-area.tsx +40 -25
  56. package/lib/runtime/components/react/mpx-movable-view.tsx +29 -29
  57. package/lib/runtime/components/react/mpx-navigator.tsx +2 -2
  58. package/lib/runtime/components/react/mpx-picker/date.tsx +4 -4
  59. package/lib/runtime/components/react/mpx-picker/index.tsx +12 -11
  60. package/lib/runtime/components/react/mpx-picker/multiSelector.tsx +17 -13
  61. package/lib/runtime/components/react/mpx-picker/region.tsx +23 -19
  62. package/lib/runtime/components/react/mpx-picker/selector.tsx +7 -7
  63. package/lib/runtime/components/react/mpx-picker/time.tsx +29 -31
  64. package/lib/runtime/components/react/mpx-picker/type.ts +1 -1
  65. package/lib/runtime/components/react/mpx-picker-view-column.tsx +149 -20
  66. package/lib/runtime/components/react/mpx-picker-view.tsx +180 -63
  67. package/lib/runtime/components/react/mpx-radio-group.tsx +51 -47
  68. package/lib/runtime/components/react/mpx-radio.tsx +57 -72
  69. package/lib/runtime/components/react/mpx-root-portal.tsx +10 -8
  70. package/lib/runtime/components/react/mpx-scroll-view.tsx +136 -104
  71. package/lib/runtime/components/react/mpx-swiper/carouse.tsx +175 -96
  72. package/lib/runtime/components/react/mpx-swiper/index.tsx +21 -9
  73. package/lib/runtime/components/react/mpx-swiper/type.ts +16 -5
  74. package/lib/runtime/components/react/mpx-swiper-item.tsx +48 -14
  75. package/lib/runtime/components/react/mpx-switch.tsx +46 -24
  76. package/lib/runtime/components/react/mpx-text.tsx +38 -45
  77. package/lib/runtime/components/react/mpx-textarea.tsx +1 -1
  78. package/lib/runtime/components/react/mpx-view.tsx +401 -241
  79. package/lib/runtime/components/react/mpx-web-view.tsx +22 -22
  80. package/lib/runtime/components/react/parser.ts +245 -0
  81. package/lib/runtime/components/react/types/common.ts +4 -4
  82. package/lib/runtime/components/react/types/global.d.ts +24 -2
  83. package/lib/runtime/components/react/useNodesRef.ts +1 -7
  84. package/lib/runtime/components/react/utils.tsx +524 -0
  85. package/lib/runtime/components/web/mpx-scroll-view.vue +25 -5
  86. package/lib/style-compiler/index.js +5 -4
  87. package/lib/template-compiler/compiler.js +133 -161
  88. package/lib/template-compiler/gen-node-react.js +1 -3
  89. package/lib/utils/const.js +2 -1
  90. package/lib/web/processStyles.js +2 -1
  91. package/lib/web/processTemplate.js +2 -3
  92. package/lib/wxml/loader.js +1 -1
  93. package/package.json +7 -4
  94. package/lib/runtime/components/react/dist/utils.js +0 -148
  95. package/lib/runtime/components/react/utils.ts +0 -170
@@ -6,8 +6,8 @@
6
6
  */
7
7
  import {
8
8
  JSX,
9
- useRef,
10
9
  useState,
10
+ useRef,
11
11
  forwardRef,
12
12
  useEffect,
13
13
  ReactNode,
@@ -15,19 +15,17 @@ import {
15
15
  Dispatch,
16
16
  SetStateAction
17
17
  } from 'react'
18
-
19
18
  import {
20
19
  View,
21
- Text,
22
20
  StyleSheet,
23
21
  ViewStyle,
24
- NativeSyntheticEvent,
25
- TextStyle
22
+ NativeSyntheticEvent
26
23
  } from 'react-native'
24
+ import { warn } from '@mpxjs/utils'
27
25
  import useInnerProps, { getCustomEvent } from './getInnerListeners'
28
26
  import useNodesRef, { HandlerRef } from './useNodesRef'
29
27
  import Icon from './mpx-icon'
30
- import { every, splitStyle, isText, splitProps, throwReactWarning } from './utils'
28
+ import { splitProps, splitStyle, useLayout, useTransformStyle, wrapChildren } from './utils'
31
29
  import { CheckboxGroupContext, LabelContext } from './context'
32
30
 
33
31
  interface Selection {
@@ -41,6 +39,11 @@ export interface CheckboxProps extends Selection {
41
39
  style?: ViewStyle & Record<string, any>
42
40
  groupValue?: Array<string>
43
41
  'enable-offset'?: boolean
42
+ 'enable-var'?: boolean
43
+ 'external-var-context'?: Record<string, any>
44
+ 'parent-font-size'?: number;
45
+ 'parent-width'?: number;
46
+ 'parent-height'?: number;
44
47
  children?: ReactNode
45
48
  bindtap?: (evt: NativeSyntheticEvent<TouchEvent> | unknown) => void
46
49
  catchtap?: (evt: NativeSyntheticEvent<TouchEvent> | unknown) => void
@@ -74,41 +77,38 @@ const styles = StyleSheet.create({
74
77
  })
75
78
 
76
79
  const Checkbox = forwardRef<HandlerRef<View, CheckboxProps>, CheckboxProps>(
77
- (props, ref): JSX.Element => {
80
+ (checkboxProps, ref): JSX.Element => {
81
+ const { textProps, innerProps: props = {} } = splitProps(checkboxProps)
82
+
78
83
  const {
79
84
  value = '',
80
85
  disabled = false,
81
86
  checked = false,
82
87
  color = '#09BB07',
83
88
  style = {},
84
- 'enable-offset': enableOffset,
85
- children,
89
+ 'enable-var': enableVar,
90
+ 'external-var-context': externalVarContext,
91
+ 'parent-font-size': parentFontSize,
92
+ 'parent-width': parentWidth,
93
+ 'parent-height': parentHeight,
86
94
  bindtap,
87
- catchtap,
95
+ catchtap
88
96
  } = props
89
97
 
90
- const layoutRef = useRef({})
91
-
92
98
  const [isChecked, setIsChecked] = useState<boolean>(!!checked)
93
99
 
94
- const { textStyle, imageStyle, innerStyle } = splitStyle(style)
95
-
96
- if (imageStyle) {
97
- throwReactWarning('[Mpx runtime warn]: Checkbox does not support background image-related styles!')
98
- }
99
-
100
100
  const groupContext = useContext(CheckboxGroupContext)
101
- let groupValue: { [key: string]: { checked: boolean; setValue: Dispatch<SetStateAction<boolean>>; } } | undefined;
102
- let notifyChange: (evt: NativeSyntheticEvent<TouchEvent>) => void | undefined;
101
+ let groupValue: { [key: string]: { checked: boolean; setValue: Dispatch<SetStateAction<boolean>>; } } | undefined
102
+ let notifyChange: (evt: NativeSyntheticEvent<TouchEvent>) => void | undefined
103
103
 
104
104
  const defaultStyle = {
105
105
  ...styles.wrapper,
106
- ...(disabled && styles.wrapperDisabled),
106
+ ...(disabled && styles.wrapperDisabled)
107
107
  }
108
108
 
109
- const viewStyle = {
110
- ...defaultStyle,
111
- ...innerStyle
109
+ const styleObj = {
110
+ ...styles.container,
111
+ ...style
112
112
  }
113
113
 
114
114
  const onChange = (evt: NativeSyntheticEvent<TouchEvent>) => {
@@ -133,46 +133,28 @@ const Checkbox = forwardRef<HandlerRef<View, CheckboxProps>, CheckboxProps>(
133
133
  onChange(evt)
134
134
  }
135
135
 
136
- const { nodeRef } = useNodesRef(props, ref, {
136
+ const {
137
+ hasSelfPercent,
138
+ normalStyle,
139
+ hasVarDec,
140
+ varContextRef,
141
+ setWidth,
142
+ setHeight
143
+ } = useTransformStyle(styleObj, { enableVar, externalVarContext, parentFontSize, parentWidth, parentHeight })
144
+
145
+ const nodeRef = useRef(null)
146
+
147
+ useNodesRef(props, ref, nodeRef, {
137
148
  defaultStyle,
138
149
  change: onChange
139
150
  })
140
151
 
141
- const onLayout = () => {
142
- nodeRef.current?.measure(
143
- (
144
- x: number,
145
- y: number,
146
- width: number,
147
- height: number,
148
- offsetLeft: number,
149
- offsetTop: number
150
- ) => {
151
- layoutRef.current = { x, y, width, height, offsetLeft, offsetTop }
152
- }
153
- )
154
- }
152
+ const { layoutRef, layoutStyle, layoutProps } = useLayout({ props, hasSelfPercent, setWidth, setHeight, nodeRef })
155
153
 
156
- const wrapChildren = (
157
- children: ReactNode,
158
- textStyle?: TextStyle
159
- ) => {
160
- if (!children) return children
161
- const { textProps } = splitProps(props)
162
-
163
- if (every(children, (child) => isText(child))) {
164
- if (textStyle || textProps) {
165
- children = <Text key='checkboxTextWrap' style={textStyle || {}} {...(textProps || {})}>{children}</Text>
166
- }
167
- } else {
168
- if (textStyle) {
169
- throwReactWarning(
170
- '[Mpx runtime warn]: Text style will be ignored unless every child of the Checkbox is Text node!'
171
- )
172
- }
173
- }
154
+ const { textStyle, backgroundStyle, innerStyle } = splitStyle(normalStyle)
174
155
 
175
- return children
156
+ if (backgroundStyle) {
157
+ warn('Checkbox does not support background image-related styles!')
176
158
  }
177
159
 
178
160
  const labelContext = useContext(LabelContext)
@@ -190,12 +172,12 @@ const Checkbox = forwardRef<HandlerRef<View, CheckboxProps>, CheckboxProps>(
190
172
  props,
191
173
  {
192
174
  ref: nodeRef,
193
- style: styles.container,
175
+ style: { ...innerStyle, ...layoutStyle },
176
+ ...layoutProps,
194
177
  bindtap: onTap,
195
- catchtap: catchTap,
196
- ...(enableOffset ? { onLayout } : {})
178
+ catchtap: catchTap
197
179
  },
198
- ['enable-offset'],
180
+ [],
199
181
  {
200
182
  layoutRef
201
183
  }
@@ -226,7 +208,7 @@ const Checkbox = forwardRef<HandlerRef<View, CheckboxProps>, CheckboxProps>(
226
208
 
227
209
  return (
228
210
  <View {...innerProps}>
229
- <View style={viewStyle}>
211
+ <View style={defaultStyle}>
230
212
  <Icon
231
213
  type='success_no_circle'
232
214
  size={18}
@@ -234,7 +216,17 @@ const Checkbox = forwardRef<HandlerRef<View, CheckboxProps>, CheckboxProps>(
234
216
  style={isChecked ? styles.iconChecked : styles.icon}
235
217
  />
236
218
  </View>
237
- {wrapChildren(children, textStyle)}
219
+ {
220
+ wrapChildren(
221
+ props,
222
+ {
223
+ hasVarDec,
224
+ varContext: varContextRef.current,
225
+ textStyle,
226
+ textProps
227
+ }
228
+ )
229
+ }
238
230
  </View>
239
231
  )
240
232
  }
@@ -5,15 +5,21 @@
5
5
  * ✔ bindreset
6
6
  */
7
7
 
8
- import { View, LayoutChangeEvent } from 'react-native';
9
- import { JSX, useRef, forwardRef, ReactNode } from 'react';
8
+ import { View } from 'react-native'
9
+ import { JSX, useRef, forwardRef, ReactNode } from 'react'
10
10
  import useNodesRef, { HandlerRef } from './useNodesRef'
11
11
  import useInnerProps, { getCustomEvent } from './getInnerListeners'
12
12
  import { FormContext } from './context'
13
-
13
+ import { useTransformStyle, splitProps, splitStyle, useLayout, wrapChildren } from './utils'
14
14
  interface FormProps {
15
15
  style?: Record<string, any>;
16
16
  children: ReactNode;
17
+ 'enable-offset'?: boolean;
18
+ 'enable-var'?: boolean
19
+ 'external-var-context'?: Record<string, any>;
20
+ 'parent-font-size'?: number;
21
+ 'parent-width'?: number;
22
+ 'parent-height'?: number;
17
23
  bindsubmit?: (evt: {
18
24
  detail: {
19
25
  value: any;
@@ -22,23 +28,38 @@ interface FormProps {
22
28
  bindreset?: () => void;
23
29
  }
24
30
 
25
- const _Form = forwardRef<HandlerRef<View, FormProps>, FormProps>((props: FormProps, ref): JSX.Element => {
26
- const { children, style } = props;
27
- const layoutRef = useRef({})
31
+ const _Form = forwardRef<HandlerRef<View, FormProps>, FormProps>((fromProps: FormProps, ref): JSX.Element => {
32
+ const { textProps, innerProps: props = {} } = splitProps(fromProps)
28
33
  const formValuesMap = useRef(new Map()).current
34
+ const {
35
+ style,
36
+ 'enable-var': enableVar,
37
+ 'external-var-context': externalVarContext,
38
+ 'parent-font-size': parentFontSize,
39
+ 'parent-width': parentWidth,
40
+ 'parent-height': parentHeight
41
+ } = props
29
42
 
30
- const { nodeRef: formRef } = useNodesRef(props, ref)
43
+ const {
44
+ hasSelfPercent,
45
+ normalStyle,
46
+ hasVarDec,
47
+ varContextRef,
48
+ setWidth,
49
+ setHeight
50
+ } = useTransformStyle(style, { enableVar, externalVarContext, parentFontSize, parentWidth, parentHeight })
31
51
 
32
- const onLayout = (e: LayoutChangeEvent) => {
33
- formRef.current?.measure((x: number, y: number, width: number, height: number, offsetLeft: number, offsetTop: number) => {
34
- layoutRef.current = { x, y, width, height, offsetLeft, offsetTop }
35
- })
36
- }
52
+ const { textStyle, innerStyle } = splitStyle(normalStyle)
53
+
54
+ const formRef = useRef(null)
55
+ useNodesRef(props, ref, formRef)
56
+
57
+ const { layoutRef, layoutStyle, layoutProps } = useLayout({ props, hasSelfPercent, setWidth, setHeight, nodeRef: formRef })
37
58
 
38
59
  const submit = () => {
39
60
  const { bindsubmit } = props
40
61
  const formValue: Record<string, any> = {}
41
- for (let name of formValuesMap.keys()) {
62
+ for (const name of formValuesMap.keys()) {
42
63
  if (formValuesMap.get(name).getValue) {
43
64
  formValue[name] = formValuesMap.get(name).getValue()
44
65
  }
@@ -63,27 +84,35 @@ const _Form = forwardRef<HandlerRef<View, FormProps>, FormProps>((props: FormPro
63
84
  }
64
85
 
65
86
  const innerProps = useInnerProps(props, {
87
+ style: { ...innerStyle, ...layoutStyle },
66
88
  ref: formRef,
67
- style,
68
- onLayout
89
+ ...layoutProps
69
90
  }, [
70
- 'children',
71
- 'style',
72
91
  'bindsubmit',
73
92
  'bindreset'
74
- ], { layoutRef });
93
+ ], { layoutRef })
75
94
 
76
95
  return (
77
96
  <View
78
97
  {...innerProps}
79
98
  >
80
99
  <FormContext.Provider value={{ formValuesMap, submit, reset }}>
81
- {children}
100
+ {
101
+ wrapChildren(
102
+ props,
103
+ {
104
+ hasVarDec,
105
+ varContext: varContextRef.current,
106
+ textStyle,
107
+ textProps
108
+ }
109
+ )
110
+ }
82
111
  </FormContext.Provider>
83
112
  </View>
84
- );
113
+ )
85
114
  })
86
115
 
87
- _Form.displayName = 'mpx-form';
116
+ _Form.displayName = 'mpx-form'
88
117
 
89
- export default _Form
118
+ export default _Form
@@ -3,10 +3,11 @@
3
3
  * ✔ size
4
4
  * ✔ color
5
5
  */
6
- import { JSX, useRef, forwardRef } from 'react'
6
+ import { JSX, forwardRef, useRef } from 'react'
7
7
  import { Text, TextStyle, Image } from 'react-native'
8
8
  import useInnerProps from './getInnerListeners'
9
9
  import useNodesRef, { HandlerRef } from './useNodesRef'
10
+ import { useLayout, useTransformStyle } from './utils'
10
11
 
11
12
  export type IconType =
12
13
  | 'success'
@@ -25,6 +26,11 @@ export interface IconProps {
25
26
  color?: string
26
27
  style?: TextStyle & Record<string, any>
27
28
  'enable-offset'?: boolean
29
+ 'enable-var'?: boolean
30
+ 'external-var-context'?: Record<string, any>
31
+ 'parent-font-size'?: number
32
+ 'parent-width'?: number
33
+ 'parent-height'?: number
28
34
  }
29
35
 
30
36
  const IconTypeMap = new Map<IconType, string>([
@@ -46,49 +52,47 @@ const Icon = forwardRef<HandlerRef<Text, IconProps>, IconProps>(
46
52
  size = 23,
47
53
  color,
48
54
  style = {},
49
- 'enable-offset': enableOffset
55
+ 'enable-var': enableVar,
56
+ 'external-var-context': externalVarContext,
57
+ 'parent-font-size': parentFontSize,
58
+ 'parent-width': parentWidth,
59
+ 'parent-height': parentHeight
50
60
  } = props
51
61
 
52
62
  const uri = IconTypeMap.get(type)
53
63
 
54
64
  const defaultStyle = { width: ~~size, height: ~~size }
55
65
 
56
- const layoutRef = useRef({})
66
+ const styleObj = {
67
+ ...defaultStyle,
68
+ ...style
69
+ }
70
+
71
+ const {
72
+ hasSelfPercent,
73
+ normalStyle,
74
+ setWidth,
75
+ setHeight
76
+ } = useTransformStyle(styleObj, { enableVar, externalVarContext, parentFontSize, parentWidth, parentHeight })
57
77
 
58
- const { nodeRef } = useNodesRef(props, ref, {
59
- defaultStyle
60
- })
78
+ const nodeRef = useRef(null)
79
+ useNodesRef(props, ref, nodeRef, { defaultStyle })
61
80
 
62
- const onLayout = () => {
63
- nodeRef.current?.measure(
64
- (
65
- x: number,
66
- y: number,
67
- width: number,
68
- height: number,
69
- offsetLeft: number,
70
- offsetTop: number
71
- ) => {
72
- layoutRef.current = { x, y, width, height, offsetLeft, offsetTop }
73
- }
74
- )
75
- }
81
+ const { layoutRef, layoutStyle, layoutProps } = useLayout({ props, hasSelfPercent, setWidth, setHeight, nodeRef })
76
82
 
77
83
  const innerProps = useInnerProps(
78
84
  props,
79
85
  {
80
86
  ref: nodeRef,
81
87
  style: {
82
- ...defaultStyle,
83
- tintColor: color,
84
- ...style
88
+ ...normalStyle,
89
+ ...layoutStyle,
90
+ tintColor: color
85
91
  },
86
92
  source: { uri },
87
- ...(enableOffset ? { onLayout } : {})
93
+ ...layoutProps
88
94
  },
89
- [
90
- 'enable-offset'
91
- ],
95
+ [],
92
96
  {
93
97
  layoutRef
94
98
  }
@@ -23,10 +23,11 @@ import {
23
23
  ImageErrorEventData,
24
24
  LayoutChangeEvent,
25
25
  DimensionValue,
26
- ImageLoadEventData,
26
+ ImageLoadEventData
27
27
  } from 'react-native'
28
28
  import useInnerProps, { getCustomEvent } from '../getInnerListeners'
29
29
  import useNodesRef, { HandlerRef } from '../useNodesRef'
30
+ import { useLayout, useTransformStyle } from '../utils'
30
31
 
31
32
  export type Mode =
32
33
  | 'scaleToFill'
@@ -52,6 +53,11 @@ export interface ImageProps {
52
53
  svg?: boolean
53
54
  style?: ImageStyle & Record<string, any>
54
55
  'enable-offset'?: boolean;
56
+ 'enable-var'?: boolean
57
+ 'external-var-context'?: Record<string, any>
58
+ 'parent-font-size'?: number
59
+ 'parent-width'?: number
60
+ 'parent-height'?: number
55
61
  bindload?: (evt: NativeSyntheticEvent<ImageLoadEventData> | unknown) => void
56
62
  binderror?: (evt: NativeSyntheticEvent<ImageErrorEventData> | unknown) => void
57
63
  }
@@ -79,7 +85,7 @@ const cropMode: Mode[] = [
79
85
  'top left',
80
86
  'top right',
81
87
  'bottom left',
82
- 'bottom right',
88
+ 'bottom right'
83
89
  ]
84
90
 
85
91
  const ModeMap = new Map<Mode, ImageResizeMode | undefined>([
@@ -88,7 +94,7 @@ const ModeMap = new Map<Mode, ImageResizeMode | undefined>([
88
94
  ['aspectFill', 'cover'],
89
95
  ['widthFix', 'stretch'],
90
96
  ['heightFix', 'stretch'],
91
- ...cropMode.map<[Mode, ImageResizeMode]>(mode => [mode, 'stretch']),
97
+ ...cropMode.map<[Mode, ImageResizeMode]>(mode => [mode, 'stretch'])
92
98
  ])
93
99
 
94
100
  const isNumber = (value: DimensionValue) => typeof value === 'number'
@@ -109,21 +115,42 @@ const Image = forwardRef<HandlerRef<RNImage, ImageProps>, ImageProps>((props, re
109
115
  mode = 'scaleToFill',
110
116
  // svg = false,
111
117
  style = {},
112
- 'enable-offset': enableOffset,
118
+ 'enable-var': enableVar,
119
+ 'external-var-context': externalVarContext,
120
+ 'parent-font-size': parentFontSize,
121
+ 'parent-width': parentWidth,
122
+ 'parent-height': parentHeight,
113
123
  bindload,
114
124
  binderror
115
125
  } = props
116
126
 
117
- const { width = DEFAULT_IMAGE_WIDTH, height = DEFAULT_IMAGE_HEIGHT } = style as ImageStyle
127
+ const defaultStyle = {
128
+ width: DEFAULT_IMAGE_WIDTH,
129
+ height: DEFAULT_IMAGE_HEIGHT
130
+ }
118
131
 
119
- const { nodeRef } = useNodesRef(props, ref, {
120
- defaultStyle: {
121
- width: DEFAULT_IMAGE_WIDTH,
122
- height: DEFAULT_IMAGE_HEIGHT
123
- }
132
+ const styleObj = {
133
+ ...defaultStyle,
134
+ ...style,
135
+ overflow: 'hidden'
136
+ }
137
+
138
+ const nodeRef = useRef(null)
139
+ useNodesRef(props, ref, nodeRef, {
140
+ defaultStyle
124
141
  })
125
142
 
126
- const layoutRef = useRef({})
143
+ const onLayout = ({ nativeEvent: { layout: { width, height } } }: LayoutChangeEvent) => {
144
+ setViewWidth(width)
145
+ setViewHeight(height)
146
+ }
147
+
148
+ const { normalStyle, hasSelfPercent, setWidth, setHeight } = useTransformStyle(styleObj, { enableVar, externalVarContext, parentFontSize, parentWidth, parentHeight })
149
+
150
+ const { layoutRef, layoutStyle, layoutProps } = useLayout({ props, hasSelfPercent, setWidth, setHeight, nodeRef, onLayout })
151
+
152
+ const { width, height } = normalStyle
153
+
127
154
  const preSrc = useRef<string | undefined>()
128
155
 
129
156
  const resizeMode: ImageResizeMode = ModeMap.get(mode) || 'stretch'
@@ -133,8 +160,8 @@ const Image = forwardRef<HandlerRef<RNImage, ImageProps>, ImageProps>((props, re
133
160
 
134
161
  const source: ImageSourcePropType = typeof src === 'string' ? { uri: src } : src
135
162
 
136
- const [viewWidth, setViewWidth] = useState(isNumber(width) ? (width as number) : 0)
137
- const [viewHeight, setViewHeight] = useState(isNumber(height) ? (height as number) : 0)
163
+ const [viewWidth, setViewWidth] = useState(isNumber(width) ? width : 0)
164
+ const [viewHeight, setViewHeight] = useState(isNumber(height) ? height : 0)
138
165
  const [imageWidth, setImageWidth] = useState(0)
139
166
  const [imageHeight, setImageHeight] = useState(0)
140
167
  const [ratio, setRatio] = useState(0)
@@ -224,21 +251,6 @@ const Image = forwardRef<HandlerRef<RNImage, ImageProps>, ImageProps>((props, re
224
251
  )
225
252
  }
226
253
 
227
- const onViewLayout = ({
228
- nativeEvent: {
229
- layout: { width, height },
230
- },
231
- }: LayoutChangeEvent) => {
232
- setViewWidth(width)
233
- setViewHeight(height)
234
- }
235
-
236
- const onImageLayout = () => {
237
- nodeRef.current?.measure((x: number, y: number, width: number, height: number, offsetLeft: number, offsetTop: number) => {
238
- layoutRef.current = { x, y, width, height, offsetLeft, offsetTop }
239
- })
240
- }
241
-
242
254
  useEffect(() => {
243
255
  if (!isWidthFixMode && !isHeightFixMode && !isCropMode) {
244
256
  setLoaded(true)
@@ -275,14 +287,18 @@ const Image = forwardRef<HandlerRef<RNImage, ImageProps>, ImageProps>((props, re
275
287
 
276
288
  const innerProps = useInnerProps(props, {
277
289
  ref: nodeRef,
278
- ...(enableOffset ? { onLayout: onImageLayout } : {})
290
+ style: {
291
+ ...normalStyle,
292
+ ...layoutStyle,
293
+ ...(isHeightFixMode && { width: fixedWidth }),
294
+ ...(isWidthFixMode && { height: fixedHeight })
295
+ },
296
+ ...layoutProps
279
297
  },
280
- [
281
- 'enable-offset'
282
- ],
283
- {
284
- layoutRef
285
- }
298
+ [],
299
+ {
300
+ layoutRef
301
+ }
286
302
  )
287
303
 
288
304
  // if (typeof src === 'string' && REMOTE_SVG_REGEXP.test(src)) {
@@ -306,26 +322,17 @@ const Image = forwardRef<HandlerRef<RNImage, ImageProps>, ImageProps>((props, re
306
322
  // }
307
323
 
308
324
  return (
309
- <View
310
- style={{
311
- width, height,
312
- ...style,
313
- ...(isHeightFixMode && { width: fixedWidth }),
314
- ...(isWidthFixMode && { height: fixedHeight }),
315
- overflow: 'hidden',
316
- }}
317
- onLayout={onViewLayout}>
325
+ <View {...innerProps}>
318
326
  {
319
327
  loaded && <RNImage
320
- {...innerProps}
321
328
  source={source}
322
329
  resizeMode={resizeMode}
323
330
  onLoad={onImageLoad}
324
331
  onError={onImageError}
325
332
  style={{
326
333
  ...StyleSheet.absoluteFillObject,
327
- width: !isCropMode ? '100%' : imageWidth,
328
- height: !isCropMode ? '100%' : imageHeight,
334
+ width: isCropMode ? imageWidth : '100%',
335
+ height: isCropMode ? imageHeight : '100%',
329
336
  ...(isCropMode && cropModeStyle)
330
337
  }}
331
338
  />
@@ -10,11 +10,13 @@ interface SvgProps {
10
10
  }
11
11
 
12
12
  const Svg = ({ local = false, src, style, width, height }: SvgProps): JSX.Element => {
13
- return local ? (
13
+ return local
14
+ ? (
14
15
  <WithLocalSvg style={style} asset={src as ImageSourcePropType} width={width} height={height} />
15
- ) : (
16
+ )
17
+ : (
16
18
  <SvgCssUri style={style} uri={src as string} width={width} height={height} />
17
- )
19
+ )
18
20
  }
19
21
 
20
22
  export default Svg