diff --git a/docs/renderers/Carousel.md b/docs/renderers/Carousel.md index 6cffcb00..508925a6 100644 --- a/docs/renderers/Carousel.md +++ b/docs/renderers/Carousel.md @@ -12,7 +12,8 @@ - `titleClassName` 图片标题类名 - `description` 图片描述 - `descriptionClassName` 图片描述类名 - - `html` HTML 自定义,同[Tpl](./Tpl.md)一致 + - `item` HTML 自定义,同[Tpl](./Tpl.md)一致 +- `itemSchema` 自定义`schema`来展示数据 - `auto` 是否自动轮播,默认`true` - `interval` 切换动画间隔,默认`5s` - `duration` 切换动画时长,默认`0.5s` @@ -33,7 +34,7 @@ "image": "https://video-react.js.org/assets/poster.png" }, { - "html": "
carousel data
" + "item": "
carousel data
" }, { "image": "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3893101144,2877209892&fm=23&gp=0.jpg" diff --git a/examples/components/Carousel.jsx b/examples/components/Carousel.jsx index 5d5d8bc4..741b8ab9 100644 --- a/examples/components/Carousel.jsx +++ b/examples/components/Carousel.jsx @@ -4,7 +4,7 @@ export default { data: { carousel: [ { - html: '
carousel data in form
' + item: '
carousel data in form
' }, { image: 'https://www.baidu.com/img/bd_logo1.png' @@ -28,7 +28,7 @@ export default { image: 'https://video-react.js.org/assets/poster.png' }, { - html: '
carousel data
' + item: '
carousel data
' }, { image: 'https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3893101144,2877209892&fm=23&gp=0.jpg' diff --git a/mock/crud/list.js b/mock/crud/list.js index d6551266..f2bcc656 100644 --- a/mock/crud/list.js +++ b/mock/crud/list.js @@ -30,7 +30,7 @@ module.exports = function(req, res) { image: 'https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3893101144,2877209892&fm=23&gp=0.jpg' }, { - html: '
carousel data in crud
' + item: '
carousel data in crud
' }, { image: 'https://video-react.js.org/assets/poster.png' diff --git a/src/renderers/Carousel.tsx b/src/renderers/Carousel.tsx index e6445567..0b5815a1 100644 --- a/src/renderers/Carousel.tsx +++ b/src/renderers/Carousel.tsx @@ -1,7 +1,8 @@ import React from 'react'; import Transition, {ENTERED, ENTERING, EXITING} from 'react-transition-group/Transition'; import {Renderer, RendererProps} from '../factory'; -import {autobind, createObject} from '../utils/helper'; +import {resolveVariable} from '../utils/tpl-builtin'; +import {autobind, createObject, isObject} from '../utils/helper'; import {leftArrowIcon, rightArrowIcon} from '../components/icons'; const animationStyles: { @@ -53,7 +54,14 @@ export class Carousel extends React.Component { state = { current: 0, - options: this.props.value ? this.props.value : this.props.options ? this.props.options : [], + options: + this.props.value + ? this.props.value + : this.props.options + ? this.props.options + : resolveVariable(this.props.name, this.props.data) + ? resolveVariable(this.props.name, this.props.data) + : [], showArrows: false, nextAnimation: '' }; @@ -178,24 +186,6 @@ export class Carousel extends React.Component { ) } - @autobind - defaultSchema() { - return { - type: 'tpl', - tpl: - "<% if (data.image) { %> " + - "
)\" class=\"image <%= data.imageClassName %>\">
" + - "<% if (data.title) { %> " + - "
\"><%= data.title %>
" + - "<% } if (data.description) { %> " + - "
\"><%= data.description %>
" + - "<% } %>" + - "<% } else if (data.html) { %>" + - "<%= data.html %>" + - "<% } %>" - } - } - @autobind handleMouseEnter() { this.setState({ @@ -223,8 +213,7 @@ export class Carousel extends React.Component { height, controls, controlsTheme, - placeholder, - data + placeholder } = this.props; const { options, @@ -232,6 +221,20 @@ export class Carousel extends React.Component { current, nextAnimation } = this.state; + const defaultSchema = { + type: 'tpl', + tpl: + "<% if (data.image) { %> " + + "
)\" class=\"image <%= data.imageClassName %>\">
" + + "<% if (data.title) { %> " + + "
\"><%= data.title %>
" + + "<% } if (data.description) { %> " + + "
\"><%= data.description %>
" + + "<% } %>" + + "<% } else if (data.item) { %>" + + "<%= data.item %>" + + "<% } %>" + } let body:JSX.Element | null = null; let carouselStyles: { @@ -266,8 +269,8 @@ export class Carousel extends React.Component { return (
- {render(`${current}/body`, itemSchema ? itemSchema : this.defaultSchema(), { - data: createObject(data, option) + {render(`${current}/body`, itemSchema ? itemSchema : defaultSchema, { + data: isObject(option) ? option : {item: option} })}
);