Skip to content

Commit

Permalink
Do not trim value on change do it on blur
Browse files Browse the repository at this point in the history
  • Loading branch information
Kiho committed Jul 10, 2024
1 parent 9a68f72 commit 7eaf761
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/form.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class ReactForm extends React.Component {
return defaultChecked;
}

_getItemValue(item, ref) {
_getItemValue(item, ref, trimValue) {
let $item = {
element: item.element,
value: '',
Expand All @@ -87,7 +87,7 @@ class ReactForm extends React.Component {
$item.value = ref.state.fileUpload;
} else if (ref && ref.inputField && ref.inputField.current) {
$item = ReactDOM.findDOMNode(ref.inputField.current);
if ($item && typeof $item.value === 'string') {
if (trimValue && $item && typeof $item.value === 'string') {
$item.value = $item.value.trim();
}
}
Expand Down Expand Up @@ -149,7 +149,7 @@ class ReactForm extends React.Component {
return invalid;
}

_collect(item) {
_collect(item, trimValue) {
const itemData = {
id: item.id,
name: item.field_name,
Expand All @@ -168,15 +168,15 @@ class ReactForm extends React.Component {
itemData.value = checked_options;
} else {
if (!ref) return null;
itemData.value = this._getItemValue(item, ref).value;
itemData.value = this._getItemValue(item, ref, trimValue).value;
}
return itemData;
}

_collectFormData(data) {
_collectFormData(data, trimValue) {
const formData = [];
data.forEach(item => {
const item_data = this._collect(item);
const item_data = this._collect(item, trimValue);
if (item_data) {
formData.push(item_data);
}
Expand Down Expand Up @@ -213,7 +213,7 @@ class ReactForm extends React.Component {
if (errors.length < 1) {
const { onSubmit } = this.props;
if (onSubmit) {
const data = this._collectFormData(this.props.data);
const data = this._collectFormData(this.props.data, true);
onSubmit(data);
} else {
const $form = ReactDOM.findDOMNode(this.form);
Expand All @@ -225,17 +225,17 @@ class ReactForm extends React.Component {
handleBlur(event) {
// Call submit function on blur
if (this.props.onBlur) {
const {onBlur} = this.props;
const data = this._collectFormData(this.props.data);
const { onBlur } = this.props;
const data = this._collectFormData(this.props.data, true);
onBlur(data);
}
}

handleChange(event) {
// Call submit function on change
if (this.props.onChange) {
const {onChange} = this.props;
const data = this._collectFormData(this.props.data);
const { onChange } = this.props;
const data = this._collectFormData(this.props.data, false);
onChange(data);
}
}
Expand Down

0 comments on commit 7eaf761

Please sign in to comment.