Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Calendar): Calendar 组件添加仅显示有效时间组功能 #527

Merged
merged 2 commits into from
Apr 18, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ project.config.json
site/
docs/h5/

coverage
coverage

.idea
4 changes: 4 additions & 0 deletions src/components/calendar/body/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export default class AtCalendarBody extends Taro.Component<
constructor (props) {
super(...arguments)
const {
validDates,
marks,
format,
minDate,
Expand All @@ -62,6 +63,7 @@ export default class AtCalendarBody extends Taro.Component<
} = props

this.generateFunc = generateCalendarGroup({
validDates,
format,
minDate,
maxDate,
Expand Down Expand Up @@ -114,6 +116,7 @@ export default class AtCalendarBody extends Taro.Component<

componentWillReceiveProps (nextProps: Props) {
const {
validDates,
marks,
format,
minDate,
Expand All @@ -124,6 +127,7 @@ export default class AtCalendarBody extends Taro.Component<
} = nextProps

this.generateFunc = generateCalendarGroup({
validDates,
format,
minDate,
maxDate,
Expand Down
2 changes: 2 additions & 0 deletions src/components/calendar/body/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export type ListGroup = Array<Calendar.ListInfo<Calendar.Item>>
export interface Props {
format: string

validDates: Array<Calendar.ValidDate>

marks: Array<Calendar.Mark>

isSwiper: boolean
Expand Down
25 changes: 24 additions & 1 deletion src/components/calendar/common/plugins.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import dayjs from 'dayjs'
import _forEach from 'lodash/forEach'
import _isEmpty from 'lodash/isEmpty'

import Calendar from '../types'

Expand Down Expand Up @@ -100,9 +101,31 @@ export function handleDisabled (
!!(minDate && _value.isBefore(dayjsMinDate)) ||
!!(maxDate && _value.isAfter(dayjsMaxDate))

return item
}

export function handleValid (
args: PluginArg,
item: Calendar.Item
): Calendar.Item {
const { options } = args
const { _value } = item
const { validDates } = options

if (!_isEmpty(validDates)) {
let isInclude = false;
_forEach(validDates, date => { // 判断当前日期是否在有效时间组内
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这块逻辑可以优化一下 如果你追求性能 那么 isInclude 为 true 的时候 剩下的还需要判断吗? 如果你追求代码的简洁性 那用 Array.some 会不会更好呢?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

代码已经改用 Array.some 来提高代码简洁性,因并不是经常使用 JS,所以不知道有 Array.some 这个方法,学习了,感谢~

if (dayjs(date.value).startOf('day').isSame(_value)) {
isInclude = true
}
})

item.isDisabled = !isInclude
}

delete item._value

return item
}

export default [handleActive, handleMarks, handleDisabled]
export default [handleActive, handleMarks, handleDisabled, handleValid]
3 changes: 3 additions & 0 deletions src/components/calendar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import AtCalendarController from './controller/index'
import { DefaultProps, Props, State, PropsWithDefaults } from './interface'

const defaultProps: DefaultProps = {
validDates: [],
marks: [],
isSwiper: true,
hideArrow: false,
Expand Down Expand Up @@ -269,6 +270,7 @@ export default class AtCalendar extends Taro.Component<Props, Readonly<State>> {
render () {
const { generateDate, selectedDate } = this.state
const {
validDates,
marks,
format,
minDate,
Expand All @@ -294,6 +296,7 @@ export default class AtCalendar extends Taro.Component<Props, Readonly<State>> {
onSelectDate={this.handleSelectDate}
/>
<AtCalendarBody
validDates={validDates}
marks={marks}
format={format}
minDate={minDate}
Expand Down
2 changes: 2 additions & 0 deletions src/components/calendar/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ export interface DefaultProps {

isSwiper: boolean

validDates: Array<Calendar.ValidDate>

marks: Array<Calendar.Mark>

currentDate: Calendar.DateArg | Calendar.SelectedDate
Expand Down
6 changes: 6 additions & 0 deletions src/components/calendar/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ declare namespace Calendar {
value: DateArg
}

export interface ValidDate {
value: DateArg
}

export interface Item {
value: string

Expand Down Expand Up @@ -43,6 +47,8 @@ declare namespace Calendar {
}

export interface GroupOptions {
validDates: Array<ValidDate>

marks: Array<Mark>

format: string
Expand Down
23 changes: 22 additions & 1 deletion src/pages/advanced/calendar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ export default class Index extends Component {
{
value: '2018/11/11'
}
],
validDates: [
{
value: '2019/04/17'
},
{
value: '2019/04/21'
},
{
value: '2019/05/04'
},
{
value: '2019/05/28'
}
]
}

Expand Down Expand Up @@ -67,7 +81,7 @@ export default class Index extends Component {
}

render () {
const { now, minDate, maxDate, mark, multiCurentDate } = this.state
const { now, minDate, maxDate, mark, multiCurentDate, validDates } = this.state
return (
<View className='page calendar-page'>
<DocsHeader title='Calendar 日历' />
Expand Down Expand Up @@ -178,6 +192,13 @@ export default class Index extends Component {
</View>
</View>
</View>

<View className='panel'>
<View className='panel__title'>有效时间组</View>
<View className='panel__content'>
<AtCalendar validDates={validDates}/>
</View>
</View>
</View>
</View>
)
Expand Down