Socket
Socket
Sign inDemoInstall

@bearei/react-form

Package Overview
Dependencies
Maintainers
1
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@bearei/react-form

Base form components that support React and React native


Version published
Maintainers
1
Created
Source

react-form

Base form components that support React and React native

Installation

yarn add @bearei/react-form --save

Parameters

Form
NameTypeRequiredDescription
formFormInstance✘Form instance
initialValuesRecord<string, unknown>✘Initializes the form value
itemsBaseFormItemProps✘Form items
onFinish(options: OnFinishOptions) => void✘This function is called when the form is completed
onFinishFailed(options: Errors) => void✘This function is called when the form fails to complete
onValuesChange(changedValues: Record<string, unknown>, values: Record<string, unknown>) => void✘This function is called when the form field value changes
renderMain(props: FormMainProps) => ReactNodeβœ”Render the form main
renderContainer(props: FormContainerProps) => ReactNodeβœ”Render the form container
Form Item
NameTypeRequiredDescription
namestring✘Form item field name
labelReactNode✘Form item label
noStyleReactNode✘Whether the form item is unstyled
initialValueunknown✘The initial value of the form item
extraReactNode✘Additional content for the form item
requiredboolean✘Whether the form entry is a required field
renderControl(props: ControlProps) => JSX.Element✘Render the controlled component
rulesRuleItem[]✘Validate rules -- RuleItem
validateFirstboolean✘When a rule fails, do you stop checking the rest of the rules
renderLabel(props: FormItemLabelProps) => ReactNode✘Render the form item label
renderExtra(props: FormItemExtraProps) => ReactNode✘Render the form item extra
renderMain(props: FormItemMainProps) => ReactNodeβœ”Render the form item main
renderContainer(props: FormItemContainerProps) => ReactNodeβœ”Render the form item container

Api

Form instance
NameTypeDescription
getFieldEntities(signOut?: boolean) => FieldEntity[]Gets the form field entities
getFieldEntitiesName(names?: string[], signOut?: boolean) => (string)[]Gets the form field entities name
signInField(entity: FieldEntity) => {signOut: () => void}Sign in form field
signOutField(name?: NamePath) => voidSign out form field
setFieldsValue(values?: Record<string, unknown>, options?: {validate?: boolean; response?: boolean}) => voidSet the value of the form fields
getFieldValue(name?: NamePath): unknownGets the value of the form field
setFieldError(error: Errors) => voidError setting form field
getFieldError(name?: NamePath): Errors Gets the form field error
setInitialValues(values?: Record<string, unknown>, init?: boolean) => voidSet the form initialization value
getInitialValues() => Record<string, unknown>Gets the form initialization value
setCallbacks(callbackValues: Callbacks) => voidSets the form callbacks
setFieldTouched(name?: string, touched?: boolean) => voidSets whether the form field is operated on
isFieldTouched(name?: NamePath) => booleanCheck that the form fields have been manipulated
validateField(name?: NamePath): Promise<Errors>Validate form field
resetField(name?: NamePath) => voidReset the form field
submit(skipValidate?: boolean) => voidComplete the form field submission

Use

import React from 'React';
import ReactDOM from 'react-dom';
import form, { FormItem } from '@bearei/react-form';

interface CostInputProps {
  onValueChange?: (value: string) => void;
  value?: unknown;
}

const CostInput: FC<CostInputProps> = ({ value, onValueChange }) => {
  const [inputValue, setInputValue] = useState('');
  const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    e.preventDefault();
    const inputtedValue = e.currentTarget.value;

    setInputValue(inputtedValue);
    onValueChange?.(inputtedValue);
  };

  useEffect(() => {
    value && value !== inputValue && setInputValue(`${value}`);
  }, [value]);

  return <input value={inputValue} onChange={handleChange} />;
};

const items = [
  {
    label: 'label1',
    name: 'name1',
    renderControl: (props: ControlProps) => <CostInput {...props} />,
  },
  {
    label: 'label2',
    name: 'name2',
    renderControl: (props: ControlProps) => <CostInput {...props} />,
  },
  {
    label: 'label3',
    name: 'name3',
    renderControl: (props: ControlProps) => <CostInput {...props} />,
  },
];

const form = (
  <Form
    items={items}
    renderMain={({ items }) =>
      items?.map((item, index) => (
        <Form.Item
          key={item.name}
          {...item}
          renderMain={({ value, onValueChange, renderControl }) =>
            renderControl?.({ value, onValueChange })
          }
          renderContainer={({ children }) => <div>{children}</div>}
        />
      ))
    }
    renderContainer={({ children }) => <div>{children}</div>}
  />
);

ReactDOM.render(form, container);

Keywords

FAQs

Package last updated on 15 Mar 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚑️ by Socket Inc