Mil*_*ind 5 javascript momentjs reactjs react-component
我正在尝试制作简单的日历。在我的日历中,我有日、日名、月和月名组件,我在主日历上访问它。初始加载时,它将显示当前月历。
现在,当我从月份名称组件中选择月份时,想要传递给月份组件,然后传递给日期组件以呈现日期。
我尝试使用回调功能,但它没有帮助我
整个反应组件在这里https://stackblitz.com/edit/react-ts-z2278r
Day component
import * as moment from 'moment'
import * as React from 'react'
import { showComponent } from '../../../../models'
import { MonthNames } from './'
import styles from './Calendar.module.scss'
interface IDatePickerMonthsProps {
changeMonthComponent: (showComponent: showComponent, monthNo: number) => void
}
interface IDatePickerMonthsState {
dateObject: moment.Moment,
monthNo?: number
}
export class DatePickerMonths extends React.Component<IDatePickerMonthsProps, IDatePickerMonthsState> {
constructor(props: IDatePickerMonthsProps) {
super(props)
this.state = {
dateObject: moment()
}
}
public render() {
const monthPicker =
<div className="datepicker-days">
<table className={styles.table}>
<thead>
<tr>
<th><span className="ms-Icon ms-Icon--ChevronLeft" title="Previous Month" onClick={this.onPrev}></span></th>
<th className={styles["picker-switch"]} data-action="pickerSwitch" colSpan={5} title="Select Month">
{this.year()}
</th>
<th><span className="ms-Icon ms-Icon--ChevronRight" title="Next Month" onClick={this.onNext}></span></th>
</tr>
</thead>
<tbody>
<tr>
<td colSpan={7}>
<MonthNames changeMonthComponent={() => this.showDays(this.state.monthNo)} />
</td>
</tr>
</tbody>
</table>
</div>
return (monthPicker)
}
private year = () => {
return this.state.dateObject.format("Y");
};
private onPrev = () => {
this.setState({
dateObject: this.state.dateObject.subtract(1, "year")
});
};
private onNext = () => {
this.setState({
dateObject: this.state.dateObject.add(1, "year")
});
};
private showDays = (monthNo: number) => {
console.log(monthNo)
this.setState({ monthNo: monthNo })
this.props.changeMonthComponent(showComponent.Days, monthNo)
}
}
Run Code Online (Sandbox Code Playgroud)
Month name component
import * as moment from 'moment'
import * as React from 'react'
import { showComponent } from '../../../../models'
interface IMonthNamesProps {
changeMonthComponent: (showComponent: showComponent, monthNo: number) => void
}
export class MonthNames extends React.Component<IMonthNamesProps, {}> {
private monthshort: string[] = moment.monthsShort();
constructor(props: IMonthNamesProps) {
super(props)
}
public render() {
let monthshortname = this.monthshort.map((month, monthNo) => {
return <span key={monthNo} onClick={() => this.showDays(monthNo)}>{month}</span>;
});
return monthshortname
}
private showDays = (monthNo: number) => {
this.props.changeMonthComponent(showComponent.Days, monthNo)
}
}
Run Code Online (Sandbox Code Playgroud)
Month component
import * as moment from 'moment'
import * as React from 'react'
import { showComponent } from '../../../../models'
import { MonthNames } from './'
import styles from './Calendar.module.scss'
interface IDatePickerMonthsProps {
changeMonthComponent: (showComponent: showComponent, monthNo: number) => void
}
interface IDatePickerMonthsState {
dateObject: moment.Moment,
monthNo?: number
}
export class DatePickerMonths extends React.Component<IDatePickerMonthsProps, IDatePickerMonthsState> {
constructor(props: IDatePickerMonthsProps) {
super(props)
this.state = {
dateObject: moment()
}
}
public render() {
const monthPicker =
<div className="datepicker-days">
<table className={styles.table}>
<thead>
<tr>
<th><span className="ms-Icon ms-Icon--ChevronLeft" title="Previous Month" onClick={this.onPrev}></span></th>
<th className={styles["picker-switch"]} data-action="pickerSwitch" colSpan={5} title="Select Month">
{this.year()}
</th>
<th><span className="ms-Icon ms-Icon--ChevronRight" title="Next Month" onClick={this.onNext}></span></th>
</tr>
</thead>
<tbody>
<tr>
<td colSpan={7}>
<MonthNames changeMonthComponent={() => this.showDays(this.state.monthNo)} />
</td>
</tr>
</tbody>
</table>
</div>
return (monthPicker)
}
private year = () => {
return this.state.dateObject.format("Y");
};
private onPrev = () => {
this.setState({
dateObject: this.state.dateObject.subtract(1, "year")
});
};
private onNext = () => {
this.setState({
dateObject: this.state.dateObject.add(1, "year")
});
};
private showDays = (monthNo: number) => {
console.log(monthNo)
this.setState({ monthNo: monthNo })
this.props.changeMonthComponent(showComponent.Days, monthNo)
}
}
Run Code Online (Sandbox Code Playgroud)
在**看得更清晰**的帮助下,我已经解决了我的问题,感谢支持和投票。
现在回到我的问题。我的日历最初加载正确,但是当我更改 MonthName 组件(即子组件)中的月份和年份时,向上传递到 DayPickerMonths (即父组件),然后向上传递到 Calendar 组件(即祖父组件),然后向下传递到 DatePickerDays 组件(即子组件) Calendar 的组件和 DayPickerMonths 的同级)
所以从孩子开始(即MonthName)
功能
private showDays = (monthNo: number) => {
this.props.changeMonthComponent(monthNo)
}
Run Code Online (Sandbox Code Playgroud)
调用渲染
public render() {
let monthshortname = this.monthshort.map((month, monthNo) => {
return <span key={monthNo} onClick={() => this.showDays(monthNo)}>{month}</span>;
});
return monthshortname
}
Run Code Online (Sandbox Code Playgroud)
DayPickerMonths(即父级)
<MonthNames changeMonthComponent={(monthNo) => this.showDays(monthNo, this.state.dateObject.year())} />
private showDays = (monthNo: number, year: number) => {
this.props.changeMonthComponent(showComponent.Days, monthNo, year)
}
Run Code Online (Sandbox Code Playgroud)
日历(即祖父母)
<DatePickerMonths
changeMonthComponent={(childData, monthNo, year) => this.changeMonthComponent(childData, monthNo, year)} />
private changeMonthComponent = (childData: showComponent, monthNo: number, year: number) => {
this.setState({ showComponent: childData, monthNo: monthNo, year: year })
}
Run Code Online (Sandbox Code Playgroud)
DayPickerDays(即日历的子级和 DayPicker 的同级)
private showMonths = () => {
this.props.changeComponent(showComponent.Months)
}
public componentDidMount() {
this.setState({ dateObject: this.state.dateObject.month(this.props.monthNo).year(this.props.year) })
}
Run Code Online (Sandbox Code Playgroud)
我已经在这里更新了我的https://stackblitz.com/edit/react-ts-z2278r
但仍然存在一个问题,当 DayPickerDays 组件中的年份发生变化时,它没有反映在 DayPickerMonths 中,并且传递数据很麻烦,我们应该使用React 上下文来使其全局且简单。一旦我成为提供者,我很快就会更新这个答案,因为我需要预约医生进行眼睛检查
| 归档时间: |
|
| 查看次数: |
390 次 |
| 最近记录: |