sle*_*mon 4 javascript datepicker reactjs
我在我的应用程序中实现了 react-datepicker。
一切都很好,除了我想自定义日期选择器的输入字段并使其适应我的其他自定义字段,如标题输入。
当我通过
customInput={<Input />}
Run Code Online (Sandbox Code Playgroud)
字段,它的外观已经改变,但我不能再选择日期(选择器不再工作)。
继承人我的代码:
<DatePicker
customInput={<Input />}
dateFormat="DD.MM.YYYY"
selected=...
onChange=...
/>
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
export namespace Input {
export interface Props {
onChange: (event: any) => void;
placeholder?: string;
value?: string | number;
isSecure?: any;
id?: string;
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
所以我通过以下方式添加了时钟事件:
export namespace Input {
export interface Props {
onChange: (event: any) => void;
placeholder?: string;
value?: string | number;
isSecure?: any;
id?: string;
onClick: (event: any) => void;
}
}
Run Code Online (Sandbox Code Playgroud)
那正确吗?
组件代码:
export class Input extends React.Component<Input.Props, Input.State> {
public render() {
const controlClass = classNames(
[style.control]
);
const inputClass = classNames(
[style.input]
);
return (
<p className={controlClass} >
<input
id={this.props.id}
onChange={this.props.onChange}
className={inputClass}
type={this.props.isSecure ? "password" : "text"}
placeholder={this.props.placeholder}
value={this.props.value}
/>
</p>
);
}
}
Run Code Online (Sandbox Code Playgroud)
tri*_*ixn 10
您的Input组件需要实现一个onClick事件并将其作为道具提供,因为这会触发日期选择器自行打开。
const Input = ({onChange, placeholder, value, isSecure, id, onClick}) => (
<input
onChange={onChange}
placeholder={placeholder}
value={value}
isSecure={isSecure}
id={id}
onClick={onClick}
/>
);
const NoClickInput = ({onClick, ...props}) => <Input {...props} />;
class App extends Component {
state = {
value: moment(),
};
render() {
return (
<div>
<DatePicker
value={this.state.value}
dateFormat="DD.MM.YYYY"
customInput={<Input />}
selected={this.state.date}
onChange={date => this.setState({date})}
/>
<DatePicker
value={this.state.value}
dateFormat="DD.MM.YYYY"
customInput={<NoClickInput />} {/* Will not work */}
selected={this.state.date}
onChange={date => this.setState({date})}
/>
</div>
);
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:
在不触及Input组件实现的情况下,可能的解决方法是将其包装到容器中并处理对其的点击:
const ClickableInput = ({onClick, ...props}) => (
<div onClick={onClick}>
<Input {...props}>
</div>
);
Run Code Online (Sandbox Code Playgroud)
然后使用ClickableInput而不是Input作为您的自定义输入。
| 归档时间: |
|
| 查看次数: |
18268 次 |
| 最近记录: |