# all files / lib/components/TextField/ TextField.js

87.61% Statements 99/113
68.42% Branches 39/57
78.57% Functions 22/28
88.89% Lines 96/108

    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
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    1×
    1×
    1×
    1×

    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×
    11×
    11×
    11×
    11×
    11×

    11×
    11×
    11×
    11×
    11×

    1×

    1×
    11×
    11×

    1×
    11×

    1×
    1×
    1×
    1×
    1×

    1×

    1×

    1×

    1×
    19×
    19×
    19×

    19×

    1×

    1×

    1×

    1×

    1×

    19×
    19×

    1×

    38×
    38×
    22×

    38×

    1×
    1×
    1×

    1×
    18×
    26×

    1×
    2×
    2×
    2×

    2×
    2×
    2×
    2×

    1×
    14×

    14×

    14×
    14×
    14×
    14×
    10×
    7×

    7×

    3×
    3×
    3×
    3×

    3×

    4×

    1×
    14×
    3×
    3×
    3×
    1×
    1×

    11×

    1×

    4×

    1×

    1×

     "use strict";
    var __extends = (this && this.__extends) || function (d, b) {
        for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
    var React = require('react');
    var Label_1 = require('../../Label');
    var css_1 = require('../../utilities/css');
    var Async_1 = require('../../utilities/Async/Async');
    require('./TextField.scss');
    var _instance = 0;
    var TextField = (function (_super) {
        __extends(TextField, _super);
        function TextField(props) {
            _super.call(this, props);
            this._id = "TextField-" + _instance++;
            this._descriptionId = "TextFieldDescription-" + _instance++;
            this._async = new Async_1.Async(this);
            this.state = {
                value: props.value || props.defaultValue,
                errorMessage: ''
            };
            this._onInputChange = this._onInputChange.bind(this);
            this._delayedValidate = this._async.debounce(this._validate, this.props.deferredValidationTime);
            this._lastValidation = 0;
            this._latestValidateValue = '';
            this._willMountTriggerValidation = false;
        }
        Object.defineProperty(TextField.prototype, "value", {
            /**
             * Gets the current value of the text field.
             */
            get: function () {
                return this.state.value;
            },
            enumerable: true,
            configurable: true
        });
        TextField.prototype.componentWillMount = function () {
            this._willMountTriggerValidation = true;
            this._validate(this.state.value);
        };
        TextField.prototype.componentDidMount = function () {
            this._isMounted = true;
        };
        TextField.prototype.componentWillReceiveProps = function (newProps) {
            var onBeforeChange = this.props.onBeforeChange;
            Eif (newProps.value !== undefined && newProps.value !== this.state.value) {
                Eif (onBeforeChange) {
                    onBeforeChange(newProps.value);
                }
                this.setState({
                    value: newProps.value
                });
                this._delayedValidate(newProps.value);
            }
        };
        TextField.prototype.componentWillUnmount = function () {
            this._async.dispose();
            this._isMounted = false;
        };
        TextField.prototype.render = function () {
            var _a = this.props, disabled = _a.disabled, required = _a.required, multiline = _a.multiline, underlined = _a.underlined, label = _a.label, description = _a.description, iconClass = _a.iconClass, className = _a.className;
            var errorMessage = this._errorMessage;
            var textFieldClassName = css_1.css('ms-TextField', className, {
                'is-required': required,
                'is-disabled': disabled,
                'ms-TextField--multiline': multiline,
                'ms-TextField--underlined': underlined
            });
            return (React.createElement("div", {className: textFieldClassName}, label && React.createElement(Label_1.Label, {htmlFor: this._id}, label), iconClass && React.createElement("i", {className: iconClass}), multiline ? this._renderTextArea() : this._renderInput(), errorMessage && React.createElement("p", {className: 'ms-TextField-errorMessage ms-u-slideDownIn20'}, errorMessage), errorMessage && React.createElement("div", {"aria-live": 'assertive', className: 'ms-u-screenReaderOnly', "data-automation-id": 'error-message'}, errorMessage), description && React.createElement("span", {className: 'ms-TextField-description'}, description), this.props.ariaLabel && React.createElement("span", {id: this._descriptionId, className: 'ms-TextField-hidden'}, this.props.ariaLabel)));
        };
        /**
         * Sets focus on the text field
         */
        TextField.prototype.focus = function () {
            if (this._field) {
                this._field.focus();
            }
        };
        /**
         * Selects the text field
         */
        TextField.prototype.select = function () {
            if (this._field) {
                this._field.select();
            }
        };
        /**
         * Sets the selection start of the text field to a specified value
         */
        TextField.prototype.setSelectionStart = function (value) {
            if (this._field) {
                this._field.selectionStart = value;
            }
        };
        /**
         * Sets the selection end of the text field to a specified value
         */
        TextField.prototype.setSelectionEnd = function (value) {
            if (this._field) {
                this._field.selectionEnd = value;
            }
        };
        Object.defineProperty(TextField.prototype, "_fieldClassName", {
            get: function () {
                var errorMessage = this._errorMessage;
                return css_1.css('ms-TextField-field', {
                    'ms-TextField-invalid': !!errorMessage
                });
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(TextField.prototype, "_errorMessage", {
            get: function () {
                var errorMessage = this.state.errorMessage;
                if (!errorMessage) {
                    errorMessage = this.props.errorMessage;
                }
                return errorMessage;
            },
            enumerable: true,
            configurable: true
        });
        TextField.prototype._renderTextArea = function () {
            var _this = this;
            return (React.createElement("textarea", React.__spread({}, this.props, {id: this._id, ref: function (c) { return _this._field = c; }, value: this.state.value, onChange: this._onInputChange, className: this._fieldClassName})));
        };
        TextField.prototype._renderInput = function () {
            var _this = this;
            return (React.createElement("input", React.__spread({}, this.props, {id: this._id, type: 'text', ref: function (c) { return _this._field = c; }, value: this.state.value, onChange: this._onInputChange, className: this._fieldClassName, "aria-describedby": this._descriptionId})));
        };
        TextField.prototype._onInputChange = function (event) {
            var element = event.target;
            var value = element.value;
            this.setState({
                value: value,
                errorMessage: ''
            });
            this._willMountTriggerValidation = false;
            this._delayedValidate(value);
            var onBeforeChange = this.props.onBeforeChange;
            onBeforeChange(value);
        };
        TextField.prototype._validate = function (value) {
            var _this = this;
            // In case of _validate called multi-times during executing validate logic with promise return.
            Iif (this._latestValidateValue === value) {
                return;
            }
            this._latestValidateValue = value;
            var onGetErrorMessage = this.props.onGetErrorMessage;
            var result = onGetErrorMessage(value || '');
            if (result !== undefined) {
                if (typeof result === 'string') {
                    this.setState({
                        errorMessage: result
                    });
                    this._notifyAfterValidate(value, result);
                }
                else {
                    var currentValidation_1 = ++this._lastValidation;
                    result.then(function (errorMessage) {
                        Eif (_this._isMounted && currentValidation_1 === _this._lastValidation) {
                            _this.setState({ errorMessage: errorMessage });
                        }
                        _this._notifyAfterValidate(value, errorMessage);
                    });
                }
            }
            else {
                this._notifyAfterValidate(value, '');
            }
        };
        TextField.prototype._notifyAfterValidate = function (value, errorMessage) {
            if (!this._willMountTriggerValidation && value === this.state.value) {
                var onNotifyValidationResult = this.props.onNotifyValidationResult;
                onNotifyValidationResult(errorMessage, value);
                if (!errorMessage) {
                    var onChanged = this.props.onChanged;
                    onChanged(value);
                }
            }
            else {
                this._willMountTriggerValidation = false;
            }
        };
        TextField.defaultProps = {
            multiline: false,
            underlined: false,
            onChanged: function () { },
            onBeforeChange: function () { },
            onNotifyValidationResult: function () { },
            onGetErrorMessage: function () { return undefined; },
            deferredValidationTime: 200,
            errorMessage: ''
        };
        return TextField;
    }(React.Component));
    exports.TextField = TextField;

    //# sourceMappingURL=TextField.js.map