Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functionality to display whitespaces accordingly in normal(non-editing) mode #60

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
node_modules
lib
.idea
*.log
.DS_Store
.DS_Store
88 changes: 43 additions & 45 deletions demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -19114,45 +19114,43 @@
var warning = emptyFunction;

if (process.env.NODE_ENV !== 'production') {
(function () {
var printWarning = function printWarning(format) {
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}

var argIndex = 0;
var message = 'Warning: ' + format.replace(/%s/g, function () {
return args[argIndex++];
});
if (typeof console !== 'undefined') {
console.error(message);
}
try {
// --- Welcome to debugging React ---
// This error was thrown as a convenience so that you can use this stack
// to find the callsite that caused this warning to fire.
throw new Error(message);
} catch (x) {}
};
var printWarning = function printWarning(format) {
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}

warning = function warning(condition, format) {
if (format === undefined) {
throw new Error('`warning(condition, format, ...args)` requires a warning ' + 'message argument');
}
var argIndex = 0;
var message = 'Warning: ' + format.replace(/%s/g, function () {
return args[argIndex++];
});
if (typeof console !== 'undefined') {
console.error(message);
}
try {
// --- Welcome to debugging React ---
// This error was thrown as a convenience so that you can use this stack
// to find the callsite that caused this warning to fire.
throw new Error(message);
} catch (x) {}
};

if (format.indexOf('Failed Composite propType: ') === 0) {
return; // Ignore CompositeComponent proptype check.
}
warning = function warning(condition, format) {
if (format === undefined) {
throw new Error('`warning(condition, format, ...args)` requires a warning ' + 'message argument');
}

if (!condition) {
for (var _len2 = arguments.length, args = Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) {
args[_key2 - 2] = arguments[_key2];
}
if (format.indexOf('Failed Composite propType: ') === 0) {
return; // Ignore CompositeComponent proptype check.
}

printWarning.apply(undefined, [format].concat(args));
if (!condition) {
for (var _len2 = arguments.length, args = Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) {
args[_key2 - 2] = arguments[_key2];
}
};
})();

printWarning.apply(undefined, [format].concat(args));
}
};
}

module.exports = warning;
Expand Down Expand Up @@ -20225,28 +20223,28 @@
}, _this.props.editProps));
}, _this.renderNormalComponent = function () {
var value = _this.state.newValue || _this.props.value;
var spans_and_brs = [];
var spans_and_brs_and_whitespaces = [];
var i = 0;
value.split("\n").map(function (line) {
spans_and_brs.push(_react2.default.createElement(
spans_and_brs_and_whitespaces.push(_react2.default.createElement(
'span',
{ key: i },
line
line.replace(/ /gi, '\xA0')
));
spans_and_brs.push(_react2.default.createElement('br', { key: i + 1 }));
spans_and_brs_and_whitespaces.push(_react2.default.createElement('br', { key: i + 1 }));
i += 2;
});
spans_and_brs.pop // remove last br tag
spans_and_brs_and_whitespaces.pop(); // remove last br tag

();return _react2.default.createElement(
return _react2.default.createElement(
'span',
_extends({
tabIndex: '0',
className: _this.makeClassString(),
onFocus: _this.startEditing,
onClick: _this.startEditing
}, _this.props.defaultProps),
spans_and_brs
spans_and_brs_and_whitespaces
);
}, _temp), _possibleConstructorReturn(_this, _ret);
}
Expand Down Expand Up @@ -20304,20 +20302,20 @@
};

_this.selectInputText = function (element) {
debug('selectInputText(' + element + ')'
debug('selectInputText(' + element + ')');
// element.setSelectionRange won't work for an input of type "number"
);setTimeout(function () {
setTimeout(function () {
element.select();
}, 10);
};

_this.elementBlur = function (element) {
debug('elementBlur(' + element + ')'
debug('elementBlur(' + element + ')');
/*
Firefox workaround
Found at https://tirdadc.github.io/blog/2015/06/11/react-dot-js-firefox-issue-with-onblur/
*/
);if (element.nativeEvent.explicitOriginalTarget && element.nativeEvent.explicitOriginalTarget == element.nativeEvent.originalTarget) {
if (element.nativeEvent.explicitOriginalTarget && element.nativeEvent.explicitOriginalTarget == element.nativeEvent.originalTarget) {
return;
}
_this.finishEditing();
Expand Down
130 changes: 130 additions & 0 deletions lib/RIEBase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var debug = require('debug')('RIEBase');

var RIEBase = function (_React$Component) {
_inherits(RIEBase, _React$Component);

function RIEBase(props) {
_classCallCheck(this, RIEBase);

var _this = _possibleConstructorReturn(this, (RIEBase.__proto__ || Object.getPrototypeOf(RIEBase)).call(this, props));

_this.doValidations = function (value) {
debug('doValidations(' + value + ')');
var isValid = void 0;
if (_this.props.validate) {
isValid = _this.props.validate(value);
} else if (_this.validate) {
isValid = _this.validate(value);
} else return true;
_this.setState({ invalid: !isValid });
return isValid;
};

_this.selectInputText = function (element) {
debug('selectInputText(' + element.value + ')');
if (element.setSelectionRange) element.setSelectionRange(0, element.value.length);
};

_this.elementClick = function (event) {
throw "RIEBase must be subclassed first: use a concrete class like RIEInput, RIEToggle et.c";
};

_this.componentWillReceiveProps = function (nextProps) {
debug('componentWillReceiveProps(' + nextProps + ')');
if ('value' in nextProps && !(nextProps.shouldRemainWhileInvalid && _this.state.invalid)) {
_this.setState({ loading: false, editing: false, invalid: false, newValue: null });
}
};

_this.commit = function (value) {
debug('commit(' + value + ')');
if (!_this.state.invalid) {
var newProp = {};
newProp[_this.props.propName] = value;
_this.setState({ loading: true, newValue: value });
_this.props.change(newProp);
}
};

_this.makeClassString = function () {
debug('makeClassString()');
var classNames = [];
if (_this.props.className) classNames.push(_this.props.className);
if (_this.state.editing && _this.props.classEditing) classNames.push(_this.props.classEditing);
if (_this.state.loading && _this.props.classLoading) classNames.push(_this.props.classLoading);
if (_this.state.disabled && _this.props.classDisabled) classNames.push(_this.props.classDisabled);
if (_this.state.invalid && _this.props.classInvalid) classNames.push(_this.props.classInvalid);
return classNames.join(' ');
};

_this.render = function () {
debuf('render()');
return _react2.default.createElement(
'span',
_extends({}, _this.props.defaultProps, { tabindex: '0', className: _this.makeClassString(), onClick: _this.elementClick }),
_this.props.value
);
};

if (!_this.props.propName) throw "RTFM: missing 'propName' prop";
if (!_this.props.change) throw "RTFM: missing 'change' prop";
if (typeof _this.props.value == 'undefined') throw "RTFM: missing 'value' prop";

_this.state = {
editing: false,
loading: false,
disabled: false,
invalid: false
};
return _this;
}

return RIEBase;
}(_react2.default.Component);

RIEBase.propTypes = {
value: _propTypes2.default.any.isRequired,
change: _propTypes2.default.func.isRequired,
propName: _propTypes2.default.string.isRequired,
editProps: _propTypes2.default.object,
defaultProps: _propTypes2.default.object,
isDisabled: _propTypes2.default.bool,
validate: _propTypes2.default.func,
handleValidationFail: _propTypes2.default.func,
shouldBlockWhileLoading: _propTypes2.default.bool,
shouldRemainWhileInvalid: _propTypes2.default.bool,
classLoading: _propTypes2.default.string,
classEditing: _propTypes2.default.string,
classDisabled: _propTypes2.default.string,
classInvalid: _propTypes2.default.string,
className: _propTypes2.default.string,
beforeStart: _propTypes2.default.func,
afterStart: _propTypes2.default.func,
beforeFinish: _propTypes2.default.func,
afterFinish: _propTypes2.default.func
};
exports.default = RIEBase;
100 changes: 100 additions & 0 deletions lib/RIENumber.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _RIEStatefulBase2 = require('./RIEStatefulBase');

var _RIEStatefulBase3 = _interopRequireDefault(_RIEStatefulBase2);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var debug = require('debug')('RIENumber');

var RIENumber = function (_RIEStatefulBase) {
_inherits(RIENumber, _RIEStatefulBase);

function RIENumber(props) {
_classCallCheck(this, RIENumber);

var _this = _possibleConstructorReturn(this, (RIENumber.__proto__ || Object.getPrototypeOf(RIENumber)).call(this, props));

_this.validate = function (value) {
debug('validate(' + value + ')');
return !isNaN(value) && isFinite(value) && value.length > 0;
};

_this.selectInputText = function (element) {
debug('selectInputText(' + element + ')');
// element.setSelectionRange won't work for an input of type "number"
setTimeout(function () {
element.select();
}, 10);
};

_this.elementBlur = function (element) {
debug('elementBlur(' + element + ')');
/*
Firefox workaround
Found at https://tirdadc.github.io/blog/2015/06/11/react-dot-js-firefox-issue-with-onblur/
*/
if (element.nativeEvent.explicitOriginalTarget && element.nativeEvent.explicitOriginalTarget == element.nativeEvent.originalTarget) {
return;
}
_this.finishEditing();
};

_this.renderNormalComponent = function () {
debug('renderNormalComponent()');
return _react2.default.createElement(
'span',
_extends({
tabIndex: '0',
className: _this.makeClassString(),
onFocus: _this.startEditing,
onClick: _this.elementClick
}, _this.props.defaultProps),
_this.props.format ? _this.props.format(_this.state.newValue || _this.props.value) : _this.state.newValue || _this.props.value
);
};

_this.renderEditingComponent = function () {
debug('renderEditingComponent()');
return _react2.default.createElement('input', _extends({ disabled: _this.props.shouldBlockWhileLoading && _this.state.loading,
type: 'number',
className: _this.makeClassString(),
defaultValue: _this.props.value,
onInput: _this.textChanged,
onBlur: _this.elementBlur,
ref: 'input',
onKeyDown: _this.keyDown
}, _this.props.editProps));
};

return _this;
}

return RIENumber;
}(_RIEStatefulBase3.default);

RIENumber.propTypes = {
format: _propTypes2.default.func
};
exports.default = RIENumber;
Loading