@algolia/satellite 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/AutoComplete/AutoComplete.d.ts +1 -1
- package/dist/cjs/AutoComplete/AutoComplete.js +49 -26
- package/dist/cjs/AutoComplete/AutoComplete.tailwind.js +9 -1
- package/dist/cjs/AutoComplete/types.d.ts +1 -0
- package/dist/cjs/Checkbox/Checkbox.d.ts +1 -1
- package/dist/cjs/Checkbox/Checkbox.js +21 -13
- package/dist/cjs/Checkbox/Checkbox.tailwind.js +8 -2
- package/dist/cjs/DatePicker/DatePicker/DatePicker.d.ts +4 -2
- package/dist/cjs/DatePicker/DatePicker/DatePicker.js +18 -7
- package/dist/cjs/DatePicker/components/Display.d.ts +3 -3
- package/dist/cjs/DatePicker/components/Display.js +16 -5
- package/dist/cjs/Dropzone/Dropzone.d.ts +4 -2
- package/dist/cjs/Dropzone/Dropzone.js +23 -12
- package/dist/cjs/Field/Field.d.ts +1 -0
- package/dist/cjs/Field/Field.js +63 -11
- package/dist/cjs/Field/FieldContext.d.ts +3 -0
- package/dist/cjs/Field/useField.d.ts +4 -1
- package/dist/cjs/Field/useField.js +17 -1
- package/dist/cjs/Form/Form.d.ts +16 -0
- package/dist/cjs/Form/Form.js +140 -0
- package/dist/cjs/Form/FormContext.d.ts +13 -0
- package/dist/cjs/Form/FormContext.js +8 -0
- package/dist/cjs/Form/index.d.ts +3 -0
- package/dist/cjs/Form/index.js +27 -0
- package/dist/cjs/Form/stories/AsynchronousValidation.js +396 -0
- package/dist/cjs/Form/stories/Complex.js +928 -0
- package/dist/cjs/Form/stories/DependentFieldsValidation.js +249 -0
- package/dist/cjs/Form/stories/DirtyFields.js +339 -0
- package/dist/cjs/Form/stories/DynamicFieldsValidation.js +425 -0
- package/dist/cjs/Form/stories/FieldArrays.js +549 -0
- package/dist/cjs/Form/stories/ValidationStrategies.js +455 -0
- package/dist/cjs/Form/stories/utils/useFormikAutoFocusOnError.js +22 -0
- package/dist/cjs/Form/useForm.d.ts +1 -0
- package/dist/cjs/Form/useForm.js +11 -0
- package/dist/cjs/Input/Input.js +22 -9
- package/dist/cjs/Input/Input.tailwind.js +7 -2
- package/dist/cjs/Pagination/DotPagination/DotPagination.d.ts +1 -1
- package/dist/cjs/RadioGroup/RadioButton.d.ts +1 -1
- package/dist/cjs/RadioGroup/RadioButton.js +16 -3
- package/dist/cjs/RadioGroup/RadioButton.tailwind.js +23 -0
- package/dist/cjs/RadioGroup/RadioGroup.d.ts +7 -5
- package/dist/cjs/RadioGroup/RadioGroup.js +78 -16
- package/dist/cjs/RadioGroup/{RadiogroupContext.d.ts → RadioGroupContext.d.ts} +8 -1
- package/dist/cjs/RangeSlider/RangeSlider.d.ts +2 -3
- package/dist/cjs/RangeSlider/RangeSlider.js +27 -10
- package/dist/cjs/Satellite/locale.d.ts +2 -0
- package/dist/cjs/Select/Select.js +39 -6
- package/dist/cjs/Select/Select.tailwind.js +19 -0
- package/dist/cjs/Stepper/Step.js +2 -2
- package/dist/cjs/Switch/Switch.js +2 -2
- package/dist/cjs/Switch/SwitchOption.js +3 -3
- package/dist/cjs/Tables/DataTable/DataTable.d.ts +1 -1
- package/dist/cjs/Tag/Tag.d.ts +1 -1
- package/dist/cjs/TextArea/TextArea.d.ts +1 -0
- package/dist/cjs/TextArea/TextArea.js +47 -8
- package/dist/cjs/TextArea/TextArea.tailwind.js +41 -4
- package/dist/cjs/Toggle/Toggle.d.ts +1 -1
- package/dist/cjs/Toggle/Toggle.js +23 -6
- package/dist/cjs/Toggle/Toggle.tailwind.js +9 -0
- package/dist/cjs/Tooltip/Tooltip.d.ts +1 -1
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/index.js +12 -0
- package/dist/cjs/utils/isCssPropertySupported.d.ts +1 -1
- package/dist/cjs/utils/mergeRefs.d.ts +2 -0
- package/dist/cjs/utils/mergeRefs.js +17 -0
- package/dist/esm/AutoComplete/AutoComplete.d.ts +1 -1
- package/dist/esm/AutoComplete/AutoComplete.js +52 -27
- package/dist/esm/AutoComplete/AutoComplete.tailwind.js +9 -1
- package/dist/esm/AutoComplete/types.d.ts +1 -0
- package/dist/esm/Checkbox/Checkbox.d.ts +1 -1
- package/dist/esm/Checkbox/Checkbox.js +21 -13
- package/dist/esm/Checkbox/Checkbox.tailwind.js +8 -2
- package/dist/esm/DatePicker/DatePicker/DatePicker.d.ts +4 -2
- package/dist/esm/DatePicker/DatePicker/DatePicker.js +19 -8
- package/dist/esm/DatePicker/components/Display.d.ts +3 -3
- package/dist/esm/DatePicker/components/Display.js +15 -5
- package/dist/esm/Dropzone/Dropzone.d.ts +4 -2
- package/dist/esm/Dropzone/Dropzone.js +24 -13
- package/dist/esm/Field/Field.d.ts +1 -0
- package/dist/esm/Field/Field.js +64 -12
- package/dist/esm/Field/FieldContext.d.ts +3 -0
- package/dist/esm/Field/useField.d.ts +4 -1
- package/dist/esm/Field/useField.js +17 -2
- package/dist/esm/Form/Form.d.ts +16 -0
- package/dist/esm/Form/Form.js +135 -0
- package/dist/esm/Form/FormContext.d.ts +13 -0
- package/dist/esm/Form/FormContext.js +2 -0
- package/dist/esm/Form/index.d.ts +3 -0
- package/dist/esm/Form/index.js +3 -0
- package/dist/esm/Form/stories/AsynchronousValidation.js +387 -0
- package/dist/esm/Form/stories/Complex.js +919 -0
- package/dist/esm/Form/stories/DependentFieldsValidation.js +240 -0
- package/dist/esm/Form/stories/DirtyFields.js +330 -0
- package/dist/esm/Form/stories/DynamicFieldsValidation.js +416 -0
- package/dist/esm/Form/stories/FieldArrays.js +544 -0
- package/dist/esm/Form/stories/ValidationStrategies.js +446 -0
- package/dist/esm/Form/stories/utils/useFormikAutoFocusOnError.js +16 -0
- package/dist/esm/Form/useForm.d.ts +1 -0
- package/dist/esm/Form/useForm.js +5 -0
- package/dist/esm/Input/Input.js +22 -9
- package/dist/esm/Input/Input.tailwind.js +7 -2
- package/dist/esm/Pagination/DotPagination/DotPagination.d.ts +1 -1
- package/dist/esm/RadioGroup/RadioButton.d.ts +1 -1
- package/dist/esm/RadioGroup/RadioButton.js +16 -3
- package/dist/esm/RadioGroup/RadioButton.tailwind.js +23 -0
- package/dist/esm/RadioGroup/RadioGroup.d.ts +7 -5
- package/dist/esm/RadioGroup/RadioGroup.js +77 -15
- package/dist/esm/RadioGroup/{RadiogroupContext.d.ts → RadioGroupContext.d.ts} +8 -1
- package/dist/esm/RangeSlider/RangeSlider.d.ts +2 -3
- package/dist/esm/RangeSlider/RangeSlider.js +27 -10
- package/dist/esm/Satellite/locale.d.ts +2 -0
- package/dist/esm/Select/Select.js +41 -7
- package/dist/esm/Select/Select.tailwind.js +19 -0
- package/dist/esm/Stepper/Step.js +2 -2
- package/dist/esm/Switch/Switch.js +1 -1
- package/dist/esm/Switch/SwitchOption.js +2 -2
- package/dist/esm/Tables/DataTable/DataTable.d.ts +1 -1
- package/dist/esm/Tag/Tag.d.ts +1 -1
- package/dist/esm/TextArea/TextArea.d.ts +1 -0
- package/dist/esm/TextArea/TextArea.js +49 -9
- package/dist/esm/TextArea/TextArea.tailwind.js +41 -4
- package/dist/esm/Toggle/Toggle.d.ts +1 -1
- package/dist/esm/Toggle/Toggle.js +23 -6
- package/dist/esm/Toggle/Toggle.tailwind.js +9 -0
- package/dist/esm/Tooltip/Tooltip.d.ts +1 -1
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/utils/isCssPropertySupported.d.ts +1 -1
- package/dist/esm/utils/mergeRefs.d.ts +2 -0
- package/dist/esm/utils/mergeRefs.js +11 -0
- package/dist/satellite.min.css +1 -1
- package/package.json +13 -7
- /package/dist/cjs/RadioGroup/{RadiogroupContext.js → RadioGroupContext.js} +0 -0
- /package/dist/esm/RadioGroup/{RadiogroupContext.js → RadioGroupContext.js} +0 -0
@@ -0,0 +1,544 @@
|
|
1
|
+
import _taggedTemplateLiteral from "@babel/runtime/helpers/taggedTemplateLiteral";
|
2
|
+
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
|
3
|
+
import _defineProperty from "@babel/runtime/helpers/defineProperty";
|
4
|
+
var _templateObject, _templateObject2, _templateObject3, _templateObject4, _templateObject5, _templateObject6, _templateObject7, _templateObject8, _templateObject9, _templateObject10, _templateObject11, _templateObject12, _templateObject13, _templateObject14, _templateObject15, _templateObject16, _templateObject17, _templateObject18, _templateObject19, _templateObject20, _templateObject21, _templateObject22, _templateObject23, _templateObject24, _templateObject25, _templateObject26, _templateObject27, _templateObject28;
|
5
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
6
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
7
|
+
import { zodResolver } from "@hookform/resolvers/zod";
|
8
|
+
import { FieldArray, Formik, useFormikContext } from "formik";
|
9
|
+
import { useState } from "react";
|
10
|
+
import { useWatch } from "react-hook-form";
|
11
|
+
import { useFormContext } from "react-hook-form";
|
12
|
+
import { FormProvider } from "react-hook-form";
|
13
|
+
import { Controller, useFieldArray, useForm } from "react-hook-form";
|
14
|
+
import * as yup from "yup";
|
15
|
+
import { z } from "zod";
|
16
|
+
import { AutoComplete } from "../../AutoComplete";
|
17
|
+
import { Button, IconButton } from "../../Button";
|
18
|
+
import { Field } from "../../Field";
|
19
|
+
import { MinusIcon, PlusIcon } from "../../Icons";
|
20
|
+
import { Select } from "../../Select";
|
21
|
+
import stl from "../../styles/helpers/satellitePrefixer";
|
22
|
+
import { ExperimentalForm, ExperimentalFormErrorMessage, ExperimentalFormReset, ExperimentalFormSubmit } from "../Form";
|
23
|
+
import { useFormikAutoFocusOnError } from "./utils/useFormikAutoFocusOnError";
|
24
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
25
|
+
import { jsxs as _jsxs } from "react/jsx-runtime";
|
26
|
+
import { Fragment as _Fragment } from "react/jsx-runtime";
|
27
|
+
var facets = ["size", "price", "gender", "color", "brand"];
|
28
|
+
var autocompleteFacets = facets.map(function (facet) {
|
29
|
+
return {
|
30
|
+
label: facet,
|
31
|
+
value: facet
|
32
|
+
};
|
33
|
+
});
|
34
|
+
var operators = ["is", "is not", "contains", "not contains"];
|
35
|
+
var facetValues = {
|
36
|
+
size: ["XS", "S", "M", "L", "XL", "XXL"],
|
37
|
+
price: ["0-50", "50-100", "100-150", "150-200", "200-250", "250-300"],
|
38
|
+
gender: ["Male", "Female", "Unisex"],
|
39
|
+
color: ["Red", "Green", "Blue", "Yellow", "Black", "White"],
|
40
|
+
brand: ["Nike", "Adidas", "Puma", "Reebok", "Under Armour"]
|
41
|
+
};
|
42
|
+
var getAutocompleteFacetValues = function getAutocompleteFacetValues(facet) {
|
43
|
+
return facet in facetValues ? facetValues[facet].map(function (value) {
|
44
|
+
return {
|
45
|
+
label: value,
|
46
|
+
value: value
|
47
|
+
};
|
48
|
+
}) : [];
|
49
|
+
};
|
50
|
+
var initialFormData = {
|
51
|
+
filterGroups: [[{
|
52
|
+
facet: undefined,
|
53
|
+
operator: "is",
|
54
|
+
value: undefined
|
55
|
+
}]]
|
56
|
+
};
|
57
|
+
var zodSchema = z.object({
|
58
|
+
filterGroups: z.array(z.array(z.object({
|
59
|
+
facet: z.object({
|
60
|
+
label: z.string(),
|
61
|
+
value: z.string()
|
62
|
+
}, {
|
63
|
+
errorMap: function errorMap() {
|
64
|
+
return {
|
65
|
+
message: "You must specify a facet"
|
66
|
+
};
|
67
|
+
}
|
68
|
+
}),
|
69
|
+
operator: z["enum"](operators),
|
70
|
+
value: z.object({
|
71
|
+
label: z.string(),
|
72
|
+
value: z.string()
|
73
|
+
}, {
|
74
|
+
errorMap: function errorMap() {
|
75
|
+
return {
|
76
|
+
message: "You must specify a value"
|
77
|
+
};
|
78
|
+
}
|
79
|
+
})
|
80
|
+
}).transform(function (obj) {
|
81
|
+
return _objectSpread(_objectSpread({}, obj), {}, {
|
82
|
+
facet: obj.facet.value,
|
83
|
+
value: obj.value.value
|
84
|
+
});
|
85
|
+
})))
|
86
|
+
});
|
87
|
+
export var RHFFieldsArraysComponent = function RHFFieldsArraysComponent() {
|
88
|
+
var _useState = useState(initialFormData),
|
89
|
+
_useState2 = _slicedToArray(_useState, 2),
|
90
|
+
formData = _useState2[0],
|
91
|
+
setFormData = _useState2[1];
|
92
|
+
var api = useForm({
|
93
|
+
defaultValues: initialFormData,
|
94
|
+
reValidateMode: "onChange",
|
95
|
+
resolver: zodResolver(zodSchema)
|
96
|
+
});
|
97
|
+
var control = api.control,
|
98
|
+
handleSubmit = api.handleSubmit,
|
99
|
+
reset = api.reset;
|
100
|
+
var _useFieldArray = useFieldArray({
|
101
|
+
name: "filterGroups",
|
102
|
+
control: control
|
103
|
+
}),
|
104
|
+
fields = _useFieldArray.fields,
|
105
|
+
append = _useFieldArray.append,
|
106
|
+
remove = _useFieldArray.remove;
|
107
|
+
return /*#__PURE__*/_jsx("div", {
|
108
|
+
className: stl(_templateObject || (_templateObject = _taggedTemplateLiteral(["flex flex-col gap-8 w-full"]))),
|
109
|
+
children: /*#__PURE__*/_jsxs(FormProvider, _objectSpread(_objectSpread({}, api), {}, {
|
110
|
+
children: [/*#__PURE__*/_jsxs(ExperimentalForm, {
|
111
|
+
onSubmit: handleSubmit(function (data) {
|
112
|
+
return setFormData(data);
|
113
|
+
}),
|
114
|
+
onReset: function onReset() {
|
115
|
+
return setFormData(initialFormData);
|
116
|
+
},
|
117
|
+
className: stl(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["flex flex-col items-start gap-4 min-w-72"]))),
|
118
|
+
children: [/*#__PURE__*/_jsx(ExperimentalFormErrorMessage, {}), fields.map(function (field, index) {
|
119
|
+
return /*#__PURE__*/_jsxs("div", {
|
120
|
+
className: stl(_templateObject3 || (_templateObject3 = _taggedTemplateLiteral(["w-full flex flex-col gap-2"]))),
|
121
|
+
children: [/*#__PURE__*/_jsxs("div", {
|
122
|
+
className: stl(_templateObject4 || (_templateObject4 = _taggedTemplateLiteral(["w-full flex items-center justify-between"]))),
|
123
|
+
children: [/*#__PURE__*/_jsx("div", {
|
124
|
+
children: index === 0 ? "Only display items that match this group" : "And"
|
125
|
+
}), /*#__PURE__*/_jsx(IconButton, {
|
126
|
+
title: "Remove group",
|
127
|
+
variant: "subtle",
|
128
|
+
icon: MinusIcon,
|
129
|
+
disabled: fields.length === 1,
|
130
|
+
onClick: function onClick() {
|
131
|
+
return remove(index);
|
132
|
+
}
|
133
|
+
})]
|
134
|
+
}), /*#__PURE__*/_jsx(RHFFilter, {
|
135
|
+
parentIndex: index
|
136
|
+
})]
|
137
|
+
}, field.id);
|
138
|
+
}), /*#__PURE__*/_jsx(Button, {
|
139
|
+
variant: "subtle",
|
140
|
+
startIcon: PlusIcon,
|
141
|
+
onClick: function onClick() {
|
142
|
+
return (
|
143
|
+
// It accepts `object` and object[]`, so we need to append an array of array
|
144
|
+
append([[{
|
145
|
+
facet: undefined,
|
146
|
+
operator: "is",
|
147
|
+
value: undefined
|
148
|
+
}]])
|
149
|
+
);
|
150
|
+
},
|
151
|
+
children: "And"
|
152
|
+
}), /*#__PURE__*/_jsxs("div", {
|
153
|
+
className: stl(_templateObject5 || (_templateObject5 = _taggedTemplateLiteral(["flex gap-2"]))),
|
154
|
+
children: [/*#__PURE__*/_jsx(ExperimentalFormSubmit, {
|
155
|
+
variant: "primary",
|
156
|
+
"aria-label": "Apply changes",
|
157
|
+
children: "Apply"
|
158
|
+
}), /*#__PURE__*/_jsx(ExperimentalFormReset, {
|
159
|
+
"aria-label": "Reset the form",
|
160
|
+
onClick: function onClick() {
|
161
|
+
return reset();
|
162
|
+
},
|
163
|
+
children: "Reset"
|
164
|
+
})]
|
165
|
+
})]
|
166
|
+
}), /*#__PURE__*/_jsx("hr", {
|
167
|
+
className: stl(_templateObject6 || (_templateObject6 = _taggedTemplateLiteral(["text-grey-500"])))
|
168
|
+
}), /*#__PURE__*/_jsx("code", {
|
169
|
+
className: stl(_templateObject7 || (_templateObject7 = _taggedTemplateLiteral(["whitespace-pre"]))),
|
170
|
+
children: JSON.stringify(formData, null, 2)
|
171
|
+
})]
|
172
|
+
}))
|
173
|
+
});
|
174
|
+
};
|
175
|
+
var RHFFilter = function RHFFilter(_ref) {
|
176
|
+
var parentIndex = _ref.parentIndex;
|
177
|
+
var _useFormContext = useFormContext(),
|
178
|
+
control = _useFormContext.control,
|
179
|
+
formState = _useFormContext.formState,
|
180
|
+
getFieldStateFromReactHookForm = _useFormContext.getFieldState;
|
181
|
+
var _useFieldArray2 = useFieldArray({
|
182
|
+
name: "filterGroups.".concat(parentIndex),
|
183
|
+
control: control
|
184
|
+
}),
|
185
|
+
fields = _useFieldArray2.fields,
|
186
|
+
append = _useFieldArray2.append,
|
187
|
+
remove = _useFieldArray2.remove;
|
188
|
+
var filterGroup = useWatch({
|
189
|
+
name: "filterGroups.".concat(parentIndex)
|
190
|
+
});
|
191
|
+
var getFieldState = function getFieldState(fieldName) {
|
192
|
+
var _fieldState$error;
|
193
|
+
var fieldState = getFieldStateFromReactHookForm(fieldName, formState);
|
194
|
+
if ((_fieldState$error = fieldState.error) !== null && _fieldState$error !== void 0 && _fieldState$error.message) {
|
195
|
+
return {
|
196
|
+
status: "invalid",
|
197
|
+
errors: [fieldState.error.message]
|
198
|
+
};
|
199
|
+
} else if (!fieldState.invalid && formState.isSubmitted) {
|
200
|
+
return {
|
201
|
+
status: "validated"
|
202
|
+
};
|
203
|
+
} else {
|
204
|
+
return {
|
205
|
+
status: "default"
|
206
|
+
};
|
207
|
+
}
|
208
|
+
};
|
209
|
+
return /*#__PURE__*/_jsxs("div", {
|
210
|
+
className: stl(_templateObject8 || (_templateObject8 = _taggedTemplateLiteral(["w-full flex flex-col items-start gap-4 bg-grey-100 border border-grey-200 rounded p-4"]))),
|
211
|
+
children: [fields.map(function (field, index) {
|
212
|
+
return /*#__PURE__*/_jsxs("div", {
|
213
|
+
className: stl(_templateObject9 || (_templateObject9 = _taggedTemplateLiteral(["w-full flex items-stretch gap-2"]))),
|
214
|
+
children: [index > 0 && /*#__PURE__*/_jsx("div", {
|
215
|
+
className: stl(_templateObject10 || (_templateObject10 = _taggedTemplateLiteral(["mt-8"]))),
|
216
|
+
children: "Or"
|
217
|
+
}), /*#__PURE__*/_jsx(Controller, {
|
218
|
+
name: "filterGroups.".concat(parentIndex, ".").concat(index, ".facet"),
|
219
|
+
control: control,
|
220
|
+
render: function render(_ref2) {
|
221
|
+
var field = _ref2.field;
|
222
|
+
return /*#__PURE__*/_jsx(Field, {
|
223
|
+
label: "Facet",
|
224
|
+
labelFor: "filterGroups.".concat(parentIndex, ".").concat(index, ".facet"),
|
225
|
+
state: getFieldState("filterGroups.".concat(parentIndex, ".").concat(index, ".facet")),
|
226
|
+
className: stl(_templateObject11 || (_templateObject11 = _taggedTemplateLiteral(["w-full"]))),
|
227
|
+
children: /*#__PURE__*/_jsx(AutoComplete, _objectSpread({
|
228
|
+
id: "filterGroups.".concat(parentIndex, ".").concat(index, ".facet"),
|
229
|
+
options: autocompleteFacets,
|
230
|
+
placeholder: "Size",
|
231
|
+
required: true,
|
232
|
+
creatable: true
|
233
|
+
}, field))
|
234
|
+
});
|
235
|
+
}
|
236
|
+
}), /*#__PURE__*/_jsx(Controller, {
|
237
|
+
name: "filterGroups.".concat(parentIndex, ".").concat(index, ".operator"),
|
238
|
+
control: control,
|
239
|
+
render: function render(_ref3) {
|
240
|
+
var field = _ref3.field;
|
241
|
+
return /*#__PURE__*/_jsx(Field, {
|
242
|
+
label: "Operator",
|
243
|
+
labelFor: "filterGroups.".concat(parentIndex, ".").concat(index, ".operator"),
|
244
|
+
state: getFieldState("filterGroups.".concat(parentIndex, ".").concat(index, ".operator")),
|
245
|
+
children: /*#__PURE__*/_jsx(Select, _objectSpread(_objectSpread({
|
246
|
+
id: "filterGroups.".concat(parentIndex, ".").concat(index, ".operator"),
|
247
|
+
className: stl(_templateObject12 || (_templateObject12 = _taggedTemplateLiteral(["w-min"]))),
|
248
|
+
required: true
|
249
|
+
}, field), {}, {
|
250
|
+
children: operators.map(function (operator) {
|
251
|
+
return /*#__PURE__*/_jsx("option", {
|
252
|
+
value: operator,
|
253
|
+
children: operator
|
254
|
+
}, operator);
|
255
|
+
})
|
256
|
+
}))
|
257
|
+
});
|
258
|
+
}
|
259
|
+
}), /*#__PURE__*/_jsx(Controller, {
|
260
|
+
name: "filterGroups.".concat(parentIndex, ".").concat(index, ".value"),
|
261
|
+
control: control,
|
262
|
+
render: function render(_ref4) {
|
263
|
+
var _filterGroup$index;
|
264
|
+
var field = _ref4.field;
|
265
|
+
return /*#__PURE__*/_jsx(Field, {
|
266
|
+
label: "Value",
|
267
|
+
labelFor: "filterGroups.".concat(parentIndex, ".").concat(index, ".value"),
|
268
|
+
state: getFieldState("filterGroups.".concat(parentIndex, ".").concat(index, ".value")),
|
269
|
+
className: stl(_templateObject13 || (_templateObject13 = _taggedTemplateLiteral(["w-full"]))),
|
270
|
+
children: /*#__PURE__*/_jsx(AutoComplete, _objectSpread({
|
271
|
+
id: "filterGroups.".concat(parentIndex, ".").concat(index, ".value"),
|
272
|
+
options: getAutocompleteFacetValues((_filterGroup$index = filterGroup[index]) === null || _filterGroup$index === void 0 || (_filterGroup$index = _filterGroup$index.facet) === null || _filterGroup$index === void 0 ? void 0 : _filterGroup$index.label),
|
273
|
+
placeholder: "M",
|
274
|
+
required: true,
|
275
|
+
creatable: true
|
276
|
+
}, field))
|
277
|
+
});
|
278
|
+
}
|
279
|
+
}), /*#__PURE__*/_jsx(IconButton, {
|
280
|
+
title: "Remove filter",
|
281
|
+
variant: "subtle",
|
282
|
+
icon: MinusIcon,
|
283
|
+
className: stl(_templateObject14 || (_templateObject14 = _taggedTemplateLiteral(["mt-8 shrink-0"]))),
|
284
|
+
disabled: fields.length === 1,
|
285
|
+
onClick: function onClick() {
|
286
|
+
return remove(index);
|
287
|
+
}
|
288
|
+
})]
|
289
|
+
}, field.id);
|
290
|
+
}), /*#__PURE__*/_jsx(Button, {
|
291
|
+
variant: "subtle",
|
292
|
+
startIcon: PlusIcon,
|
293
|
+
onClick: function onClick() {
|
294
|
+
return append({
|
295
|
+
facet: undefined,
|
296
|
+
operator: "is",
|
297
|
+
value: undefined
|
298
|
+
});
|
299
|
+
},
|
300
|
+
children: "Or"
|
301
|
+
})]
|
302
|
+
});
|
303
|
+
};
|
304
|
+
var yupSchema = yup.object({
|
305
|
+
filterGroups: yup.array(yup.array(yup.object({
|
306
|
+
facet: yup.object({
|
307
|
+
label: yup.string(),
|
308
|
+
value: yup.string()
|
309
|
+
})["default"](undefined).required("You must specify a facet"),
|
310
|
+
operator: yup.string().oneOf(operators).required(),
|
311
|
+
value: yup.object({
|
312
|
+
label: yup.string(),
|
313
|
+
value: yup.string()
|
314
|
+
})["default"](undefined).required("You must specify a value")
|
315
|
+
})))
|
316
|
+
});
|
317
|
+
export var FormikFieldArraysComponent = function FormikFieldArraysComponent() {
|
318
|
+
var _useState3 = useState(initialFormData),
|
319
|
+
_useState4 = _slicedToArray(_useState3, 2),
|
320
|
+
formData = _useState4[0],
|
321
|
+
setFormData = _useState4[1];
|
322
|
+
var _useState5 = useState(false),
|
323
|
+
_useState6 = _slicedToArray(_useState5, 2),
|
324
|
+
revalidationEnabled = _useState6[0],
|
325
|
+
setRevalidationEnabled = _useState6[1];
|
326
|
+
return /*#__PURE__*/_jsxs("div", {
|
327
|
+
className: stl(_templateObject15 || (_templateObject15 = _taggedTemplateLiteral(["flex flex-col gap-8 w-full"]))),
|
328
|
+
children: [/*#__PURE__*/_jsx(Formik, {
|
329
|
+
initialValues: initialFormData,
|
330
|
+
validateOnChange: revalidationEnabled,
|
331
|
+
validateOnBlur: revalidationEnabled,
|
332
|
+
validationSchema: yupSchema,
|
333
|
+
onSubmit: function onSubmit(values) {
|
334
|
+
// We need to transform the values here because transforms in Yup are run before validations
|
335
|
+
var transformedValues = _objectSpread(_objectSpread({}, values), {}, {
|
336
|
+
filterGroups: values.filterGroups.map(function (group) {
|
337
|
+
return group.map(function (item) {
|
338
|
+
var _item$facet, _item$value;
|
339
|
+
return _objectSpread(_objectSpread({}, item), {}, {
|
340
|
+
facet: (_item$facet = item.facet) === null || _item$facet === void 0 ? void 0 : _item$facet.label,
|
341
|
+
value: (_item$value = item.value) === null || _item$value === void 0 ? void 0 : _item$value.label
|
342
|
+
});
|
343
|
+
});
|
344
|
+
})
|
345
|
+
});
|
346
|
+
setFormData(transformedValues);
|
347
|
+
},
|
348
|
+
onReset: function onReset() {
|
349
|
+
return setFormData(initialFormData);
|
350
|
+
},
|
351
|
+
render: function render(_ref5) {
|
352
|
+
var handleSubmit = _ref5.handleSubmit,
|
353
|
+
handleReset = _ref5.handleReset,
|
354
|
+
values = _ref5.values,
|
355
|
+
resetForm = _ref5.resetForm;
|
356
|
+
return /*#__PURE__*/_jsxs(ExperimentalForm, {
|
357
|
+
onSubmit: function onSubmit(e) {
|
358
|
+
setRevalidationEnabled(true);
|
359
|
+
handleSubmit(e);
|
360
|
+
},
|
361
|
+
onReset: function onReset(e) {
|
362
|
+
setRevalidationEnabled(false);
|
363
|
+
handleReset(e);
|
364
|
+
},
|
365
|
+
className: stl(_templateObject16 || (_templateObject16 = _taggedTemplateLiteral(["flex flex-col items-start gap-4 min-w-72"]))),
|
366
|
+
children: [/*#__PURE__*/_jsx(ExperimentalFormErrorMessage, {}), /*#__PURE__*/_jsx(FieldArray, {
|
367
|
+
name: "filterGroups",
|
368
|
+
render: function render(_ref6) {
|
369
|
+
var push = _ref6.push,
|
370
|
+
remove = _ref6.remove;
|
371
|
+
return /*#__PURE__*/_jsxs(_Fragment, {
|
372
|
+
children: [values.filterGroups.map(function (_, index) {
|
373
|
+
return /*#__PURE__*/_jsxs("div", {
|
374
|
+
className: stl(_templateObject17 || (_templateObject17 = _taggedTemplateLiteral(["w-full flex flex-col gap-2"]))),
|
375
|
+
children: [/*#__PURE__*/_jsxs("div", {
|
376
|
+
className: stl(_templateObject18 || (_templateObject18 = _taggedTemplateLiteral(["w-full flex items-center justify-between"]))),
|
377
|
+
children: [/*#__PURE__*/_jsx("div", {
|
378
|
+
children: index === 0 ? "Only display items that match this group" : "And"
|
379
|
+
}), /*#__PURE__*/_jsx(IconButton, {
|
380
|
+
title: "Remove group",
|
381
|
+
variant: "subtle",
|
382
|
+
icon: MinusIcon,
|
383
|
+
disabled: values.filterGroups.length === 1,
|
384
|
+
onClick: function onClick() {
|
385
|
+
return remove(index);
|
386
|
+
}
|
387
|
+
})]
|
388
|
+
}), /*#__PURE__*/_jsx(FormikFilter, {
|
389
|
+
parentIndex: index
|
390
|
+
})]
|
391
|
+
}, index);
|
392
|
+
}), /*#__PURE__*/_jsx(Button, {
|
393
|
+
variant: "subtle",
|
394
|
+
startIcon: PlusIcon,
|
395
|
+
onClick: function onClick() {
|
396
|
+
return push([{
|
397
|
+
facet: undefined,
|
398
|
+
operator: "is",
|
399
|
+
value: undefined
|
400
|
+
}]);
|
401
|
+
},
|
402
|
+
children: "And"
|
403
|
+
})]
|
404
|
+
});
|
405
|
+
}
|
406
|
+
}), /*#__PURE__*/_jsxs("div", {
|
407
|
+
className: stl(_templateObject19 || (_templateObject19 = _taggedTemplateLiteral(["flex gap-2"]))),
|
408
|
+
children: [/*#__PURE__*/_jsx(ExperimentalFormSubmit, {
|
409
|
+
variant: "primary",
|
410
|
+
"aria-label": "Apply changes",
|
411
|
+
children: "Apply"
|
412
|
+
}), /*#__PURE__*/_jsx(ExperimentalFormReset, {
|
413
|
+
"aria-label": "Reset the form",
|
414
|
+
onClick: function onClick() {
|
415
|
+
return resetForm();
|
416
|
+
},
|
417
|
+
children: "Reset"
|
418
|
+
})]
|
419
|
+
})]
|
420
|
+
});
|
421
|
+
}
|
422
|
+
}), /*#__PURE__*/_jsx("hr", {
|
423
|
+
className: stl(_templateObject20 || (_templateObject20 = _taggedTemplateLiteral(["text-grey-500"])))
|
424
|
+
}), /*#__PURE__*/_jsx("code", {
|
425
|
+
className: stl(_templateObject21 || (_templateObject21 = _taggedTemplateLiteral(["whitespace-pre"]))),
|
426
|
+
children: JSON.stringify(formData, null, 2)
|
427
|
+
})]
|
428
|
+
});
|
429
|
+
};
|
430
|
+
var FormikFilter = function FormikFilter(_ref7) {
|
431
|
+
var parentIndex = _ref7.parentIndex;
|
432
|
+
var formik = useFormikContext();
|
433
|
+
var getFieldState = function getFieldState(fieldName) {
|
434
|
+
var fieldMeta = formik.getFieldMeta(fieldName);
|
435
|
+
if (fieldMeta.error) {
|
436
|
+
return {
|
437
|
+
status: "invalid",
|
438
|
+
errors: [fieldMeta.error]
|
439
|
+
};
|
440
|
+
} else if (formik.isSubmitting && fieldMeta.touched) {
|
441
|
+
return {
|
442
|
+
status: "validated"
|
443
|
+
};
|
444
|
+
} else {
|
445
|
+
return {
|
446
|
+
status: "default"
|
447
|
+
};
|
448
|
+
}
|
449
|
+
};
|
450
|
+
useFormikAutoFocusOnError(formik);
|
451
|
+
return /*#__PURE__*/_jsx(FieldArray, {
|
452
|
+
name: "filterGroups.".concat(parentIndex),
|
453
|
+
render: function render(_ref8) {
|
454
|
+
var push = _ref8.push,
|
455
|
+
remove = _ref8.remove,
|
456
|
+
form = _ref8.form;
|
457
|
+
return /*#__PURE__*/_jsxs("div", {
|
458
|
+
className: stl(_templateObject22 || (_templateObject22 = _taggedTemplateLiteral(["w-full flex flex-col items-start gap-4 bg-grey-100 border border-grey-200 rounded p-4"]))),
|
459
|
+
children: [formik.values.filterGroups[parentIndex].map(function (_, index) {
|
460
|
+
var _form$values$filterGr;
|
461
|
+
return /*#__PURE__*/_jsxs("div", {
|
462
|
+
className: stl(_templateObject23 || (_templateObject23 = _taggedTemplateLiteral(["w-full flex items-stretch gap-2"]))),
|
463
|
+
children: [index > 0 && /*#__PURE__*/_jsx("div", {
|
464
|
+
className: stl(_templateObject24 || (_templateObject24 = _taggedTemplateLiteral(["mt-8"]))),
|
465
|
+
children: "Or"
|
466
|
+
}), /*#__PURE__*/_jsx(Field, {
|
467
|
+
label: "Facet",
|
468
|
+
labelFor: "filterGroups.".concat(parentIndex, ".").concat(index, ".facet"),
|
469
|
+
state: getFieldState("filterGroups.".concat(parentIndex, ".").concat(index, ".facet")),
|
470
|
+
className: stl(_templateObject25 || (_templateObject25 = _taggedTemplateLiteral(["w-full"]))),
|
471
|
+
children: /*#__PURE__*/_jsx(AutoComplete, _objectSpread(_objectSpread({
|
472
|
+
id: "filterGroups.".concat(parentIndex, ".").concat(index, ".facet"),
|
473
|
+
options: autocompleteFacets,
|
474
|
+
placeholder: "Size",
|
475
|
+
required: true,
|
476
|
+
creatable: true
|
477
|
+
}, form.getFieldProps("filterGroups.".concat(parentIndex, ".").concat(index, ".facet"))), {}, {
|
478
|
+
multiple: false // We need to override it because otherwise `getFieldProps` would spread `multiple`
|
479
|
+
,
|
480
|
+
onChange: function onChange(value) {
|
481
|
+
return form.setFieldValue("filterGroups.".concat(parentIndex, ".").concat(index, ".facet"), value);
|
482
|
+
}
|
483
|
+
}))
|
484
|
+
}), /*#__PURE__*/_jsx(Field, {
|
485
|
+
label: "Operator",
|
486
|
+
labelFor: "filterGroups.".concat(parentIndex, ".").concat(index, ".operator"),
|
487
|
+
state: getFieldState("filterGroups.".concat(parentIndex, ".").concat(index, ".operator")),
|
488
|
+
children: /*#__PURE__*/_jsx(Select, _objectSpread(_objectSpread({
|
489
|
+
id: "filterGroups.".concat(parentIndex, ".").concat(index, ".operator"),
|
490
|
+
className: stl(_templateObject26 || (_templateObject26 = _taggedTemplateLiteral(["w-min"]))),
|
491
|
+
required: true
|
492
|
+
}, form.getFieldProps("filterGroups.".concat(parentIndex, ".").concat(index, ".operator"))), {}, {
|
493
|
+
children: operators.map(function (operator) {
|
494
|
+
return /*#__PURE__*/_jsx("option", {
|
495
|
+
value: operator,
|
496
|
+
children: operator
|
497
|
+
}, operator);
|
498
|
+
})
|
499
|
+
}))
|
500
|
+
}), /*#__PURE__*/_jsx(Field, {
|
501
|
+
label: "Value",
|
502
|
+
labelFor: "filterGroups.".concat(parentIndex, ".").concat(index, ".value"),
|
503
|
+
state: getFieldState("filterGroups.".concat(parentIndex, ".").concat(index, ".value")),
|
504
|
+
className: stl(_templateObject27 || (_templateObject27 = _taggedTemplateLiteral(["w-full"]))),
|
505
|
+
children: /*#__PURE__*/_jsx(AutoComplete, _objectSpread(_objectSpread({
|
506
|
+
id: "filterGroups.".concat(parentIndex, ".").concat(index, ".value"),
|
507
|
+
options: getAutocompleteFacetValues((_form$values$filterGr = form.values.filterGroups[parentIndex][index]) === null || _form$values$filterGr === void 0 || (_form$values$filterGr = _form$values$filterGr.facet) === null || _form$values$filterGr === void 0 ? void 0 : _form$values$filterGr.label),
|
508
|
+
placeholder: "M",
|
509
|
+
required: true,
|
510
|
+
creatable: true
|
511
|
+
}, form.getFieldProps("filterGroups.".concat(parentIndex, ".").concat(index, ".value"))), {}, {
|
512
|
+
multiple: false // We need to override it because otherwise `getFieldProps` would spread `multiple`
|
513
|
+
,
|
514
|
+
onChange: function onChange(value) {
|
515
|
+
return form.setFieldValue("filterGroups.".concat(parentIndex, ".").concat(index, ".value"), value);
|
516
|
+
}
|
517
|
+
}))
|
518
|
+
}), /*#__PURE__*/_jsx(IconButton, {
|
519
|
+
title: "Remove filter",
|
520
|
+
variant: "subtle",
|
521
|
+
icon: MinusIcon,
|
522
|
+
className: stl(_templateObject28 || (_templateObject28 = _taggedTemplateLiteral(["mt-8 shrink-0"]))),
|
523
|
+
disabled: form.values.filterGroups[parentIndex].length === 1,
|
524
|
+
onClick: function onClick() {
|
525
|
+
return remove(index);
|
526
|
+
}
|
527
|
+
})]
|
528
|
+
}, index);
|
529
|
+
}), /*#__PURE__*/_jsx(Button, {
|
530
|
+
variant: "subtle",
|
531
|
+
startIcon: PlusIcon,
|
532
|
+
onClick: function onClick() {
|
533
|
+
return push({
|
534
|
+
facet: undefined,
|
535
|
+
operator: "is",
|
536
|
+
value: undefined
|
537
|
+
});
|
538
|
+
},
|
539
|
+
children: "Or"
|
540
|
+
})]
|
541
|
+
});
|
542
|
+
}
|
543
|
+
});
|
544
|
+
};
|