antd/lib/form#Rule TypeScript Examples

The following examples show how to use antd/lib/form#Rule. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SourceEditorFormStreamsConfigurableCollectionsField.tsx    From jitsu with MIT License 5 votes vote down vote up
SourceEditorFormStreamsCollectionsFieldComponent = ({
  collection,
  field,
  documentation,
  handleFormFieldsChange,
}: Props) => {
  const formItemChild = useMemo(() => {
    switch (collection.type.typeName) {
      case "selection":
        return (
          <Select
            allowClear
            mode={collection.type.data?.maxOptions > 1 || !collection.type.data?.maxOptions ? "multiple" : undefined}
            onChange={handleFormFieldsChange}
          >
            {collection.type.data.options.map((option: { displayName: string; id: string }) => (
              <Select.Option key={option.id} value={option.id}>
                {option.displayName}
              </Select.Option>
            ))}
          </Select>
        )

      case "string":
      default:
        return <Input autoComplete="off" onChange={handleFormFieldsChange} />
    }
  }, [collection])

  const validationRules = useMemo(() => {
    const rules = []

    if (collection.required) {
      rules.push({ required: collection.required, message: `${collection.displayName} is required` })
    }

    if (collection.type.data?.maxOptions > 1) {
      rules.push({
        validator: (rule: Rule, value: string[]) =>
          (value?.length ?? 0) <= collection.type.data.maxOptions
            ? Promise.resolve()
            : Promise.reject(`You can select maximum ${collection.type.data?.maxOptions} options`),
      })
    }

    return rules
  }, [collection])

  return (
    <Row>
      <Col span={16}>
        <Form.Item
          className="form-field_fixed-label"
          label={
            documentation ? (
              <LabelWithTooltip documentation={documentation} render={collection.displayName} />
            ) : (
              <span>{collection.displayName}:</span>
            )
          }
          key={collection.id}
          name={[field.name, "parameters", collection.id]}
          rules={validationRules}
          labelCol={{ span: 6 }}
          wrapperCol={{ span: 18 }}
        >
          {formItemChild}
        </Form.Item>
      </Col>
    </Row>
  )
}