0.0.7 • Published 7 months ago

smh-rn-typescript-hooks v0.0.7

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

smh-rn-typescript-hooks

smh-rn-typescript-hooks

Installation

React Native

npm install --save smh-rn-typescript-hooks

Expo

npx expo install smh-rn-typescript-hooks

Available Hooks

  • useValidateForm
  • useValidateSelect
  • useValidateCheckBox

Usage

This packages is customized react-native hooks for making development easier, faster and more performant

useValidateForm()

We tested this with the help of rn-material-ui-textfield package.I would like to recommand to use the same. Other packages also work.

props
  • options
ParameterTypeDescriptionDefault*Required
typestringeither "number" or "string"NoneTrue
labelstringTextInput Label NameNoneTrue
isRequiredbooleanIt will checks the applied field is mandatory or notNoneTrue
keyboardstringkeyboard type for textinputdefaultFalse
minValuenumberThis is the minimum value for number text inputNoneFalse
maxValuenumberThis is the maximum value for number text inputNoneFalse
minLengthnumberThis is the minimum Length for string text inputNoneFalse
maxLengthnumberThis is the maximum value for string text inputNoneFalse
defaultValuenumber or stringThis is the default value for stringNoneFalse
isDisabledbooleanFor disable the user text inputNoneFalse
isEditablebooleanTo restrict the user to edit the value instead of disableNoneFalse
validationPatternregex patternThis is the recommended way to validate the user input (min and max values will be not consider once validationPattern presents)NoneFalse
minValueErrorstringcustomizing the error for Minimum Value checkMinimum value is required !!!False
maxValueErrorstringcustomizing the error for Maximum Value checkMaximum value is required !!!False
minLengthErrorstringcustomizing the error for Minimum Length checkMinimum length is required !!!False
maxLengthErrorstringcustomizing the error for Maximum Value checkMaximum Length is required !!!False
focusErrorstringcustomizing the error on FocusValue is required !!!False
validErrorstringcustomizing the error on not Valid inputGiven value is not valid !!!False
valueChangeCallbackfunctionit will triggers when the value changesNoneFalse
  • Structured Parameters
ParameterDefinitiontype
labelText Input labelstring
valueText Input valuestring
requiredTo get the Text Input is Required or notboolean
keyboardTypeTo get the Text Input keyboard typestring
defaultValueTo get the default text input valuestring or number
isValidTo get the given input is valid or notboolean
isFocusedTo get the given input is Focused or notboolean
isBluredTo get the given input is Blured or notboolean
hasErrorTo get the given value is error or notboolean
customErrorTo know the exact errorstring
inputIsDisabledTo get whether input is disabled or notboolean
inputIsEditableTo get whether input is editable or notboolean
minTo get minimum value for textinputnumber
maxTo get maximum value for textinputnumber
valueChangeHandlerfor updating the values in text inputfunction
valueFocusHandlerIt will fires when the text input focusedfunction
valueBlurHandlerIt will fires when the text input bluredfunction
resetIt will helps to reset the functionfunction

example

import {
  TextField,
  FilledTextField,
  OutlinedTextField,
} from "rn-material-ui-textfield";
import { StyleSheet, Text, View } from "react-native";
import { useValidateForm } from "smh-rn-typescript-hooks";

const TextInput = () => {
  const {
    label: textLabel,
    value: textValue,
    required: textIsRequired,
    keyboardType: textKeyboardType,
    defaultValue: textDefaultValue,
    isValid: textIsValid,
    isFocused:textIsFocused,
    isBlured:textIsBlured,
    hasError: textHasError,
    customError: textCustomError,
    inputIsDisabled: textIsDisabled,
    inputIsEditable: textIsEditable,
    min: textMin,
    max: textMax,
    valueChangeHandler: textValueChangeHandler,
    valueFocusHandler: textFocusHandler,
    valueBlurHandler: textBlurHandler,
    reset: textResetHandler,
  } = useValidateForm({
    type: "string",
    label: "TextLabel",
    isRequired: true,
    minLength: 10,
    maxLength: 15,
    isDisabled: false,
    isEditable: true,
    //   validationPattern:/^([a-z0-9]{5,})$/,
    minLengthError: "MinLengthError",
    maxLengthError: "MaxLengthErrorSudhakar",
    validError: "ValidError",
  });
  const {
    label: numberLabel,
    value: numberValue,
    required: numberIsRequired,
    keyboardType: numberKeyboardType,
    defaultValue: numberDefaultValue,
    isValid: numberIsValid,
    hasError: numberHasError,
    customError: numberCustomError,
    inputIsDisabled: numberIsDisabled,
    inputIsEditable: numberIsEditable,
    min: numberMin,
    max: numberMax,
    valueChangeHandler: numberValueChangeHandler,
    valueFocusHandler: numberFocusHandler,
    valueBlurHandler: numberBlurHandler,
    reset: numberResetHandler,
  } = useValidateForm({
    type: "number",
    label: "Number",
    isRequired: true,
    minValue: 10,
    maxValue: 15,
    isDisabled: false,
    isEditable: true,
    minValueError: "MinValueError",
    maxValueError: "MaxValueError",
    validError: "ValidError",
  });
  return (
    <Fragment>
      <OutlinedTextField
        label={textLabel}
        value={textValue}
        keyboardType={textKeyboardType}
        onChangeText={textValueChangeHandler}
        onFocus={textFocusHandler}
        onBlur={textBlurHandler}
        disabled={textIsDisabled}
        editable={textIsEditable}
        min={textMin}
        max={textMax}
        defaultValue={textDefaultValue}
        error={textCustomError}
      />
      <OutlinedTextField
        label={numberLabel}
        value={numberValue}
        keyboardType={numberKeyboardType}
        onChangeText={numberValueChangeHandler}
        onFocus={numberFocusHandler}
        onBlur={numberBlurHandler}
        disabled={numberIsDisabled}
        editable={numberIsEditable}
        min={numberMin}
        max={numberMax}
        defaultValue={numberDefaultValue}
        error={numberCustomError}
      />
    </Fragment>
  );
};

export default TextInput;

useValidateCheckBox()

We tested this with the help of expo-checkbox package.I would like to recommand to use the same. Other packages also work.

props
  • options
ParameterTypeDescriptionDefault*Required
isRequiredbooleanIt will checks the applied field is mandatory or notNoneTrue
valuebooleanvalue of the checkboxfalseTrue
validErrorstringcustomizing the error on not ValidPlease tick checkbox !!!False
checkedColorstringthe color code when its checked#000000False
uncheckedColorstringthe color code when its unchecked#a8a8a8False
onChangeCallBackfunctionit will triggers when the value changesNoneFalse
disabledbooleanthe checkbox is disabled or notNoneFalse
  • Structured Parameters
ParameterDefinitiontype
colorColor of the checkboxstring
valuevalue of the checkboxboolean
isValidTo get the given input is valid or notboolean
hasErrorTo get the given value is error or notboolean
customErrorTo know the exact errorstring
inputIsDisabledTo get whether input is disabled or notboolean
onValueChangeHandlerfor updating the values in text inputfunction
resetIt will helps to reset the functionfunction

example

import Checkbox from 'expo-checkbox';
import React, { useState } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { useValidateCheckBox } from 'smh-rn-typescript-hooks';

const CheckBox=()=> {
  const {
    color:checkboxColor,
    inputIsDisabled:checkBoxIsDisabled,
    onValueChangeHandler:checkBoxChangeHandler,
    value:checkBoxValue,
    isValid:checkBoxIsValid,
    customError:checkBoxCustomError,
    hasError:checkBoxHasError,
    reset:checkBoxReset,
}= useValidateCheckBox({
    isRequired:true,
    value:false,
    checkedColor:'#4630EB'
})
console.log("checkboxValue",checkBoxValue)

  return (
    <View style={styles.container}>
      <View style={styles.section}>
        <Checkbox
          style={styles.checkbox}
          value={checkBoxValue}
          onValueChange={checkBoxChangeHandler}
          color={checkboxColor}
        />
        <Text style={styles.paragraph}>Custom colored checkbox</Text>
      </View>
    </View>
  );
}
export default CheckBox;
const styles = StyleSheet.create({
  container: {
    flex: 1,
    marginHorizontal: 16,
    marginVertical: 32,
  },
  section: {
    flexDirection: 'row',
    alignItems: 'center',
  },
  paragraph: {
    fontSize: 15,
    color:"red"
  },
  checkbox: {
    margin: 8,
  },
});

useValidateSelect()

We tested this with the help of react-native-dropdown-picker package.I would like to recommand to use the same. Other packages also work.

props
  • options
ParameterTypeDescriptionDefault*Required
itemsListitems[]the list of objects having label value pairsNoneTrue
isRequiredbooleanto make it whether it will be required or notNoneTrue
defaultValueitemobject of label value pairNoneFalse
disabledbooleanwhether the input is disabled or notfalseFalse
multiplebooleanif its true, allows to select multiple valuesfalseFalse
minnumberif multiple is true,we can restrict the minimum items to pick0False
maxnumberif multiple is true,we can restrict the maximum items to pick100False
minErrorstringcustomizing the error on not satisfies the Minimum picks'Min Selection Expected !!!'False
maxErrorstringcustomizing the error on not satisfies the Maximum picks'Max Selections reached !!!'False
validErrorstringcustomizing the error on not Valid'Given value is not valid !!!'False
onChangeCallBackfunctionit will triggers when the value changesNoneFalse
onSelectItemCallBackfuntionfunction to call on select itemNoneFalse
onPressItemCallBackfunctionfunction to callback when the picker will be pressed#a8a8a8False
onOpenCallBackfuntionfunction to call when the picker is openedNoneFalse
onCloseCallBackfunctionfunction to callback when the picker is closed#a8a8a8False
  • Structured Parameters
ParameterDefinitiontype
openTo open or close the pickerboolean
itemsListTo get the list of itemsitems[]
valuevalue of the picked itemsitem or items[]
isDisabledThe status of picker is disabled or notboolean
setItemsfunction to set the Items after picker rendersfunction
setValuefunction to set the Itemsfunction
setOpenfunction to set the picker open or closefunction
onChangeValueCallBackfunction for after changing valuesfunction
onSelectItemCallBackfunction for after selecting valuesfunction
onPressCallBackfunction for after Pressing the pickerfunction
onOpenCallBackfunction for after Opening the pickerfunction
onCloseCallBackfunction for after closing the pickerfunction
minto get the minimum value of the picker when multiple is truenumber
maxto get the maximum value of the picker when multiple is truenumber
multipleto get whether multiple is true or falseboolean
selectedItemsto get the list of selected itemsitems[] or item
selectedValuesto get the list of selected valuesitems[] or item
isValidTo get the given input is valid or notboolean
hasErrorTo get the given value is error or notboolean
customErrorTo know the exact errorstring
resetTo reset everythingfunction

example

import { View, Text } from 'react-native'
import React, { Fragment, useEffect } from 'react'
import DropDownPicker from "react-native-dropdown-picker";
import { useValidateSelect } from 'smh-rn-typescript-hooks';

const Dropdown = () => {
    const {
        open:selectOpen,
        itemsList:selectItems,
        value:selectValue,
        isDisabled:selectIsDisabled,
        setItems:selectSetItems,
        setValue:selectSetValue,
        setOpen:selectSetOpen,
        onChangeValueCallBack:selectOnChangeValueCallBack,
        onSelectItemCallBack:selectOnSelectItemCallBack,
        onPressCallBack:selectOnPressCallBack,
        onOpenCallBack:selectOnOpenCallBack,
        onCloseCallBack:selectOnCloseCallBack,
        min:selectMin,
        max:selectMax,
        multiple:selectMultiple,
        selectedItems:selectSelectedItems,
        selectedValues:selectSelectedValues,
        isValid:selectIsValid,
        hasError:selectHasError,
        customError:selectCustomError,
    } = useValidateSelect({
        itemsList:[
            {label: 'Apple', value: 'apple'},
            {label: 'Banana', value: 'banana'},
            {label: 'Mango', value: 'mango'},
            {label: 'Lemon', value: 'lemon'},
          ],
          isRequired:true,
          // defaultValue:"banana",
          defaultValue:["banana","apple"],
          multiple:true
    })
    useEffect(() => {
      selectSetItems([
        {label: 'Apple', value: 'apple'},
        {label: 'Banana', value: 'banana'},
      ])
    },[])
  return (
    <Fragment>
    <DropDownPicker
        open={selectOpen}
        mode={"BADGE"}
        // style={styles.pickerStyle}
        // textStyle={styles.pickerItem}
        // containerStyle={styles.pickerItem}
        // labelStyle={styles.pickerItem}
        // placeholder={label}
        // listMode="MODAL"
        closeAfterSelecting={true}
        value={selectValue}
        items={selectItems}
        setOpen={selectSetOpen}
        setValue={selectSetValue}
        // setItems={setItems}
        autoScroll={true}
        itemSeparator={true}
        onChangeValue={selectOnChangeValueCallBack}
        onSelectItem={selectOnSelectItemCallBack}
        disabled={selectIsDisabled}
        onPress={selectOnPressCallBack}
        onOpen={selectOnOpenCallBack}
        onClose={selectOnCloseCallBack}
        min={selectMin}
        max={selectMax}
        multiple={selectMultiple}
        searchable={true}
      />
      <Text style={{color:"red",fontSize:20}}>{selectIsValid.toString()} - {selectHasError.toString()} - {selectCustomError}</Text>
      </Fragment>
  )
}

export default Dropdown

And we're done 🎉

Contributing

Contribution are always welcome, no matter how large or small !

We want this community to be friendly and respectful to each other.Please follow it in all your interactions with the project.

Please feel free to drop me a mail S MUNI HARISH

Acknowledgements

Thanks to the authors of these libraries for inspiration

Sponsor & Support

To keep this library maintained and up-to-date please consider sponsoring it on GitHub. Or if you are looking for a private support or help in customizing the experience, then reach out to me on Linkedin @smuniharish.

License

MIT


Made with ❤️

0.0.7

7 months ago

0.0.6

8 months ago

0.0.5

8 months ago

0.0.4

8 months ago

0.0.3

9 months ago

0.0.2

9 months ago