我有一个动态输入,可以添加和删除带有输入的行,还有用于timepicker的material-ui输入,当我单击它时,带有输入的带有时钟图标的时钟就会出现。但是此输入的值不能随时钟改变。
我的代码是:
import { TimePicker } from "material-ui-time-picker";
import {
Input as Time,
Dialog as Clock,
DialogActions,
Button as ButtonOk
} from "@material-ui/core";
constructor(props) {
super(props);
this.state = {
isOpenS: false,
isOpenE: false,
start: moment().format("HH:MM"),
end: moment().format("HH:MM"),
tranches: [
{ start: moment().format("HH:MM"), end: moment().format("HH:MM") }
]
};
this.ajouterTranche = this.ajouterTranche.bind(this);
this.supprimerTranche = this.supprimerTranche.bind(this);
this.handleKeyboardStartChange = this.handleKeyboardStartChange.bind(this);
}
openDialogS = () => this.setState({ isOpenS: true });
closeDialogS = () => this.setState({ isOpenS: false });
backdropClickS = () => this.setState({ isOpenS: false });
handleDialogStartChange = (i, newValue) => {
const hours = newValue
.getHours()
.toString()
.padStart(2, "0");
const minutes = newValue
.getMinutes()
.toString()
.padStart(2, "0");
const textValue = hours + ":" + minutes;
// this.setState({ start: textValue });
this.state.tranches[i] = Object.assign({}, this.state.tranches[i], {
start: textValue
});
this.setState({
tranches: this.state.tranches
});
};
handleKeyboardStartChange = (i, event) => {
const rowDataCopy = this.state.tranches.slice(0);
rowDataCopy[i] = Object.assign({}, rowDataCopy[i], {
start: event.target.value
});
this.setState({
tranches: rowDataCopy
});
};
createDateFromTextValue = (i, value) => {
const splitParts = value.split(":");
return new Date(1970, 1, 1, splitParts[0], splitParts[1]);
};
openDialogE = () => this.setState({ isOpenE: true });
closeDialogE = () => this.setState({ isOpenE: false });
handleDialogEndChange = newValue => {
const hours = newValue
.getHours()
.toString()
.padStart(2, "0");
const minutes = newValue
.getMinutes()
.toString()
.padStart(2, "0");
const textValue = hours + ":" + minutes;
this.setState({ end: textValue });
};
handleKeyboardEndChange = (i, event) => {
// On va copier le tableau de tranches
const rowDataCopy = this.state.tranches.slice(0);
// On va jouter cette valeur changée au tableau de tranches
rowDataCopy[i] = Object.assign({}, rowDataCopy[i], {
end: event.target.value
});
this.setState({
tranches: rowDataCopy
});
};
createDateFromTextValue = value => {
const splitParts = value.split(":");
return new Date(1970, 1, 1, splitParts[0], splitParts[1]);
};
ajouterTranche = () => {
this.setState(prevState => ({
tranches: [...prevState.tranches, ""]
}));
};
supprimerTranche = idx => () => {
const rowDataCopy = this.state.tranches.slice(0);
rowDataCopy.splice(1, 1);
this.setState({
tranches: rowDataCopy
});
};
render() {
console.log(this.state.start);
return (
<div>
{this.state.tranches.map((el, i) => (
<Row key={i}>
<Col span={12} />
<Col span={12}>
<Row>
<Col span={8}>
<label className="pt-label .modifier">
<strong>Heure de début</strong>
</label>
<br />
<Time
value={el.start}
onChange={time => this.handleKeyboardStartChange(i, time)}
style={heure}
disableUnderline={true}
inputComponent={TextMaskCustom}
endAdornment={
<InputAdornment position="end" style={{ opacity: "0.4" }}>
<IconButton onClick={this.openDialogS}>
<AccessTime />
</IconButton>
</InputAdornment>
}
/>
<Clock
maxWidth="xs"
open={this.state.isOpenS}
onBackdropClick={this.closeDialogS}
>
<TimePicker
mode="24h"
value={this.createDateFromTextValue(this.state.start)}
onChange={time => this.handleDialogStartChange(i, time)}
/>
<DialogActions>
<ButtonOk onClick={this.closeDialogS} color="primary">
Ok
</ButtonOk>
</DialogActions>
</Clock>
<br />
</Col>
<Col span={8}>
<label className="pt-label .modifier">
<strong>Heure de fin</strong>
</label>
<br />
<Time
value={el.end}
onChange={time => this.handleKeyboardEndChange(i, time)}
style={heure}
disableUnderline={true}
inputComponent={TextMaskCustom}
endAdornment={
<InputAdornment position="end" style={{ opacity: "0.4" }}>
<IconButton onClick={this.openDialogS}>
<AccessTime />
</IconButton>
</InputAdornment>
}
/>
<Clock
maxWidth="xs"
open={this.state.isOpenE}
onBackdropClick={this.closeDialogE}
>
<TimePicker
mode="24h"
value={this.createDateFromTextValue(this.state.end)}
onChange={this.handleDialogEndChange}
/>
<DialogActions>
<ButtonOk onClick={this.closeDialogE} color="primary">
Ok
</ButtonOk>
</DialogActions>
</Clock>
<br />
</Col>
<Col span={8}>
{i === 0 ? (
<>
<br />
<Icon
type="plus-circle"
theme="twoTone"
twoToneColor="#52c41a"
onClick={this.ajouterTranche}
/>
<br />
</>
) : (
<>
<Icon
type="close-circle"
theme="twoTone"
twoToneColor="red"
onClick={this.supprimerTranche(i)}
/>
<Icon
type="plus-circle"
theme="twoTone"
twoToneColor="#52c41a"
onClick={this.ajouterTranche}
/>
<br />
</>
)}
</Col>
</Row>
</Col>
</Row>
))}
</div>
);
}
}
我的沙盒代码为:https://codesandbox.io/s/182oy5995l
当我在输入上输入值并单击时钟时,得到的是时钟的力矩值,而不是输入到输入上的值。我想要从时钟更改时间时,输入将更改,反之亦然。
我该如何解决?
答案 0 :(得分:1)
对于输入和时钟的值属性,应使用相同的变量。 试试这个
更改
value={this.createDateFromTextValue(this.state.start)}
到
value={this.createDateFromTextValue(el.start)}
终端输入和时钟也一样。
为什么要为时钟传递“ el.start”,因为我们正在渲染时钟并在循环中输入,而“ el”是map function的currentValue。对于输入和时钟,它将始终保持相同的值。 而且,它将完美地适用于多个时钟和输入