Basic Setup - agentg2007/validation-context Wiki

Creating input components

import { InputComponentType, withValidation } from "@nthity/validation";

const Input = withValidation<InputComponentType<string>>((props) => {
  const { className = "", componentId, value, onChange } = props;
  const { valid, css, messages } = useValidationState(componentId);
  return (
    <div className={className}>
      <div className={valid ? "" : css}>
        <input value={value} onChange={(e) => onChange(e.target.value)} />
      </div>
    </div>
  );
}, "InputDisplayName");

Using validation provider

import ValidationContextProvider, {
  useValidationResult,
  Validators,
} from "@nthity/validation";

const App = () => {
  const [value, setValue] = useState("");
  return (
    <div style={{ display: "flex", flexDirection: "column" }}>
      <ValidationContextProvider validators={Validators}>
        <main style={{ border: "1px solid black", padding: 16 }}>
          <Input
            value={value}
            onChange={(value) => setValue(value)}
            validators={[
              {
                name: "RequiredFieldValidator",
              },
              {
                name: "EmailFieldValidator",
              },
              {
                name: "StringLengthFieldValidator",
                min: 7,
                max: 32,
              },
            ]}
          />
        </main>
        <ButtonPanel />
      </ValidationContextProvider>
    </div>
  );
};

const ButtonPanel = () => {
  const { valid = false, messages } = useValidationResult();
  return (
    <div style={{ display: "flex", flexDirection: "column", marginTop: 8 }}>
      <button style={{ padding: 8 }} disabled={!valid}>
        Submit
      </button>
      {!valid && (
        <ul className="error-messages">
          {messages.map((m, index) => (
            <li
              key={`ValidationMessage#${index}`}
              children={<span>{m.message}</span>}
            />
          ))}
        </ul>
      )}
    </div>
  );
};
⚠️ **GitHub.com Fallback** ⚠️