import React, { Component } from 'react' import { View, Text, DeviceEventEmitter } from 'react-native' import math from 'mathjs' import kindOf from 'kind-of' import { CalculateProps } from '../utils/PropTypes' class SsCalculateView extends Component { render() { const { title, computedValue } = this.props return ( {title} {computedValue} ) } } export default class SsCalculate extends Component { private subscription: any static defaultProps = { required: false, textAlign: 'right', upper: true, } constructor(props) { super(props) this.state = { value: 0, } } componentDidMount() { this.subscription = DeviceEventEmitter.addListener('SsDynamicFormValueChanged', ({ values }) => { this.refresh(values) }) } componentWillUnmount() { this.subscription.remove() } refresh = (values: any) => { try { const { formula } = this.props let expression = '' const calElements = formula.map(item => { return kindOf(item) === 'object' ? values[item.id] || '0' : item }) calElements.forEach(item => { expression += item.toString() }) console.log(expression) const value = math.eval(expression).toFixed(2, 10) this.setState({ value: value === 'Infinity' || isNaN(value) ? '' : value, }) } catch (error) { console.log(error) } } render() { const { label, placeholder } = this.props const { value } = this.state return } }