Exemplo n.º 1
0
 return ZalgoPromise.try(() => {
     if (typeof prop.queryValue === 'function' && isDefined(value)) {
         return prop.queryValue({ value });
     } else {
         return value;
     }
 });
Exemplo n.º 2
0
export function extendProps<P>(component : Component<P>, props : PropsType<P>, inputProps : PropsInputType<P>, helpers : ParentHelpers<P>, isUpdate : boolean = false) { // eslint-disable-line complexity

    // $FlowFixMe
    inputProps = inputProps || {};
    extend(props, inputProps);

    const propNames = isUpdate ? [] : [ ...component.getPropNames() ];

    // $FlowFixMe
    for (const key of Object.keys(inputProps)) {
        if (propNames.indexOf(key) === -1) {
            propNames.push(key);
        }
    }

    const aliases = [];

    const { state, close, focus, onError } = helpers;

    for (const key of propNames) {
        const propDef = component.getPropDefinition(key);
        // $FlowFixMe
        let value = inputProps[key];

        if (!propDef) {
            continue;
        }

        const alias = propDef.alias;
        if (alias) {
            if (!isDefined(value) && isDefined(inputProps[alias])) {
                value = inputProps[alias];
            }
            aliases.push(alias);
        }

        if (propDef.value) {
            value = propDef.value({ props, state, close, focus, onError });
        }

        if (!isDefined(value) && propDef.default) {
            value = propDef.default({ props, state, close, focus, onError });
        }

        if (isDefined(value)) {
            if (propDef.type === 'array' ? !Array.isArray(value) : (typeof value !== propDef.type)) {
                throw new TypeError(`Prop is not of type ${ propDef.type }: ${ key }`);
            }
        }
        
        // $FlowFixMe
        props[key] = value;
    }

    for (const alias of aliases) {
        // $FlowFixMe
        delete props[alias];
    }

    // $FlowFixMe
    for (const key of Object.keys(props)) {
        const propDef = component.getPropDefinition(key);
        // $FlowFixMe
        const value = props[key];

        if (!propDef) {
            continue;
        }

        if (isDefined(value) && propDef.validate) {
            // $FlowFixMe
            propDef.validate({ value, props });
        }

        if (isDefined(value) && propDef.decorate) {
            // $FlowFixMe
            props[key] = propDef.decorate({ value, props, state, close, focus, onError });
        }
    }

    for (const key of component.getPropNames()) {
        const propDef = component.getPropDefinition(key);
        if (propDef.required !== false && !isDefined(props[key])) {
            throw new Error(`Expected prop "${ key }" to be defined`);
        }
    }
}