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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | 23x 11x 12x 1x 11x 128x 20x 20x 10x 10x 1x 20x 20x 20x 20x 20x 10x 10x 7x 7x 13x 12x 7x 7x 13x 2x 7x 7x | // @flow import is from 'is_js'; import type {FieldSchema, Schema, ComponentState, FieldRules} from "../flowTypes"; import {FieldStatus} from "./helpers"; import {checkIsFormOK, rulesRunner} from "./validationUtils"; /** * Create initial value for a field if no default is provided. * */ export function createInitialValue(schema: FieldRules): mixed { if (is.propertyDefined(schema, 'default')) { return schema.default; } else if (is.propertyDefined(schema, 'min')) { return schema.min; } return ''; } /** * Create a new state for a field in componentState.formStatus.fields.field * */ export function createNewFieldState() { return { status: FieldStatus.normal, errorText: '', value: undefined }; } /** * When the schema contains a default rule */ function validateStateIfHasDefaultValue( fieldSchema: FieldSchema, fieldValue: mixed ) { let result; const fieldName = Object.keys(fieldSchema)[0]; if (is.propertyDefined(fieldSchema[fieldName], 'default')){ try { result = rulesRunner(fieldValue, fieldSchema) } catch (err) { result = err } } return result; } export function createFieldState( schema: Schema, fieldName: string ){ const initialFieldState = createNewFieldState(); initialFieldState.value = createInitialValue(schema[fieldName]); const result = validateStateIfHasDefaultValue( { [fieldName]: schema[fieldName] }, initialFieldState.value ); if (result) { return result; } return initialFieldState; } export function createInitialState( schema: Schema, userState: ComponentState ) { const initialState = { ...userState }; Object.keys(schema).forEach(fieldName => { if (fieldName === 'collectValues') return; initialState[fieldName] = createFieldState(schema, fieldName); }); const schemaItems = Object.keys(schema); schemaItems.forEach(name => { if (is.propertyDefined(userState, name)) { initialState[name] = { ...initialState[name], ...userState[name] }; } }); checkIsFormOK(schema, initialState); return initialState; } |