Integrating numeric conversion with Kendo Form using FieldRenderProps

2 Answers 110 Views
Form
Tejas
Top achievements
Rank 2
Iron
Iron
Iron
Tejas asked on 16 Jun 2023, 11:07 AM

"I am using a Kendo form with FieldRenderProps, but I am facing an issue with the NumericTextBox action. Could you please provide some code to assist me? I have implemented a NumericTextBox for 'Kilometers' and would like to have the same setup for 'Miles'. The goal is to enter a value in Kilometers and have the corresponding Miles value automatically calculated and displayed in the Miles NumericTextBox. For instance, if I input 10 in Kilometers, the calculated Miles value should be 6.214 and displayed in the Miles NumericTextBox."

 

code link

https://stackblitz.com/edit/react-232pue-cjgnrl?file=app%2Fmain.jsx

 

 

import * as React from 'react';
import * as ReactDOM from 'react-dom';
import {
  Form,
  Field,
  FormElement,
  FormMultiSelect,
  FieldWrapper,
from '@progress/kendo-react-form';
import {
  Label,
  Error,
  Hint,
  FloatingLabel,
from '@progress/kendo-react-labels';
import { Button } from '@progress/kendo-react-buttons';
import { countriesemployeesgenderssizesequipment } from './data';
import {
  Input,
  MaskedTextBox,
  NumericTextBox,
  Checkbox,
  ColorPicker,
  Switch,
  RadioGroup,
  Slider,
  SliderLabel,
  RangeSlider,
  TextArea,
  Rating,
from '@progress/kendo-react-inputs';
const FormNumericTextBox = (fieldRenderProps=> {
  const {
    validationMessage,
    touched,
    label,
    id,
    valid,
    disabled,
    hint,
    ...others
  } = fieldRenderProps;
  const showValidationMessage = touched && validationMessage;
  const showHint = !showValidationMessage && hint;
  const hintId = showHint ? `${id}_hint` : '';
  const errorId = showValidationMessage ? `${id}_error` : '';
  return (
    <FieldWrapper>
      <Label editorId={id} editorValid={valid} editorDisabled={disabled}>
        {label}
      </Label>
      <NumericTextBox
        ariaDescribedBy={`${hintId} ${errorId}`}
        valid={valid}
        id={id}
        disabled={disabled}
        {...others}
      />
      {showHint && <Hint id={hintId}>{hint}</Hint>}
      {showValidationMessage && <Error id={errorId}>{validationMessage}</Error>}
    </FieldWrapper>
  );
};
const App = () => {
  const handleSubmit = (dataItem=> alert(JSON.stringify(dataItemnull2));
  return (
    <Form
      onSubmit={handleSubmit}
      initialValues={{
        genderselected: genders,
      }}
      render={(formRenderProps=> (
        <FormElement
          style={{
            width: 400,
          }}
        >
          <Field
            id={'Kilometers'}
            name={'Kilometers'}
            label={'Kilometers'}
            component={FormNumericTextBox}
          />
          <Field
            id={'Miles:'}
            name={'Miles:'}
            label={'Miles:'}
            component={FormNumericTextBox}
          />
          <div className="k-form-buttons">
            <Button
              themeColor={'primary'}
              type={'submit'}
              disabled={!formRenderProps.allowSubmit}
            >
              Submit
            </Button>
            <Button onClick={formRenderProps.onFormReset}>Clear</Button>
          </div>
        </FormElement>
      )}
    />
  );
};
ReactDOM.render(<App />document.querySelector('my-app'));

2 Answers, 1 is accepted

Sort by
0
Accepted
Konstantin Dikov
Telerik team
answered on 20 Jun 2023, 04:38 AM

Hi Tejas,

For achieving the desired result you can use the onChange event of the Field (https://www.telerik.com/kendo-react-ui/components/form/api/FieldProps/#toc-onchange) and use the Form ref and its valueSetter to change other field values. For your convenience, following is an example demonstrating how to achieve the desired result:

Hope this helps.

 

Regards,
Konstantin Dikov
Progress Telerik

As of R2 2023, the default icon type will be SVG instead of Font. See this blogpost for more information.
0
Tejas
Top achievements
Rank 2
Iron
Iron
Iron
answered on 20 Jun 2023, 01:34 PM
Thank you, Konstantin Dikov, for your invaluable assistance. Your expertise and guidance have been crucial in achieving success. I am truly grateful for your support and knowledge.
Tags
Form
Asked by
Tejas
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Konstantin Dikov
Telerik team
Tejas
Top achievements
Rank 2
Iron
Iron
Iron
Share this question
or