Skip to content

Commit

Permalink
Merge pull request #1462 from didi/feat-react-wxkey
Browse files Browse the repository at this point in the history
feat: wx:key
  • Loading branch information
hiyuki committed May 15, 2024
2 parents 7f66e9e + 30368a5 commit 4bcf20b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import directiveHelperMixin from './directiveHelperMixin.ios'
export default directiveHelperMixin
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { warn, type } from '@mpxjs/utils'
export default function directiveHelperMixin () {
return {
methods: {
__getWxKey (item, key) {
const value = key === '*this' ? item : item[key]
if (typeof value === 'string' || typeof value === 'number') {
return value
} else {
warn(`wx:key's value should return a string or a number, received: ${type(value)}`, this.__mpxProxy.options.mpxFileResource)
}
}
}
}
}
2 changes: 2 additions & 0 deletions packages/core/src/platform/builtInMixins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import componentGenericsMixin from './componentGenericsMixin'
import getTabBarMixin from './getTabBarMixin'
import pageRouteMixin from './pageRouteMixin'
import styleHelperMixin from './styleHelperMixin'
import directiveHelperMixin from './directiveHelperMixin'
import { isReact } from '@mpxjs/utils'

export default function getBuiltInMixins (options, type) {
let bulitInMixins
if (isReact) {
bulitInMixins = [
proxyEventMixin(),
directiveHelperMixin(),
styleHelperMixin()
]
} else if (__mpx_mode__ === 'web') {
Expand Down
7 changes: 7 additions & 0 deletions packages/webpack-plugin/lib/template-compiler/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -1751,6 +1751,13 @@ function postProcessFor (el) {

function postProcessForReact (el) {
if (el.for) {
if (el.for.key) {
addExp(el, `this.__getWxKey(${el.for.item}, '${el.for.key}')`, false, 'key')
addAttrs(el, [{
name: 'key',
value: el.for.key
}])
}
popForScopes()
}
}
Expand Down

0 comments on commit 4bcf20b

Please sign in to comment.