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