cncjs
1/* eslint react/prop-types: 0 */
2import React from 'react';3import i18n from './i18n';4
5const Error = (props) => (6<div {...props} style={{ color: '#A94442' }} />7);8
9const required = (value, props, components) => {10if (props.type === 'radio') {11const name = props.name;12
13components = components[name] || [];14if (components.length === 0) {15return null;16}17
18// Controls the placement of the error message for radio buttons19if (components[components.length - 1] !== props) {20return null;21}22
23const checked = components.reduce((checked, props) => {24return checked || props.checked;25}, false);26
27if (checked) {28return null;29}30
31return (32<Error>{i18n._('This field is required.')}</Error>33);34}35
36if (props.type === 'checkbox') {37if (props.checked) {38return null;39}40
41return (42<Error>{i18n._('This field is required.')}</Error>43);44}45
46value = ('' + value).trim();47if (!value) {48return (49<Error>{i18n._('This field is required.')}</Error>50);51}52
53return null;54};55
56const password = (value, props, components) => {57const bothBlurred = components.password[0].blurred && components.confirm[0].blurred;58const bothChanged = components.password[0].changed && components.confirm[0].changed;59
60if (bothBlurred && bothChanged && components.password[0].value !== components.confirm[0].value) {61return (62<Error>{i18n._('Passwords should be equal.')}</Error>63);64}65
66return null;67};68
69export {70required,71password
72};73