"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Basic = Basic;
var _react = _interopRequireWildcard(require("react"));
var _eui = require("@elastic/eui");
var _addonActions = require("@storybook/addon-actions");
var _hooks = require("../../hooks");
var _use_multi_fields = require("../use_multi_fields");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License
 * 2.0 and the Server Side Public License, v 1; you may not use this file except
 * in compliance with, at your election, the Elastic License 2.0 or the Server
 * Side Public License, v 1.
 */

const fields = {
  min: {
    path: 'minValue',
    defaultValue: 0
  },
  max: {
    path: 'maxValue',
    defaultValue: 100
  }
};
function Basic() {
  const [{
    minValue: _min,
    maxValue: _max
  }] = (0, _hooks.useFormData)();
  (0, _react.useEffect)(() => {
    (0, _addonActions.action)('Min max values')([_min, _max]);
  }, [_min, _max]);
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_eui.EuiText, null, /*#__PURE__*/_react.default.createElement("p", null, /*#__PURE__*/_react.default.createElement(_eui.EuiTextColor, {
    color: "subdued"
  }, "When you need to declare multiple fields you can use \"UseMultiFields\" to avoid nesting multiple \"UseField\"."))), /*#__PURE__*/_react.default.createElement(_eui.EuiSpacer, null), /*#__PURE__*/_react.default.createElement(_eui.EuiFormRow, {
    label: "Range",
    helpText: "The <EuiDualRange /> component sets 2 values in the form."
  }, /*#__PURE__*/_react.default.createElement(_use_multi_fields.UseMultiFields, {
    fields: fields
  }, ({
    min,
    max
  }) => {
    return /*#__PURE__*/_react.default.createElement(_eui.EuiDualRange, {
      min: 0,
      max: 100,
      value: [min.value, max.value],
      onChange: ([minValue, maxValue]) => {
        min.setValue(minValue);
        max.setValue(maxValue);
      }
    });
  })));
}
Basic.storyName = 'Basic';
Basic.parameters = {
  docs: {
    source: {
      code: `
const fields = {
  min: {
    path: 'minValue',
    defaultValue: 0,
  },
  max: {
    path: 'maxValue',
    defaultValue: 100,
  },
};

const MyFormComponent = () => {
  const { form } = useForm({ defaultValue });

  const [{ minValue: _min, maxValue: _max }] = useFormData();

  useEffect(() => {
    action('Min max values')([_min, _max]);
  }, [_min, _max]);

  const submitForm = async () => {
    const { isValid, data } = await form.submit();
    if (isValid) {
      // ... do something with the data
    }
  };

  return (
    <Form form={form}>
      <EuiText>
        <p>
          <EuiTextColor color="subdued">
            When you need to declare multiple fields you can use &quot;UseMultiFields&quot; to avoid
            nesting multiple &quot;UseField&quot;.
          </EuiTextColor>
        </p>
      </EuiText>
      <EuiSpacer />

      <EuiFormRow
        label="Range"
        helpText="The <EuiDualRange /> component sets 2 values in the form."
      >
        <UseMultiFields<{ min: number; max: number }> fields={fields}>
          {({ min, max }) => {
            return (
              <EuiDualRange
                min={0}
                max={100}
                value={[min.value, max.value]}
                onChange={([minValue, maxValue]) => {
                  min.setValue(minValue as number);
                  max.setValue(maxValue as number);
                }}
              />
            );
          }}
        </UseMultiFields>
      </EuiFormRow>
      <EuiSpacer />
      <EuiButton onClick={submitForm}>Send</EuiButton>
    </Form>
  );
};
      `,
      language: 'tsx'
    }
  }
};