npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

lx-model-validator-react

v1.0.1

Published

React headless components for model-validator

Downloads

7

Readme

Model Validator | React components

Project Helper for Model Validator - a basic set of UI components, React, to use.

Installation

  • npm install -D lx-model-validator
  • npm install -D lx-model-validator-react

Usage

For sure, you can use raw Model Validator functions, but I find it more convenient to have common react components for common tasks.

This package is for them. Just bindings: headless components that use Model Validator helpers.

Headless means that they do not contain any CSS styles.

ValidationMessageComponent

This is the root component to display the violations.

type TValidationMessageComponentProps = {
	type?: 'error' | 'warning' | 'notice'
	field?: string
	result?: TValidationResult
}
const ValidationMessageComponent: React.FC<TValidationMessageComponentProps> = (...) => {
	...
	return <div className={'validation-messages ' + type}>
		{allMessages.map((messageKey: string, idx:number) => {
			return <div className={'validation-message'} key={idx}>{messageKey}</div>;
		})}
	</div>;
};

Properties:

  • type, one of error, warning or notice, dfefault error`,
  • field to check (the key in terms of Validator's helper functions),
  • result to process.

Usage:

<fieldset>
	<label htmlFor="name">Name</label>
	<input className={'validation-' + getValidationClass(validationResult, 'name')}
		type="text"
		id="name"
		onChange={(e) => handleChange('name', e)}
		value={model.name}/>

	<ValidationMessageComponent field={'name'} result={validationResult}/>
</fieldset>

ValidationAnyMessageComponent

This is the dispatching component. As you can see, it just wraps up the calls of ValidationMessageComponent with corresponding settings:

type TValidationAnyMessageComponentProps = {
	field?: string
	types?: Array<string> // 'error' | 'warning' | 'notice'
	result?: TValidationResult
}
const ValidationAnyMessageComponent: React.FC<TValidationAnyMessageComponentProps> = ({...}) => {
	return <>
		{(!types || types.includes('error')) &&
			<ValidationMessageComponent type={'error'} field={field} result={result}/>}

		{(!types || types.includes('warning')) &&
			<ValidationMessageComponent type={'warning'} field={field} result={result}/>}

		{(!types || types.includes('notice')) && 
		    <ValidationMessageComponent type={'notice'} field={field} result={result}/>}
	</>;
};

Properties:

  • field to check (the key in terms of Validator's helper functions),
  • optional types,
  • result to process.

This is an extended version of ValidationMessageComponent which allows you to display not just one type of violations (errors, warning, notices) but the required set.

Usage:

<h1>Errors:</h1>

<ValidationAnyMessageComponent result={validationResult}/>

ValidationErrorMessageComponent

Wrapper for <ValidationMessageComponent> with predefined type={'error'}

ValidationWarningMessageComponent

Wrapper for <ValidationMessageComponent> with predefined type={'warning'}

ValidationNoticeMessageComponent

Wrapper for <ValidationMessageComponent> with predefined type={'notice'}

ValidationTooltipComponent

This component is designed to work together with React tooltip component.

type TValidationTooltipComponentProps = {
	field: string
	component: any
	result?: TValidationResult
	showOk?: boolean
	showNumber?: boolean
}
const ValidationTooltipComponent: React.FC<TValidationTooltipComponentProps> = (...) => {
	if (!result) {
		return null;
	}

    ...

	return <div className={'validation-tooltip ' + level}>
		<a data-tooltip-id={'validation-' + field} className={showNumber ? 'with-number' : ''}>{showNumber ? count : ''}</a>
		{element}
	</div>;
};

Properties:

  • field to check (the key in terms of Validator's helper functions),
  • component – target component which children will be replaced with
    <ul>
        {errors?.map(str => <li key={str}>{str}</li>)}
        {warnings?.map(str => <li key={str}>{str}</li>)}
        {notices?.map(str => <li key={str}>{str}</li>)}
    </ul>
  • result to process,
  • showOk to display the component even if there are no errors:
    <div className={'validation-tooltip ok'}>
        <a data-tooltip-id={'validation-' + field}></a>
    </div>
  • showNumber to output the number ov violation inside <a></a> tag.

Usage:

<fieldset>
	<label htmlFor="name">Name</label>
	<div className={'field-with-tooltip'}>
		<input
			className={'validation-' + getValidationClass(validationResult, 'name')}
			type="text"
			id="name"
			onChange={(e) => handleChange('name', e)}
			value={model.name}/>

		<ValidationTooltipComponent
			field={'name'}
			showOk={showOk}
			showNumber={showNumber}
			result={validationResult} component={<Tooltip place={'right'}/>}/>
	</div>
</fieldset>