All files / src/fieldcontrols/DatePicker DatePicker.tsx

40% Statements 8/20
0% Branches 0/14
16.67% Functions 1/6
33.33% Lines 6/18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103        1x         1x                     1x                                       1x                             1x                                                                                             1x
/**
 * @module FieldControls
 * 
 */ /** */
import * as React from 'react'
import { FieldSettings } from 'sn-client-js'
import { IClientFieldSetting, IReactClientFieldSetting } from '../IClientFieldSetting'
import { IDateTimeFieldSetting } from '../IDateTimeFieldSetting'
 
import { Input } from 'react-materialize'
import { styles } from './DatePickerStyles'
 
/**
 * Interface for DatePicker properties
 */
export interface DatePickerProps extends IReactClientFieldSetting, IClientFieldSetting, IDateTimeFieldSetting { }
 
/**
 * Field control that represents a Date field. Available values will be populated from the FieldSettings.
 */
export class DatePicker extends React.Component<DatePickerProps, { value }> {
    /**
     * constructor
     * @param {object} props
     */
    constructor(props: DatePickerProps) {
        super(props);
        /**
         * @type {object}
         * @property {string} value default value
         */
        this.state = {
            value: this.props['data-fieldValue'] ? this.setValue(this.props['data-fieldValue']) : this.setValue(this.props['data-defaultValue'])
        };
    }
 
    /**
     * convert string to proper date format
     * @param {string} value
     */
    setValue(value) {
        //TODO: check datetimemode and return a value based on this property
        let date = '';
        if (value) {
            date = value.split('T')[0]
        }
        else {
            date = new Date().toISOString().split('T')[0]
        }
        return date
    }
    /**
     * render
     * @return {ReactElement} markup
     */
    render() {
        switch (this.props['data-actionName']) {
            case 'edit':
                return (
                    <Input
                        name='on'
                        type='date'
                        label={
                            this.props.required
                                ? this.props['data-labelText'] + ' *'
                                : this.props['data-labelText']
                        }
                        defaultValue={this.state.value}
                        readOnly={this.props.readOnly}
                        onChange={function (e, value) { }}
                        s={12} />
                )
            case 'new':
                return (
                    <Input
                        name='on'
                        type='date'
                        label={
                            this.props.required
                                ? this.props['data-labelText'] + ' *'
                                : this.props['data-labelText']
                        }
                        defaultValue={this.state.value}
                        readOnly={this.props.readOnly}
                        onChange={function (e, value) { }}
                        s={12} />
                )
            case 'browse':
                return (
                    <div>
                        <label>
                            {this.props['data-labelText']}
                        </label>
                        <p>
                            {this.props['data-fieldValue']}
                        </p>
                    </div>
                )
            default:
                break;
        }
    }
}