How to use the react-jsonschema-form/lib/utils.asNumber function in react-jsonschema-form

To help you get started, we’ve selected a few react-jsonschema-form examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github rjsf-team / react-jsonschema-form / packages / material-ui / src / SelectWidget / SelectWidget.tsx View on Github external
const { type, items } = schema;
  if (value === '') {
    return undefined;
  } else if (type === 'array' && items && nums.has(items.type)) {
    return value.map(asNumber);
  } else if (type === 'boolean') {
    return value === 'true';
  } else if (type === 'number') {
    return asNumber(value);
  }

  // If type is undefined, but an enum is present, try and infer the type from
  // the enum values
  if (schema.enum) {
    if (schema.enum.every((x: any) => guessType(x) === 'number')) {
      return asNumber(value);
    } else if (schema.enum.every((x: any) => guessType(x) === 'boolean')) {
      return value === 'true';
    }
  }

  return value;
};
github cybertec-postgresql / rjsf-material-ui / src / SelectWidget / SelectWidget.tsx View on Github external
const { type, items } = schema;
  if (value === '') {
    return undefined;
  } else if (type === 'array' && items && nums.has(items.type)) {
    return value.map(asNumber);
  } else if (type === 'boolean') {
    return value === 'true';
  } else if (type === 'number') {
    return asNumber(value);
  }

  // If type is undefined, but an enum is present, try and infer the type from
  // the enum values
  if (schema.enum) {
    if (schema.enum.every((x: any) => guessType(x) === 'number')) {
      return asNumber(value);
    } else if (schema.enum.every((x: any) => guessType(x) === 'boolean')) {
      return value === 'true';
    }
  }

  return value;
};
github rjsf-team / react-jsonschema-form / packages / material-ui / src / SelectWidget / SelectWidget.tsx View on Github external
const processValue = (schema: any, value: any) => {
  // "enum" is a reserved word, so only "type" and "items" can be destructured
  const { type, items } = schema;
  if (value === '') {
    return undefined;
  } else if (type === 'array' && items && nums.has(items.type)) {
    return value.map(asNumber);
  } else if (type === 'boolean') {
    return value === 'true';
  } else if (type === 'number') {
    return asNumber(value);
  }

  // If type is undefined, but an enum is present, try and infer the type from
  // the enum values
  if (schema.enum) {
    if (schema.enum.every((x: any) => guessType(x) === 'number')) {
      return asNumber(value);
    } else if (schema.enum.every((x: any) => guessType(x) === 'boolean')) {
      return value === 'true';
    }
  }

  return value;
};
github cloudwan / gohan_webui / src / Form / formComponents / widgets / SelectWidget.js View on Github external
function processValue({type, items}, value) {
  if (type === 'array' && items && ['number', 'integer'].includes(items.type)) {
    return value.map(asNumber);
  } else if (type === 'boolean') {
    return value === 'true';
  } else if (type === 'number') {
    return asNumber(value);
  }
  return value;
}
github cloudwan / gohan_webui / src / Form / formComponents / widgets / BaseInput.js View on Github external
onInputChange = event => {
    const errors = [];
    const {type} = this.props.schema;
    let value = event.target.value;

    if (this.props.required && !value) {
      errors.push({
        message: 'required'
      });
    }

    if (type.includes('number') || type.includes('integer')) {
      const dialogSchema = cloneDeepWith(this.props.schema);
      value = (value === '' && dialogSchema.nullable) ? null : asNumber(value);

      if (dialogSchema.nullable) {
        dialogSchema.type = [dialogSchema.type, 'null'];
      }

      validator.validate(dialogSchema, value);
    } else {
      validator.validate(this.props.schema, value);
    }

    if (validator.errors) {
      errors.push(...validator.errors);
    }

    this.setState({errors});
    this.props.onChange(value);