Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="container" style="padding: 24px"></div>
<script>
  var mountNode = document.getElementById('container');
</script>
              
            
!

CSS

              
                @import 'antd/dist/antd.css';


              
            
!

JS

              
                const { DatePicker } = antd;
const modeValue={
    date: 0,
    month: 1,
    year: 2,
    decade: 3
};

class MyDatePicker extends React.Component {
    static defaultProps={
        topMode: "month",
        defaultValue: moment(),
        value: moment(),
        format: "YYYY-MM-DD"
    };
    constructor(props) { 
        super(props);        
        this.state={
            value: this.props.value || this.props.defaultValue,
            mode: this.props.topMode,
            preMode: this.props.topMode
        };
        this.isOnChange = false;
    }
    componentWillReceiveProps(nextProps, nextContext){
        if(this.props.topMode != nextProps.topMode){
            this.setState({
                mode: nextProps.topMode
            });
        }
    }
    /**
     * 
     * @param {*} value 
     * @param {要打開面板} mode 
     */
    onPanelChange(value, mode){
        // console.log(`onPanelChange date:${value} mode:${mode}`);
        //mode==null默認是從year返回到month
        mode = mode || "month";
        let open = true;
        //1. 往上打開,沒有任何問題,直接打開
        if(modeValue[this.state.mode] > modeValue[mode] && modeValue[this.props.topMode] > modeValue[mode]) {
            //向下
            open = false;
            mode = this.props.topMode;
        }
        //只關閉窗口和賦值,當前的mode不變
        this.setState({
            value, open, mode,
            preMode: this.state.mode
        });
    }
    /**
     * 在date的情況下選擇直接退出
     */
    onChange(value, dateStr){
        // console.log(`onChange date:${value} dateStr:${dateStr}`);
        this.isOnChange = true;
        this.setState({
            open: false,
            value
        });
    }
    
    render() {
        // console.log(`state:${JSON.stringify(this.state)}`);
        return (
            <DatePicker 
                value={this.state.value} 
                mode={this.state.mode}
                open={this.state.open}
                format={this.props.format}
                onFocus={()=>!this.isOnChange&&(this.isOnChange=!this.isOnChange,this.setState({open:true}))}
                onChange={this.onChange.bind(this)}
                onPanelChange={this.onPanelChange.bind(this)}
                onOpenChange={(open)=>this.setState({open})}
            />
        );
    }

}
ReactDOM.render(
  <div>
    日:<MyDatePicker topMode="date" format="YYYY-MM-DD"/><br/>
    月:<MyDatePicker topMode="month" format="YYYY-MM"/><br/>
    年:<MyDatePicker topMode="year" format="YYYY"/>
  </div>,
  mountNode);
              
            
!
999px

Console