Browse Source

custom render demo

pull/520/head
afc163 9 years ago
parent
commit
4b4b2dbe36
  1. 23
      components/calendar/demo/custom-render.md
  2. 79
      components/calendar/index.jsx
  3. 1
      components/calendar/index.md

23
components/calendar/demo/custom-render.md

@ -0,0 +1,23 @@
# 自定义渲染
- order: 2
`dateCellRender``monthCellRender` 函数来自定义需要渲染的数据。
---
````jsx
import { Calendar } from 'antd';
function dateCellRender(value) {
return <span>date</span>;
}
function monthCellRender(value) {
return <span>month</span>;
}
ReactDOM.render(
<Calendar type="date" dateCellRender={dateCellRender} monthCellRender={monthCellRender} />
, document.getElementById('components-calendar-demo-custom-render'));
````

79
components/calendar/index.jsx

@ -38,40 +38,69 @@ class NoticeCalendar extends Component {
} }
monthCellRender(value, locale) { monthCellRender(value, locale) {
const prefixCls = this.props.prefixCls; const prefixCls = this.props.prefixCls;
const render = this.props.monthCellRender;
let content;
if (render) {
content = <div className={`${prefixCls}-fullscreen-month`}>
{render(value)}
</div>;
} else {
const month = value.getMonth(); const month = value.getMonth();
const noteNum = this.props.getMonthData(value); const noteNum = this.props.getMonthData(value);
if (noteNum > 0) { if (noteNum > 0) {
return ( content = (
<a className={`${prefixCls}-fullscreen-month`}> <div className={`${prefixCls}-fullscreen-month`}>
<span>{locale.format.shortMonths[month]}</span> <span>{locale.format.shortMonths[month]}</span>
<MonthCellNoteNum num={noteNum} prefixCls={`${prefixCls}-notes`} /> <MonthCellNoteNum num={noteNum} prefixCls={`${prefixCls}-notes`} />
</a> </div>
); );
} else {
content = <div className={`${prefixCls}-fullscreen-month`}>
{locale.format.shortMonths[month]}
</div>;
} }
return ( }
<a className={`${prefixCls}-fullscreen-month`}>{locale.format.shortMonths[month]}</a> return content;
);
} }
fullscreenDateCellRender(value) { fullscreenDateCellRender(value) {
const prefixCls = this.props.prefixCls; const prefixCls = this.props.prefixCls;
let listData = this.props.getDateData(value); const render = this.props.dateCellRender;
return ( let content;
<span className={`${prefixCls}-fullscreen-date`}> if (render) {
content = <span className={`${prefixCls}-fullscreen-date`}>
{render(value)}
</span>;
} else {
content = <span className={`${prefixCls}-fullscreen-date`}>
<span>{ zerofixed(value.getDayOfMonth()) }</span> <span>{ zerofixed(value.getDayOfMonth()) }</span>
<div className={`${prefixCls}-note-list-wrapper`}><NoteList listData={listData} /></div> <div className={`${prefixCls}-note-list-wrapper`}>
</span> <NoteList listData={this.props.getDateData(value)} />
); </div>
</span>;
}
return content;
} }
dateCellRender(value) { dateCellRender(value) {
const prefixCls = this.props.prefixCls; const prefixCls = this.props.prefixCls;
const el = (<span className={`${prefixCls}-date ${prefixCls}-notes-date`}>{ zerofixed(value.getDayOfMonth()) }</span>); const render = this.props.dateCellRender;
let content;
if (render) {
content = <div style={{ position: 'relative' }}>
{render(value)}
</div>;
} else {
const listData = this.props.getDateData(value); const listData = this.props.getDateData(value);
return ( content = <div style={{ position: 'relative' }}>
<div style={{ position: 'relative' }}> <span className={`${prefixCls}-date ${prefixCls}-notes-date`}>
{ el } {zerofixed(value.getDayOfMonth())}
{ (listData && listData.length > 0) ? <div className={`${prefixCls}-notes-wrapper`}><Notes listData={listData} /></div> : null } </span>
</div> {(listData && listData.length > 0) ?
); <div className={`${prefixCls}-notes-wrapper`}>
<Notes listData={listData} />
</div> : null}
</div>;
}
return content;
} }
setValue(value) { setValue(value) {
if (this.state.value !== value) { if (this.state.value !== value) {
@ -93,11 +122,10 @@ class NoticeCalendar extends Component {
render() { render() {
const props = this.props; const props = this.props;
const {value, type} = this.state; const {value, type} = this.state;
const {locale, prefixCls, style, className, fullscreen, monthCellRender, dateCellRender, fullscreenDateCellRender} = props; const {locale, prefixCls, style, className, fullscreen} = props;
const dateCellRender = fullscreen
? this.fullscreenDateCellRender : this.dateCellRender;
const _monthCellRender = monthCellRender ? monthCellRender : this.monthCellRender;
const _dateCellRender = dateCellRender ? dateCellRender : this.dateCellRender;
const _fullscreenDateCellRender = fullscreenDateCellRender ? fullscreenDateCellRender : this.fullscreenDateCellRender;
return ( return (
<div className={prefixCls + '-wrapper' + (className ? ' ' + className : '') + (fullscreen ? ' ' + prefixCls + '-wrapper-fullscreen' : '' )} style={style}> <div className={prefixCls + '-wrapper' + (className ? ' ' + className : '') + (fullscreen ? ' ' + prefixCls + '-wrapper-fullscreen' : '' )} style={style}>
<Header <Header
@ -115,8 +143,8 @@ class NoticeCalendar extends Component {
showHeader={false} showHeader={false}
value={value} value={value}
onChange={this.onPanelChange.bind(this)} onChange={this.onPanelChange.bind(this)}
monthCellRender={ fullscreen ? _monthCellRender.bind(this) : null } monthCellRender={ this.monthCellRender.bind(this) }
dateCellRender={ fullscreen ? _fullscreenDateCellRender.bind(this) : _dateCellRender.bind(this) }/> dateCellRender={ dateCellRender.bind(this) } />
</div> </div>
); );
} }
@ -136,6 +164,7 @@ NoticeCalendar.propTypes = {
onChange: PropTypes.func, onChange: PropTypes.func,
onTypeChange: PropTypes.func, onTypeChange: PropTypes.func,
}; };
NoticeCalendar.defaultProps = { NoticeCalendar.defaultProps = {
locale: CalendarLocale, locale: CalendarLocale,
getMonthData: noop, getMonthData: noop,

1
components/calendar/index.md

@ -27,7 +27,6 @@
| getDateData | 获取日的显示数据 | function | 无 | | getDateData | 获取日的显示数据 | function | 无 |
| getMonthData | 获取月的显示数据 | function | 无 | | getMonthData | 获取月的显示数据 | function | 无 |
| dateCellRender | 自定义渲染日期单元格 | function | 无 | | dateCellRender | 自定义渲染日期单元格 | function | 无 |
| fullscreenDateCellRender | 自定义渲染日期单元格(全屏) | function | 无 |
| monthCellRender | 自定义渲染月单元格 | function | 无 | | monthCellRender | 自定义渲染月单元格 | function | 无 |
| locale | 国际化配置 | object | [默认配置](https://github.com/ant-design/ant-design/issues/424) | | locale | 国际化配置 | object | [默认配置](https://github.com/ant-design/ant-design/issues/424) |
| onChange | 日期改变 | bool | 无 | | onChange | 日期改变 | bool | 无 |

Loading…
Cancel
Save