Skip to content

Commit e86c7d9

Browse files
author
Michael Braun
committed
Merge branch 'feature/update-formatted-input' into master
2 parents ba5e5c4 + 325e2ee commit e86c7d9

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

src/react-chayns-formatted_input/component/FormattedInput.jsx

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,29 @@ export default class FormattedInput extends Component {
8686
this.handleChangeEvent(parsedValue, ...args);
8787
};
8888

89+
handleEnter = (value, ...args) => {
90+
const { onEnter } = this.props;
91+
if (onEnter) {
92+
const { formatter } = this;
93+
94+
if (!(formatter instanceof Formatter)) {
95+
return;
96+
}
97+
98+
const parsedValue = formatter.parse(value);
99+
100+
onEnter(parsedValue, ...args);
101+
}
102+
};
103+
89104
render() {
90105
const { value } = this.state;
91-
const { defaultValue, initialFormatter, ...props } = this.props;
106+
const {
107+
defaultValue,
108+
initialFormatter,
109+
inputRef,
110+
...props
111+
} = this.props;
92112

93113
if (!(initialFormatter instanceof Formatter)) {
94114
return null;
@@ -99,10 +119,15 @@ export default class FormattedInput extends Component {
99119
{...props}
100120
inputRef={(ref) => {
101121
this.input = ref;
122+
123+
if (inputRef) {
124+
inputRef(ref);
125+
}
102126
}}
103127
value={value}
104128
onChange={this.handleInputChange}
105129
onBlur={this.handleChange}
130+
onEnter={this.handleEnter}
106131
/>
107132
);
108133
}
@@ -111,13 +136,17 @@ export default class FormattedInput extends Component {
111136
FormattedInput.propTypes = {
112137
initialFormatter: PropTypes.instanceOf(Formatter).isRequired,
113138
onChange: PropTypes.func,
139+
onEnter: PropTypes.func,
140+
inputRef: PropTypes.func,
114141
// eslint-disable-next-line react/forbid-prop-types
115142
defaultValue: PropTypes.any,
116143
};
117144

118145
FormattedInput.defaultProps = {
119146
onChange: null,
147+
onEnter: null,
120148
defaultValue: null,
149+
inputRef: null,
121150
};
122151

123152
FormattedInput.displayName = 'FormattedInput';

src/react-chayns-formatted_input/utils/IntegerFormatter.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ export default class IntegerFormatter extends Formatter {
55

66
/* eslint-disable-next-line class-methods-use-this */
77
format(value) {
8-
if (value === null) {
8+
const parsed = this.parse(value);
9+
if (!parsed && parsed !== 0) {
910
return '';
1011
}
1112

12-
return String(parseInt(value, 10));
13+
return String(parsed);
1314
}
1415

1516
/* eslint-disable-next-line class-methods-use-this */

0 commit comments

Comments
 (0)