Kuberform is a reactive form builder for react
npm install @kuberspace/kuberform``bash`
npm install kuberform --save
`jsx`
import {FormGroup,FormControl,FormArray} from '@kuberspace/kuberform';
`ts`
interface Props {
groupName: String;
}
`ts`
interface Props {
arrayName: String;
}
`ts
interface Props {
controlName: String;
defaultValue: any | undefined;
validators:Array< (value: string, observable: Observable<{[key:String]:String} | null>) => void > | undefined;
}
`
`ts
interface Props {
value: any | null;
status: String;
setValue: (value: any): void {};
invalid: Boolean;
touched: Boolean;
dirty: Boolean;
errors: {[key:String]: Boolean | any};
}
`
tsinterface Props {
value: any | null;
status: String;
invalid: Boolean;
touched: Boolean;
dirty: Boolean;
addChlid: (index: number): void {} | undefined;
removeChild: (index:number): void {} | undefined;
}
`
$3
`jsx
import React from 'react';
import {FormGroup,FormControl,FormArray} from '@kuberspace/kuberform';
import InputField from 'yourinputfield';class myform extends React.component {
constructor(props){
super(props);
this.myform = React.createRef();
}
submit(){
console.log(this.myform.current.getValue());
}
addChild(){
this.myform.current.getControl("nestedArray").addChild();
}
render(){
return (
)
}
}
`Making a validator
$3
`jsx
export default function required() {
return (control, obs) => {
if (control.getValue() === "bob"){
obs.next(null);
//valid
} else {
obs.next({err:true});
//invalid
}
}
}`$3
`jsx
componentDidMount(){
this.myform.getValue();
}
`
$3
`tsinterface Methods {
getValue: ()=> Object | null | String;
getStatus: ()=> "VALID" | "INVALID" | "PENDING";
getControl: (control:String) => React.Refrence;
setValue: (value: any) => void;
valueChanges: ()=>Observable
`$3
#### hence most inputs have active events such as blur and onchange so you do not need to manualy call setValue.
`jsx
import React from 'react';
import { FormControl, InputLabel, TextField, FormHelperText } from '@mui/material'class InputField extends React.Component {
constructor(props) {super(props);}
getErrorMessage(){
if (this.props.errors === null){return null};
if (this.props.errorMessages && this.props.touched && this.props.invalid){
return Object.keys(this.props.errorMessages).map(key=>{
if (this.props.errors[key]){
return ( {this.props.errorMessages[key]} )
}
})
}
}
render() {
return (
sx={{width:this.props.width}}
error={ this.props.touched && this.props.invalid ? true:false}
multiline
maxRows={2}
id="outlined-error"
label={this.props.label}
/>
{this.getErrorMessage()}
);
}
}``