All files / atoms/Input/mobile/Components/Context InputContext.tsx

90% Statements 9/10
50% Branches 1/2
50% Functions 2/4
90% Lines 9/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42      2x                       2x                 18x 18x   18x             2x 39x 39x     39x    
import { InputSize } from '../../../Input.types';
import { createContext, useContext, useState } from 'react';
 
export const InputContext = createContext<{
  status: 'error' | 'success' | 'warning' | 'default';
  setStatus: (status: 'error' | 'success' | 'warning' | 'default') => void;
  size: InputSize;
  setSize: (size: InputSize) => void;
}>({
  status: 'default',
  setStatus: () => {},
  size: 'default',
  setSize: () => {},
});
 
export const InputProvider = ({
  children,
  statusValue,
  sizeProps,
}: {
  children: React.ReactNode;
  statusValue: 'error' | 'success' | 'warning' | 'default';
  sizeProps: InputSize;
}) => {
  const [status, setStatus] = useState<typeof statusValue>(statusValue);
  const [size, setSize] = useState<typeof sizeProps>(sizeProps);
 
  return (
    <InputContext.Provider value={{ status, setStatus, size, setSize }}>
      {children}
    </InputContext.Provider>
  );
};
 
export const useInput = () => {
  const context = useContext(InputContext);
  Iif (!context) {
    throw new Error('useInput must be used within an InputProvider');
  }
  return context;
};