|
| 1 | +import React from 'react'; |
| 2 | +import FormRenderer from '@data-driven-forms/react-form-renderer/dist/cjs/form-renderer'; |
| 3 | +import componentTypes from '@data-driven-forms/react-form-renderer/dist/cjs/component-types'; |
| 4 | + |
| 5 | +import TextField from '@data-driven-forms/mui-component-mapper/dist/cjs/text-field'; |
| 6 | +import FormTemplate from '@data-driven-forms/mui-component-mapper/dist/cjs/form-template'; |
| 7 | + |
| 8 | +const schema = { |
| 9 | + title: 'Validator mapper', |
| 10 | + fields: [ |
| 11 | + { |
| 12 | + label: 'Password', |
| 13 | + type: 'password', |
| 14 | + component: componentTypes.TEXT_FIELD, |
| 15 | + name: 'password', |
| 16 | + helperText: 'Include upper letter, number and one ? _ :', |
| 17 | + validate: [{ type: 'password' }] |
| 18 | + }, |
| 19 | + { |
| 20 | + component: componentTypes.TEXT_FIELD, |
| 21 | + label: 'Password (custom message)', |
| 22 | + type: 'password', |
| 23 | + name: 'password-message', |
| 24 | + helperText: 'Include upper letter, number and one ? _ :', |
| 25 | + validate: [{ type: 'password', message: 'Heslo musí obashovat velké písmeno, číslo a jeden znak z: ? _ :' }] |
| 26 | + } |
| 27 | + ] |
| 28 | +}; |
| 29 | + |
| 30 | +const componentMapper = { |
| 31 | + [componentTypes.TEXT_FIELD]: TextField |
| 32 | +}; |
| 33 | + |
| 34 | +const validatorMapper = { |
| 35 | + password: ({ message }) => (value) => { |
| 36 | + if (!value) { |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + if (!value.match(/[A-Z]/) || !value.match(/\d/) || !value.match(/\?_:/)) { |
| 41 | + return message || 'Password has to contain upper letter, number and one of ? _ :'; |
| 42 | + } |
| 43 | + } |
| 44 | +}; |
| 45 | + |
| 46 | +const ValidatorMapper = () => ( |
| 47 | + <FormRenderer |
| 48 | + FormTemplate={FormTemplate} |
| 49 | + validatorMapper={validatorMapper} |
| 50 | + componentMapper={componentMapper} |
| 51 | + schema={schema} |
| 52 | + onSubmit={console.log} |
| 53 | + /> |
| 54 | +); |
| 55 | + |
| 56 | +export default ValidatorMapper; |
0 commit comments