Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create NumericStepperInput component #404

Open
arinchaik opened this issue Aug 15, 2023 · 1 comment · May be fixed by #405
Open

create NumericStepperInput component #404

arinchaik opened this issue Aug 15, 2023 · 1 comment · May be fixed by #405

Comments

@arinchaik
Copy link
Contributor

arinchaik commented Aug 15, 2023

Description

  • add NumericStepperInput component

Screen Shot 2566-08-15 at 16 49 22
Screen Shot 2566-08-15 at 16 49 34

@arinchaik arinchaik linked a pull request Aug 15, 2023 that will close this issue
@vmundhra
Copy link

vmundhra commented Sep 20, 2023

@arinchaik I created a FieldNumberInput

Screen Shot 2023-09-20 at 11 59 09 AM

import { Button, HStack, Input, useNumberInput } from '@chakra-ui/react';
import { useField } from '@formiz/core';

import { FormGroup } from '../FormGroup';

export const FieldNumberInput = (props: TODO) => {
  const {
    errorMessage,
    id,
    isValid,
    isSubmitted,
    resetKey,
    setValue,
    value,
    otherProps,
  } = useField(props);
  const { required, name } = props;
  const {
    children,
    label,
    helper,
    min = 1,
    max = 100000,
    step = 1,
    ...rest
  } = otherProps;
  const [isTouched, setIsTouched] = useState(false);
  const showError = !isValid && (isTouched || isSubmitted);

  const {
    valueAsNumber,
    getInputProps,
    getIncrementButtonProps,
    getDecrementButtonProps,
  } = useNumberInput({
    step,
    min,
    max,
  });

  const inc = getIncrementButtonProps();
  const dec = getDecrementButtonProps();
  const input = getInputProps();

  useEffect(() => {
    setValue(valueAsNumber);
  }, [valueAsNumber, setValue]);

  useEffect(() => {
    setIsTouched(false);
  }, [resetKey]);

  const formGroupProps = {
    errorMessage,
    helper,
    id,
    isRequired: !!required,
    label,
    showError,
    name,
    ...rest,
  };

  return (
    <FormGroup {...formGroupProps}>
      <HStack>
        <Button {...dec}>-</Button>
        <Input
          id={id}
          value={value || ''}
          //onChange={changeValue}
          onBlur={() => setIsTouched(true)}
          aria-invalid={showError}
          aria-describedby={!isValid ? `${id}-error` : undefined}
          min={min}
          max={max}
          step={step}
          {...input}
        />
        <Button {...inc}>+</Button>
      </HStack>
      {children}
    </FormGroup>
  );
};

which can be used under Formiz form as

<FieldNumberInput name="quantity" label="Quantity" required="Required" />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants