@coveord/plasma-mantine 48.28.1 → 49.0.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/.turbo/turbo-build.log +3 -3
- package/.turbo/turbo-test.log +7 -7
- package/dist/.tsbuildinfo +1 -1
- package/dist/cjs/components/collection/Collection.js +21 -23
- package/dist/cjs/components/collection/Collection.js.map +1 -1
- package/dist/cjs/form/FormProvider.js +38 -0
- package/dist/cjs/form/FormProvider.js.map +1 -0
- package/dist/cjs/form/index.js +9 -0
- package/dist/cjs/form/index.js.map +1 -0
- package/dist/cjs/form/useForm.js +43 -0
- package/dist/cjs/form/useForm.js.map +1 -0
- package/dist/cjs/index.js +8 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/definitions/components/collection/Collection.d.ts +14 -4
- package/dist/definitions/components/collection/Collection.d.ts.map +1 -1
- package/dist/definitions/form/FormProvider.d.ts +8 -0
- package/dist/definitions/form/FormProvider.d.ts.map +1 -0
- package/dist/definitions/form/index.d.ts +3 -0
- package/dist/definitions/form/index.d.ts.map +1 -0
- package/dist/definitions/form/useForm.d.ts +3 -0
- package/dist/definitions/form/useForm.d.ts.map +1 -0
- package/dist/definitions/index.d.ts +2 -1
- package/dist/definitions/index.d.ts.map +1 -1
- package/dist/esm/components/collection/Collection.js +22 -24
- package/dist/esm/components/collection/Collection.js.map +1 -1
- package/dist/esm/form/FormProvider.js +27 -0
- package/dist/esm/form/FormProvider.js.map +1 -0
- package/dist/esm/form/index.js +4 -0
- package/dist/esm/form/index.js.map +1 -0
- package/dist/esm/form/useForm.js +33 -0
- package/dist/esm/form/useForm.js.map +1 -0
- package/dist/esm/index.js +2 -1
- package/dist/esm/index.js.map +1 -1
- package/package.json +4 -3
- package/src/components/collection/Collection.tsx +32 -19
- package/src/components/collection/__tests__/Collection.spec.tsx +1 -1
- package/src/form/FormProvider.tsx +38 -0
- package/src/form/index.ts +2 -0
- package/src/form/useForm.ts +26 -0
- package/src/index.ts +3 -1
|
@@ -12,15 +12,13 @@ var _interopRequireDefault = require("@swc/helpers/lib/_interop_require_default.
|
|
|
12
12
|
var _objectSpread = require("@swc/helpers/lib/_object_spread.js").default;
|
|
13
13
|
var _objectSpreadProps = require("@swc/helpers/lib/_object_spread_props.js").default;
|
|
14
14
|
var _objectWithoutProperties = require("@swc/helpers/lib/_object_without_properties.js").default;
|
|
15
|
-
var _slicedToArray = require("@swc/helpers/lib/_sliced_to_array.js").default;
|
|
16
15
|
var _jsxRuntime = require("react/jsx-runtime");
|
|
17
16
|
var _plasmaReactIcons = require("@coveord/plasma-react-icons");
|
|
18
17
|
var _core = require("@mantine/core");
|
|
19
18
|
var _hooks = require("@mantine/hooks");
|
|
20
19
|
var _reactBeautifulDnd = require("react-beautiful-dnd");
|
|
21
|
-
var _hooks1 = require("../../hooks");
|
|
22
|
-
var _collectionItem = require("./CollectionItem");
|
|
23
20
|
var _collectionStyles = /*#__PURE__*/ _interopRequireDefault(require("./Collection.styles"));
|
|
21
|
+
var _collectionItem = require("./CollectionItem");
|
|
24
22
|
var defaultProps = {
|
|
25
23
|
draggable: false,
|
|
26
24
|
addLabel: "Add item",
|
|
@@ -30,12 +28,13 @@ var defaultProps = {
|
|
|
30
28
|
required: false
|
|
31
29
|
};
|
|
32
30
|
var Collection = function(props) {
|
|
33
|
-
var _useComponentDefaultProps = (0, _core.useComponentDefaultProps)("Collection", defaultProps, props), value = _useComponentDefaultProps.value,
|
|
31
|
+
var _useComponentDefaultProps = (0, _core.useComponentDefaultProps)("Collection", defaultProps, props), value = _useComponentDefaultProps.value, onChange = _useComponentDefaultProps.onChange, onRemoveItem = _useComponentDefaultProps.onRemoveItem, onReorderItem = _useComponentDefaultProps.onReorderItem, onInsertItem = _useComponentDefaultProps.onInsertItem, disabled = _useComponentDefaultProps.disabled, draggable = _useComponentDefaultProps.draggable, children = _useComponentDefaultProps.children, spacing = _useComponentDefaultProps.spacing, required = _useComponentDefaultProps.required, newItem = _useComponentDefaultProps.newItem, addLabel = _useComponentDefaultProps.addLabel, addDisabledTooltip = _useComponentDefaultProps.addDisabledTooltip, allowAdd = _useComponentDefaultProps.allowAdd, label = _useComponentDefaultProps.label, labelProps = _useComponentDefaultProps.labelProps, description = _useComponentDefaultProps.description, descriptionProps = _useComponentDefaultProps.descriptionProps, error = _useComponentDefaultProps.error, errorProps = _useComponentDefaultProps.errorProps, // Style props
|
|
34
32
|
classNames = _useComponentDefaultProps.classNames, className = _useComponentDefaultProps.className, styles = _useComponentDefaultProps.styles, unstyled = _useComponentDefaultProps.unstyled, others = _objectWithoutProperties(_useComponentDefaultProps, [
|
|
35
33
|
"value",
|
|
36
|
-
"defaultValue",
|
|
37
34
|
"onChange",
|
|
38
35
|
"onRemoveItem",
|
|
36
|
+
"onReorderItem",
|
|
37
|
+
"onInsertItem",
|
|
39
38
|
"disabled",
|
|
40
39
|
"draggable",
|
|
41
40
|
"children",
|
|
@@ -63,18 +62,14 @@ var Collection = function(props) {
|
|
|
63
62
|
unstyled: unstyled
|
|
64
63
|
}), classes = _useStyles.classes, cx = _useStyles.cx;
|
|
65
64
|
var collectionID = (0, _hooks.useId)("dnd-droppable");
|
|
66
|
-
var
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
remove(index);
|
|
75
|
-
onRemoveItem === null || onRemoveItem === void 0 ? void 0 : onRemoveItem(index);
|
|
76
|
-
};
|
|
77
|
-
};
|
|
65
|
+
var hasOnlyOneItem = value.length === 1;
|
|
66
|
+
/**
|
|
67
|
+
* Enforcing onChange when the value is modified will make sure the errors are carried through.
|
|
68
|
+
*/ (0, _hooks.useDidUpdate)(function() {
|
|
69
|
+
onChange === null || onChange === void 0 ? void 0 : onChange(value);
|
|
70
|
+
}, [
|
|
71
|
+
JSON.stringify(value)
|
|
72
|
+
]);
|
|
78
73
|
var _label = label ? /*#__PURE__*/ (0, _jsxRuntime.jsx)(_core.Input.Label, _objectSpreadProps(_objectSpread({
|
|
79
74
|
required: required
|
|
80
75
|
}, labelProps), {
|
|
@@ -93,19 +88,22 @@ var Collection = function(props) {
|
|
|
93
88
|
_description
|
|
94
89
|
]
|
|
95
90
|
}) : null;
|
|
96
|
-
var items =
|
|
97
|
-
|
|
91
|
+
var items = value.map(function(item, index) {
|
|
92
|
+
/*#__PURE__*/ return (0, _jsxRuntime.jsx)(_collectionItem.CollectionItem, {
|
|
98
93
|
disabled: disabled,
|
|
99
94
|
draggable: draggable,
|
|
100
95
|
index: index,
|
|
101
|
-
onRemove:
|
|
96
|
+
onRemove: function() {
|
|
97
|
+
return onRemoveItem === null || onRemoveItem === void 0 ? void 0 : onRemoveItem(index);
|
|
98
|
+
},
|
|
102
99
|
styles: styles,
|
|
103
100
|
removable: !(required && hasOnlyOneItem),
|
|
104
101
|
children: children(item, index)
|
|
105
102
|
}, index);
|
|
106
103
|
});
|
|
107
104
|
var _allowAdd;
|
|
108
|
-
var addAllowed = (_allowAdd = allowAdd === null || allowAdd === void 0 ? void 0 : allowAdd(
|
|
105
|
+
var addAllowed = (_allowAdd = allowAdd === null || allowAdd === void 0 ? void 0 : allowAdd(value)) !== null && _allowAdd !== void 0 ? _allowAdd : true;
|
|
106
|
+
var _value_length;
|
|
109
107
|
var _addButton = disabled ? null : /*#__PURE__*/ (0, _jsxRuntime.jsx)(_core.Group, {
|
|
110
108
|
children: /*#__PURE__*/ (0, _jsxRuntime.jsx)(_core.Tooltip, {
|
|
111
109
|
label: addDisabledTooltip,
|
|
@@ -117,7 +115,7 @@ var Collection = function(props) {
|
|
|
117
115
|
height: 16
|
|
118
116
|
}),
|
|
119
117
|
onClick: function() {
|
|
120
|
-
return
|
|
118
|
+
return onInsertItem(newItem, (_value_length = value === null || value === void 0 ? void 0 : value.length) !== null && _value_length !== void 0 ? _value_length : 0);
|
|
121
119
|
},
|
|
122
120
|
disabled: !addAllowed,
|
|
123
121
|
children: addLabel
|
|
@@ -128,7 +126,7 @@ var Collection = function(props) {
|
|
|
128
126
|
return /*#__PURE__*/ (0, _jsxRuntime.jsx)(_reactBeautifulDnd.DragDropContext, {
|
|
129
127
|
onDragEnd: function(param) {
|
|
130
128
|
var destination = param.destination, source = param.source;
|
|
131
|
-
return
|
|
129
|
+
return onReorderItem({
|
|
132
130
|
from: source.index,
|
|
133
131
|
to: (destination === null || destination === void 0 ? void 0 : destination.index) || 0
|
|
134
132
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/components/collection/Collection.tsx"],"sourcesContent":["import {AddSize16Px} from '@coveord/plasma-react-icons';\nimport {\n Box,\n Button,\n DefaultProps,\n Group,\n Input,\n InputWrapperBaseProps,\n MantineNumberSize,\n Selectors,\n Stack,\n Tooltip,\n useComponentDefaultProps,\n} from '@mantine/core';\nimport {useId} from '@mantine/hooks';\nimport {ReactNode} from 'react';\nimport {DragDropContext, Droppable} from 'react-beautiful-dnd';\nimport {useControlledList} from '../../hooks';\nimport {CollectionItem} from './CollectionItem';\nimport useStyles from './Collection.styles';\n\ninterface CollectionProps<T>\n extends Omit<InputWrapperBaseProps, 'inputContainer' | 'inputWrapperOrder'>,\n DefaultProps<Selectors<typeof useStyles>> {\n /**\n * The default value each new item should have\n */\n newItem: T;\n /**\n * A render function called for each item passed in the `value` prop.\n *\n * @param item The current item's value\n * @param index The current item's index\n */\n children: (item: T, index: number) => ReactNode;\n /**\n * The list of items to display inside the collection\n *\n * @default []\n */\n value?: T[];\n /**\n * The initial items of the collection (for uncontrolled usage only)\n */\n defaultValue?: T[];\n /**\n * Unused, has no effect\n */\n onFocus?: () => void;\n /**\n * Function called whenever the value needs to be updated\n *\n * @param value The whole list of items after the change\n */\n onChange?: (value: T[]) => void;\n /**\n * Function called after an item is removed from the collection using the remove button\n *\n * @param itemIndex The index of the item that was removed\n */\n onRemoveItem?: (itemIndex: number) => void;\n /**\n * Whether the collection should have drag and drop behavior enabled\n *\n * @default false\n */\n draggable?: boolean;\n /**\n * Whether the collection is disabled, or in other words in read only mode\n *\n * @default false\n */\n disabled?: boolean;\n /**\n * Function that determines if the add item button should be enabled given the current items of the collection.\n * The button is always enabled if this props remains undefined\n *\n * @param values The current items of the collection\n */\n allowAdd?: (values: T[]) => boolean;\n /**\n * The label of the add item button\n *\n * @default \"Add item\"\n */\n addLabel?: string;\n /**\n * The tooltip text displayed when hovering over the disabled add item button\n *\n * @default 'There is already an empty item'\n */\n addDisabledTooltip?: string;\n /**\n * The spacing between the colleciton items\n *\n * @default 'xs'\n */\n spacing?: MantineNumberSize;\n /**\n * Whether the collection is required. When required is true, the collection will hide the remove button if there is only one item\n *\n * @default false\n */\n required?: boolean;\n}\n\nconst defaultProps: Partial<CollectionProps<unknown>> = {\n draggable: false,\n addLabel: 'Add item',\n addDisabledTooltip: 'There is already an empty item',\n disabled: false,\n spacing: 'xs',\n required: false,\n};\n\nexport const Collection = <T,>(props: CollectionProps<T>) => {\n const {\n value,\n defaultValue,\n onChange,\n onRemoveItem,\n disabled,\n draggable,\n children,\n spacing,\n required,\n newItem,\n addLabel,\n addDisabledTooltip,\n allowAdd,\n label,\n labelProps,\n description,\n descriptionProps,\n error,\n errorProps,\n\n // Style props\n classNames,\n className,\n styles,\n unstyled,\n\n ...others\n } = useComponentDefaultProps('Collection', defaultProps as CollectionProps<T>, props);\n const {classes, cx} = useStyles(null, {classNames, name: 'Collection', styles, unstyled});\n const collectionID = useId('dnd-droppable');\n\n const [values, {append, remove, reorder}] = useControlledList({value, onChange, defaultValue});\n const hasOnlyOneItem = values.length === 1;\n const removeItem = (index: number) => () => {\n remove(index);\n onRemoveItem?.(index);\n };\n\n const _label = label ? (\n <Input.Label required={required} {...labelProps}>\n {label}\n </Input.Label>\n ) : null;\n\n const _description = description ? (\n <Input.Description {...descriptionProps}>{description}</Input.Description>\n ) : null;\n const _error = error ? <Input.Error {...errorProps}>{error}</Input.Error> : null;\n const _header =\n _label || _description ? (\n <Box mb=\"sm\">\n {_label}\n {_description}\n </Box>\n ) : null;\n\n const items = values.map((item, index) => (\n <CollectionItem\n key={index}\n disabled={disabled}\n draggable={draggable}\n index={index}\n onRemove={removeItem(index)}\n styles={styles}\n removable={!(required && hasOnlyOneItem)}\n >\n {children(item, index)}\n </CollectionItem>\n ));\n\n const addAllowed = allowAdd?.(values) ?? true;\n\n const _addButton = disabled ? null : (\n <Group>\n <Tooltip label={addDisabledTooltip} disabled={addAllowed}>\n <Box>\n <Button\n variant=\"subtle\"\n leftIcon={<AddSize16Px height={16} />}\n onClick={() => append(newItem)}\n disabled={!addAllowed}\n >\n {addLabel}\n </Button>\n </Box>\n </Tooltip>\n </Group>\n );\n\n return (\n <DragDropContext\n onDragEnd={({destination, source}) => reorder({from: source.index, to: destination?.index || 0})}\n >\n <Droppable direction=\"vertical\" droppableId={collectionID}>\n {(provided) => (\n <Box\n {...provided.droppableProps}\n ref={provided.innerRef}\n className={cx(classes.root, className)}\n {...others}\n >\n {_header}\n <Stack spacing={spacing}>\n {items}\n {provided.placeholder}\n {_addButton}\n {_error}\n </Stack>\n </Box>\n )}\n </Droppable>\n </DragDropContext>\n );\n};\n"],"names":["Collection","defaultProps","draggable","addLabel","addDisabledTooltip","disabled","spacing","required","props","useComponentDefaultProps","value","defaultValue","onChange","onRemoveItem","children","newItem","allowAdd","label","labelProps","description","descriptionProps","error","errorProps","classNames","className","styles","unstyled","others","useStyles","name","classes","cx","collectionID","useId","useControlledList","values","append","remove","reorder","hasOnlyOneItem","length","removeItem","index","_label","Input","Label","_description","Description","_error","Error","_header","Box","mb","items","map","item","CollectionItem","onRemove","removable","addAllowed","_addButton","Group","Tooltip","Button","variant","leftIcon","AddSize16Px","height","onClick","DragDropContext","onDragEnd","destination","source","from","to","Droppable","direction","droppableId","provided","droppableProps","ref","innerRef","root","Stack","placeholder"],"mappings":"AAAA;;;;+BAmHaA;;;eAAAA;;;;;;;;;gCAnHa;oBAanB;qBACa;iCAEqB;sBACT;8BACH;qEACP;AAuFtB,IAAMC,eAAkD;IACpDC,WAA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AAe;QACZC,WAAW;gBAAEC,oBAAAA,aAAaC,eAAAA;YAAYlC,OAAAA,QAAQ;gBAACmC,MAAMD,OAAO9B,KAAK;gBAAEgC,IAAIH,CAAAA,wBAAAA,yBAAAA,KAAAA,IAAAA,YAAa7B,KAAK,AAAD,KAAK;YAAC;;kBAE9F,cAAA,qBAACiC,4BAAS;YAACC,WAAU;YAAWC,aAAa7C;sBACxC,SAAC8C;qCACE,sBAAC3B,SAAG,wEACI2B,SAASC,cAAc;oBAC3BC,KAAKF,SAASG,QAAQ;oBACtBzD,WAAWO,GAAGD,QAAQoD,IAAI,EAAE1D;oBACxBG;;wBAEHuB;sCACD,sBAACiC,WAAK;4BAAC7E,SAASA;;gCACX+C;gCACAyB,SAASM,WAAW;gCACpBxB;gCACAZ;;;;;;;;AAO7B"}
|
|
1
|
+
{"version":3,"sources":["../../../../src/components/collection/Collection.tsx"],"sourcesContent":["import {AddSize16Px} from '@coveord/plasma-react-icons';\nimport {\n Box,\n Button,\n DefaultProps,\n Group,\n Input,\n InputWrapperBaseProps,\n MantineNumberSize,\n Selectors,\n Stack,\n Tooltip,\n useComponentDefaultProps,\n} from '@mantine/core';\nimport {ReorderPayload} from '@mantine/form/lib/types';\nimport {useDidUpdate, useId} from '@mantine/hooks';\nimport {ReactNode} from 'react';\nimport {DragDropContext, Droppable} from 'react-beautiful-dnd';\n\nimport useStyles from './Collection.styles';\nimport {CollectionItem} from './CollectionItem';\n\ninterface CollectionProps<T>\n extends Omit<InputWrapperBaseProps, 'inputContainer' | 'inputWrapperOrder'>,\n DefaultProps<Selectors<typeof useStyles>> {\n /**\n * The default value each new item should have\n */\n newItem: T;\n /**\n * A render function called for each item passed in the `value` prop.\n *\n * @param item The current item's value\n * @param index The current item's index\n */\n children: (item: T, index: number) => ReactNode;\n /**\n * The list of items to display inside the collection\n *\n * @default []\n */\n value?: T[];\n /**\n * Unused, has no effect\n */\n onFocus?: () => void;\n /**\n * Function called whenever the value needs to be updated\n *\n * @param value The whole list of items after the change\n */\n onChange?: (value: T[]) => void;\n /**\n * Function called after an item is removed from the collection using the remove button\n *\n * @param itemIndex The index of the item that was removed\n */\n onRemoveItem?: (itemIndex: number) => void;\n /**\n * Function that gets called whenever a collection item needs to be reordered\n *\n * @param payload The origin and destination index of the item to reorder\n */\n onReorderItem?: (payload: ReorderPayload) => void;\n /**\n * Function that gets called when a new item needs to be added to the collection\n *\n * @param value The the value of the item to insert\n * @param index The index of the new item to insert\n */\n onInsertItem?: (value: T, index: number) => void;\n /**\n * Whether the collection should have drag and drop behavior enabled\n *\n * @default false\n */\n draggable?: boolean;\n /**\n * Whether the collection is disabled, or in other words in read only mode\n *\n * @default false\n */\n disabled?: boolean;\n /**\n * Function that determines if the add item button should be enabled given the current items of the collection.\n * The button is always enabled if this props remains undefined\n *\n * @param values The current items of the collection\n */\n allowAdd?: (values: T[]) => boolean;\n /**\n * The label of the add item button\n *\n * @default \"Add item\"\n */\n addLabel?: string;\n /**\n * The tooltip text displayed when hovering over the disabled add item button\n *\n * @default 'There is already an empty item'\n */\n addDisabledTooltip?: string;\n /**\n * The spacing between the colleciton items\n *\n * @default 'xs'\n */\n spacing?: MantineNumberSize;\n /**\n * Whether the collection is required. When required is true, the collection will hide the remove button if there is only one item\n *\n * @default false\n */\n required?: boolean;\n}\n\nconst defaultProps: Partial<CollectionProps<unknown>> = {\n draggable: false,\n addLabel: 'Add item',\n addDisabledTooltip: 'There is already an empty item',\n disabled: false,\n spacing: 'xs',\n required: false,\n};\n\nexport const Collection = <T,>(props: CollectionProps<T>) => {\n const {\n value,\n onChange,\n onRemoveItem,\n onReorderItem,\n onInsertItem,\n disabled,\n draggable,\n children,\n spacing,\n required,\n newItem,\n addLabel,\n addDisabledTooltip,\n allowAdd,\n label,\n labelProps,\n description,\n descriptionProps,\n error,\n errorProps,\n\n // Style props\n classNames,\n className,\n styles,\n unstyled,\n\n ...others\n } = useComponentDefaultProps('Collection', defaultProps as CollectionProps<T>, props);\n const {classes, cx} = useStyles(null, {classNames, name: 'Collection', styles, unstyled});\n const collectionID = useId('dnd-droppable');\n\n const hasOnlyOneItem = value.length === 1;\n\n /**\n * Enforcing onChange when the value is modified will make sure the errors are carried through.\n */\n useDidUpdate(() => {\n onChange?.(value);\n }, [JSON.stringify(value)]);\n\n const _label = label ? (\n <Input.Label required={required} {...labelProps}>\n {label}\n </Input.Label>\n ) : null;\n\n const _description = description ? (\n <Input.Description {...descriptionProps}>{description}</Input.Description>\n ) : null;\n const _error = error ? <Input.Error {...errorProps}>{error}</Input.Error> : null;\n const _header =\n _label || _description ? (\n <Box mb=\"sm\">\n {_label}\n {_description}\n </Box>\n ) : null;\n\n const items = value.map((item, index) => (\n <CollectionItem\n key={index}\n disabled={disabled}\n draggable={draggable}\n index={index}\n onRemove={() => onRemoveItem?.(index)}\n styles={styles}\n removable={!(required && hasOnlyOneItem)}\n >\n {children(item, index)}\n </CollectionItem>\n ));\n\n const addAllowed = allowAdd?.(value) ?? true;\n\n const _addButton = disabled ? null : (\n <Group>\n <Tooltip label={addDisabledTooltip} disabled={addAllowed}>\n <Box>\n <Button\n variant=\"subtle\"\n leftIcon={<AddSize16Px height={16} />}\n onClick={() => onInsertItem(newItem, value?.length ?? 0)}\n disabled={!addAllowed}\n >\n {addLabel}\n </Button>\n </Box>\n </Tooltip>\n </Group>\n );\n\n return (\n <DragDropContext\n onDragEnd={({destination, source}) => onReorderItem({from: source.index, to: destination?.index || 0})}\n >\n <Droppable direction=\"vertical\" droppableId={collectionID}>\n {(provided) => (\n <Box\n {...provided.droppableProps}\n ref={provided.innerRef}\n className={cx(classes.root, className)}\n {...others}\n >\n {_header}\n <Stack spacing={spacing}>\n {items}\n {provided.placeholder}\n {_addButton}\n {_error}\n </Stack>\n </Box>\n )}\n </Droppable>\n </DragDropContext>\n );\n};\n"],"names":["Collection","defaultProps","draggable","addLabel","addDisabledTooltip","disabled","spacing","required","props","useComponentDefaultProps","value","onChange","onRemoveItem","onReorderItem","onInsertItem","children","newItem","allowAdd","label","labelProps","description","descriptionProps","error","errorProps","classNames","className","styles","unstyled","others","useStyles","name","classes","cx","collectionID","useId","hasOnlyOneItem","length","useDidUpdate","JSON","stringify","_label","Input","Label","_description","Description","_error","Error","_header","Box","mb","items","map","item","index","CollectionItem","onRemove","removable","addAllowed","_addButton","Group","Tooltip","Button","variant","leftIcon","AddSize16Px","height","onClick","DragDropContext","onDragEnd","destination","source","from","to","Droppable","direction","droppableId","provided","droppableProps","ref","innerRef","root","Stack","placeholder"],"mappings":"AAAA;;;;+BA6HaA;;;eAAAA;;;;;;;;gCA7Ha;oBAanB;qBAE2B;iCAEO;qEAEnB;8BACO;AAgG7B,IAAMC,eAAkD;IACpDC,WAAW,KAAK;IAChBC,UAAU;IACVC,oBAAoB;IACpBC,UAAU,KAAK;IACfC,SAAS;IACTC,UAAU,KAAK;AACnB;AAEO,IAAMP,aAAa,SAAKQ,OAA8B;IACzD,IA6BIC,4BAAAA,IAAAA,8BAAwB,EAAC,cAAcR,cAAoCO,QA5B3EE,QA4BAD,0BA5BAC,OACAC,WA2BAF,0BA3BAE,UACAC,eA0BAH,0BA1BAG,cACAC,gBAyBAJ,0BAzBAI,eACAC,eAwBAL,0BAxBAK,cACAT,WAuBAI,0BAvBAJ,UACAH,YAsBAO,0BAtBAP,WACAa,WAqBAN,0BArBAM,UACAT,UAoBAG,0BApBAH,SACAC,WAmBAE,0BAnBAF,UACAS,UAkBAP,0BAlBAO,SACAb,WAiBAM,0BAjBAN,UACAC,qBAgBAK,0BAhBAL,oBACAa,WAeAR,0BAfAQ,UACAC,QAcAT,0BAdAS,OACAC,aAaAV,0BAbAU,YACAC,cAYAX,0BAZAW,aACAC,mBAWAZ,0BAXAY,kBACAC,QAUAb,0BAVAa,OACAC,aASAd,0BATAc,YAEA,cAAc;IACdC,aAMAf,0BANAe,YACAC,YAKAhB,0BALAgB,WACAC,SAIAjB,0BAJAiB,QACAC,WAGAlB,0BAHAkB,UAEGC,kCACHnB;QA5BAC;QACAC;QACAC;QACA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A,SAAWP,oBAAXO,uBAAAA,YAAqB,IAAI;QASaP;IAPzD,IAAMgD,aAAarD,WAAW,IAAI,iBAC9B,qBAACsD,WAAK;kBACF,cAAA,qBAACC,aAAO;YAAC1C,OAAOd;YAAoBC,UAAUoD;sBAC1C,cAAA,qBAACT,SAAG;0BACA,cAAA,qBAACa,YAAM;oBACHC,SAAQ;oBACRC,wBAAU,qBAACC,6BAAW;wBAACC,QAAQ;;oBAC/BC,SAAS;wBAAMpD,OAAAA,aAAaE,SAASN,CAAAA,gBAAAA,kBAAAA,mBAAAA,KAAAA,IAAAA,MAAO0B,MAAM,cAAb1B,2BAAAA,gBAAiB,CAAC;;oBACvDL,UAAU,CAACoD;8BAEVtD;;;;MAKpB;IAED,qBACI,qBAACgE,kCAAe;QACZC,WAAW;gBAAEC,oBAAAA,aAAaC,eAAAA;YAAYzD,OAAAA,cAAc;gBAAC0D,MAAMD,OAAOjB,KAAK;gBAAEmB,IAAIH,CAAAA,wBAAAA,yBAAAA,KAAAA,IAAAA,YAAahB,KAAK,AAAD,KAAK;YAAC;;kBAEpG,cAAA,qBAACoB,4BAAS;YAACC,WAAU;YAAWC,aAAa1C;sBACxC,SAAC2C;qCACE,sBAAC5B,SAAG,wEACI4B,SAASC,cAAc;oBAC3BC,KAAKF,SAASG,QAAQ;oBACtBtD,WAAWO,GAAGD,QAAQiD,IAAI,EAAEvD;oBACxBG;;wBAEHmB;sCACD,sBAACkC,WAAK;4BAAC3E,SAASA;;gCACX4C;gCACA0B,SAASM,WAAW;gCACpBxB;gCACAb;;;;;;;;AAO7B"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/* eslint-disable prefer-arrow/prefer-arrow-functions */ "use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "createFormContext", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return createFormContext;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
var _interopRequireWildcard = require("@swc/helpers/lib/_interop_require_wildcard.js").default;
|
|
12
|
+
var _jsxRuntime = require("react/jsx-runtime");
|
|
13
|
+
var _react = /*#__PURE__*/ _interopRequireWildcard(require("react"));
|
|
14
|
+
var _useForm = require("./useForm");
|
|
15
|
+
function createFormContext() {
|
|
16
|
+
var FormProvider = function FormProvider(param) {
|
|
17
|
+
var form = param.form, children = param.children;
|
|
18
|
+
return /*#__PURE__*/ (0, _jsxRuntime.jsx)(FormContext.Provider, {
|
|
19
|
+
value: form,
|
|
20
|
+
children: children
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
var useFormContext = function useFormContext() {
|
|
24
|
+
var ctx = (0, _react.useContext)(FormContext);
|
|
25
|
+
if (!ctx) {
|
|
26
|
+
throw new Error("useFormContext was called outside of FormProvider context");
|
|
27
|
+
}
|
|
28
|
+
return ctx;
|
|
29
|
+
};
|
|
30
|
+
var FormContext = /*#__PURE__*/ (0, _react.createContext)(null);
|
|
31
|
+
return [
|
|
32
|
+
FormProvider,
|
|
33
|
+
useFormContext,
|
|
34
|
+
_useForm.useForm
|
|
35
|
+
];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
//# sourceMappingURL=FormProvider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/form/FormProvider.tsx"],"sourcesContent":["/* eslint-disable prefer-arrow/prefer-arrow-functions */\nimport {_TransformValues, UseForm, UseFormReturnType} from '@mantine/form/lib/types';\nimport React, {createContext, useContext} from 'react';\n\nimport {useForm} from './useForm';\n\nexport interface FormProviderProps<Form> {\n form: Form;\n children: React.ReactNode;\n}\n\nexport function createFormContext<\n Values,\n TransformValues extends _TransformValues<Values> = (values: Values) => Values\n>() {\n type Form = UseFormReturnType<Values, TransformValues>;\n\n const FormContext = createContext<Form>(null);\n\n function FormProvider({form, children}: FormProviderProps<Form>) {\n return <FormContext.Provider value={form}>{children}</FormContext.Provider>;\n }\n\n function useFormContext() {\n const ctx = useContext(FormContext);\n if (!ctx) {\n throw new Error('useFormContext was called outside of FormProvider context');\n }\n\n return ctx;\n }\n\n return [FormProvider, useFormContext, useForm] as [\n React.FC<FormProviderProps<Form>>,\n () => Form,\n UseForm<Values, TransformValues>\n ];\n}\n"],"names":["createFormContext","FormProvider","form","children","FormContext","Provider","value","useFormContext","ctx","useContext","Error","createContext","useForm"],"mappings":"AAAA,sDAAsD,GACtD;;;;+BAUgBA;;;eAAAA;;;;;2DAT+B;uBAEzB;AAOf,SAASA,oBAGZ;QAKSC,eAAT,SAASA,aAAa,KAAyC,EAAE;YAA1CC,OAAD,MAACA,MAAMC,WAAP,MAAOA;QACzB,qBAAO,qBAACC,YAAYC,QAAQ;YAACC,OAAOJ;sBAAOC;;IAC/C;QAESI,iBAAT,SAASA,iBAAiB;QACtB,IAAMC,MAAMC,IAAAA,iBAAU,EAACL;QACvB,IAAI,CAACI,KAAK;YACN,MAAM,IAAIE,MAAM,6DAA6D;QACjF,CAAC;QAED,OAAOF;IACX;IAbA,IAAMJ,4BAAcO,IAAAA,oBAAa,EAAO,IAAI;IAe5C,OAAO;QAACV;QAAcM;QAAgBK,gBAAO;KAAC;AAKlD"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
var _exportStar = require("@swc/helpers/lib/_export_star.js").default;
|
|
6
|
+
_exportStar(require("./useForm"), exports);
|
|
7
|
+
_exportStar(require("./FormProvider"), exports);
|
|
8
|
+
|
|
9
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/form/index.ts"],"sourcesContent":["export * from './useForm';\nexport * from './FormProvider';\n"],"names":[],"mappings":"AAAA;;;;;oBAAc;oBACA"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "useForm", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return useForm;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
var _objectSpread = require("@swc/helpers/lib/_object_spread.js").default;
|
|
12
|
+
var _objectSpreadProps = require("@swc/helpers/lib/_object_spread_props.js").default;
|
|
13
|
+
var _form = require("@mantine/form");
|
|
14
|
+
var useForm = function(options) {
|
|
15
|
+
var form = (0, _form.useForm)(options);
|
|
16
|
+
var getInputProps = function(path) {
|
|
17
|
+
var _ref = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {}, _ref_type = _ref.type, type = _ref_type === void 0 ? "input" : _ref_type, _ref_withError = _ref.withError, withError = _ref_withError === void 0 ? type === "input" : _ref_withError, _ref_withFocus = _ref.withFocus, withFocus = _ref_withFocus === void 0 ? true : _ref_withFocus;
|
|
18
|
+
var originalPayload = form.getInputProps(path, {
|
|
19
|
+
type: type,
|
|
20
|
+
withError: withError,
|
|
21
|
+
withFocus: withFocus
|
|
22
|
+
});
|
|
23
|
+
if (Array.isArray(originalPayload.value)) {
|
|
24
|
+
return _objectSpreadProps(_objectSpread({}, originalPayload), {
|
|
25
|
+
onReorderItem: function(payload) {
|
|
26
|
+
return form.reorderListItem(path, payload);
|
|
27
|
+
},
|
|
28
|
+
onRemoveItem: function(index) {
|
|
29
|
+
return form.removeListItem(path, index);
|
|
30
|
+
},
|
|
31
|
+
onInsertItem: function(valueToInsert, index) {
|
|
32
|
+
return form.insertListItem(path, valueToInsert, index);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
return originalPayload;
|
|
37
|
+
};
|
|
38
|
+
return _objectSpreadProps(_objectSpread({}, form), {
|
|
39
|
+
getInputProps: getInputProps
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
//# sourceMappingURL=useForm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/form/useForm.ts"],"sourcesContent":["import {useForm as useMantineForm} from '@mantine/form';\nimport {GetInputProps} from '@mantine/form/lib/types';\n\nexport const useForm: typeof useMantineForm = (options) => {\n const form = useMantineForm(options);\n\n const getInputProps: GetInputProps<Record<string, unknown>> = (\n path,\n {type = 'input', withError = type === 'input', withFocus = true} = {}\n ) => {\n const originalPayload = form.getInputProps(path, {type, withError, withFocus});\n if (Array.isArray(originalPayload.value)) {\n return {\n ...originalPayload,\n onReorderItem: (payload: Record<'from' | 'to', number>) => form.reorderListItem(path, payload),\n onRemoveItem: (index: number) => form.removeListItem(path, index),\n onInsertItem: (valueToInsert: unknown, index: number) =>\n form.insertListItem(path, valueToInsert, index),\n };\n }\n\n return originalPayload;\n };\n\n return {...form, getInputProps};\n};\n"],"names":["useForm","options","form","useMantineForm","getInputProps","path","type","withError","withFocus","originalPayload","Array","isArray","value","onReorderItem","payload","reorderListItem","onRemoveItem","index","removeListItem","onInsertItem","valueToInsert","insertListItem"],"mappings":"AAAA;;;;+BAGaA;;;eAAAA;;;;;oBAH2B;AAGjC,IAAMA,UAAiC,SAACC,SAAY;IACvD,IAAMC,OAAOC,IAAAA,aAAc,EAACF;IAE5B,IAAMG,gBAAwD,SAC1DC,MAEC;oFADkE,CAAC,oBAAnEC,MAAAA,8BAAO,2CAASC,WAAAA,wCAAYD,SAAS,gDAASE,WAAAA,wCAAY,IAAI;QAE/D,IAAMC,kBAAkBP,KAAKE,aAAa,CAACC,MAAM;YAACC,MAAAA;YAAMC,WAAAA;YAAWC,WAAAA;QAAS;QAC5E,IAAIE,MAAMC,OAAO,CAACF,gBAAgBG,KAAK,GAAG;YACtC,OAAO,qCACAH;gBACHI,eAAe,SAACC;2BAA2CZ,KAAKa,eAAe,CAACV,MAAMS;;gBACtFE,cAAc,SAACC;2BAAkBf,KAAKgB,cAAc,CAACb,MAAMY;;gBAC3DE,cAAc,SAACC,eAAwBH;2BACnCf,KAAKmB,cAAc,CAAChB,MAAMe,eAAeH;;;QAErD,CAAC;QAED,OAAOR;IACX;IAEA,OAAO,qCAAIP;QAAME,eAAAA;;AACrB"}
|
package/dist/cjs/index.js
CHANGED
|
@@ -20,15 +20,22 @@ _export(exports, {
|
|
|
20
20
|
},
|
|
21
21
|
Modal: function() {
|
|
22
22
|
return _components.Modal;
|
|
23
|
+
},
|
|
24
|
+
useForm: function() {
|
|
25
|
+
return _form.useForm;
|
|
26
|
+
},
|
|
27
|
+
createFormContext: function() {
|
|
28
|
+
return _form.createFormContext;
|
|
23
29
|
}
|
|
24
30
|
});
|
|
25
31
|
var _exportStar = require("@swc/helpers/lib/_export_star.js").default;
|
|
26
32
|
_exportStar(require("@mantine/carousel"), exports);
|
|
27
33
|
_exportStar(require("@mantine/core"), exports);
|
|
28
|
-
_exportStar(require("@mantine/form"), exports);
|
|
29
34
|
_exportStar(require("@mantine/hooks"), exports);
|
|
30
35
|
var _tableCore = require("@tanstack/table-core");
|
|
31
36
|
var _components = _exportStar(require("./components"), exports);
|
|
37
|
+
_exportStar(require("@mantine/form"), exports);
|
|
38
|
+
var _form = require("./form");
|
|
32
39
|
_exportStar(require("./theme"), exports);
|
|
33
40
|
|
|
34
41
|
//# sourceMappingURL=index.js.map
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["import {Tuple} from '@mantine/core';\n\nimport {PlasmaColors} from './theme/PlasmaColors';\n\nexport * from '@mantine/carousel';\nexport * from '@mantine/core';\nexport
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["import {Tuple} from '@mantine/core';\n\nimport {PlasmaColors} from './theme/PlasmaColors';\n\nexport * from '@mantine/carousel';\nexport * from '@mantine/core';\nexport type {FormValidateInput} from '@mantine/form/lib/types';\nexport * from '@mantine/hooks';\nexport {createColumnHelper, type ColumnDef} from '@tanstack/table-core';\nexport * from './components';\nexport * from '@mantine/form';\n// explicitly overriding mantine components\nexport {Header, Table, type HeaderProps, Modal} from './components';\nexport {useForm, createFormContext} from './form';\n\nexport * from './theme';\n\ndeclare module '@mantine/core' {\n export interface MantineThemeColorsOverride {\n // eslint-disable-next-line @typescript-eslint/ban-types\n colors: Record<keyof typeof PlasmaColors | (string & {}), Tuple<string, 10>>;\n }\n}\n"],"names":["createColumnHelper","Header","Table","Modal","useForm","createFormContext"],"mappings":"AAAA;;;;;;;;;;;IAQQA,kBAAkB;eAAlBA,6BAAkB;;IAIlBC,MAAM;eAANA,kBAAM;;IAAEC,KAAK;eAALA,iBAAK;;IAAoBC,KAAK;eAALA,iBAAK;;IACtCC,OAAO;eAAPA,aAAO;;IAAEC,iBAAiB;eAAjBA,uBAAiB;;;;oBATpB;oBACA;oBAEA;yBACmC;sCACnC;oBACA;oBAG2B;oBAE3B"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { DefaultProps, InputWrapperBaseProps, MantineNumberSize, Selectors } from '@mantine/core';
|
|
2
|
+
import { ReorderPayload } from '@mantine/form/lib/types';
|
|
2
3
|
import { ReactNode } from 'react';
|
|
3
4
|
import useStyles from './Collection.styles';
|
|
4
5
|
interface CollectionProps<T> extends Omit<InputWrapperBaseProps, 'inputContainer' | 'inputWrapperOrder'>, DefaultProps<Selectors<typeof useStyles>> {
|
|
@@ -19,10 +20,6 @@ interface CollectionProps<T> extends Omit<InputWrapperBaseProps, 'inputContainer
|
|
|
19
20
|
* @default []
|
|
20
21
|
*/
|
|
21
22
|
value?: T[];
|
|
22
|
-
/**
|
|
23
|
-
* The initial items of the collection (for uncontrolled usage only)
|
|
24
|
-
*/
|
|
25
|
-
defaultValue?: T[];
|
|
26
23
|
/**
|
|
27
24
|
* Unused, has no effect
|
|
28
25
|
*/
|
|
@@ -39,6 +36,19 @@ interface CollectionProps<T> extends Omit<InputWrapperBaseProps, 'inputContainer
|
|
|
39
36
|
* @param itemIndex The index of the item that was removed
|
|
40
37
|
*/
|
|
41
38
|
onRemoveItem?: (itemIndex: number) => void;
|
|
39
|
+
/**
|
|
40
|
+
* Function that gets called whenever a collection item needs to be reordered
|
|
41
|
+
*
|
|
42
|
+
* @param payload The origin and destination index of the item to reorder
|
|
43
|
+
*/
|
|
44
|
+
onReorderItem?: (payload: ReorderPayload) => void;
|
|
45
|
+
/**
|
|
46
|
+
* Function that gets called when a new item needs to be added to the collection
|
|
47
|
+
*
|
|
48
|
+
* @param value The the value of the item to insert
|
|
49
|
+
* @param index The index of the new item to insert
|
|
50
|
+
*/
|
|
51
|
+
onInsertItem?: (value: T, index: number) => void;
|
|
42
52
|
/**
|
|
43
53
|
* Whether the collection should have drag and drop behavior enabled
|
|
44
54
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Collection.d.ts","sourceRoot":"","sources":["../../../../src/components/collection/Collection.tsx"],"names":[],"mappings":"AACA,OAAO,EAGH,YAAY,EAGZ,qBAAqB,EACrB,iBAAiB,EACjB,SAAS,EAIZ,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"Collection.d.ts","sourceRoot":"","sources":["../../../../src/components/collection/Collection.tsx"],"names":[],"mappings":"AACA,OAAO,EAGH,YAAY,EAGZ,qBAAqB,EACrB,iBAAiB,EACjB,SAAS,EAIZ,MAAM,eAAe,CAAC;AACvB,OAAO,EAAC,cAAc,EAAC,MAAM,yBAAyB,CAAC;AAEvD,OAAO,EAAC,SAAS,EAAC,MAAM,OAAO,CAAC;AAGhC,OAAO,SAAS,MAAM,qBAAqB,CAAC;AAG5C,UAAU,eAAe,CAAC,CAAC,CACvB,SAAQ,IAAI,CAAC,qBAAqB,EAAE,gBAAgB,GAAG,mBAAmB,CAAC,EACvE,YAAY,CAAC,SAAS,CAAC,OAAO,SAAS,CAAC,CAAC;IAC7C;;OAEG;IACH,OAAO,EAAE,CAAC,CAAC;IACX;;;;;OAKG;IACH,QAAQ,EAAE,CAAC,IAAI,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,KAAK,SAAS,CAAC;IAChD;;;;OAIG;IACH,KAAK,CAAC,EAAE,CAAC,EAAE,CAAC;IACZ;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,IAAI,CAAC;IACrB;;;;OAIG;IACH,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,EAAE,KAAK,IAAI,CAAC;IAChC;;;;OAIG;IACH,YAAY,CAAC,EAAE,CAAC,SAAS,EAAE,MAAM,KAAK,IAAI,CAAC;IAC3C;;;;OAIG;IACH,aAAa,CAAC,EAAE,CAAC,OAAO,EAAE,cAAc,KAAK,IAAI,CAAC;IAClD;;;;;OAKG;IACH,YAAY,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IACjD;;;;OAIG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB;;;;OAIG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB;;;;;OAKG;IACH,QAAQ,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,EAAE,KAAK,OAAO,CAAC;IACpC;;;;OAIG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;;;OAIG;IACH,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B;;;;OAIG;IACH,OAAO,CAAC,EAAE,iBAAiB,CAAC;IAC5B;;;;OAIG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;CACtB;AAWD,eAAO,MAAM,UAAU,+CAsHtB,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { _TransformValues, UseForm, UseFormReturnType } from '@mantine/form/lib/types';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
export interface FormProviderProps<Form> {
|
|
4
|
+
form: Form;
|
|
5
|
+
children: React.ReactNode;
|
|
6
|
+
}
|
|
7
|
+
export declare function createFormContext<Values, TransformValues extends _TransformValues<Values> = (values: Values) => Values>(): [React.FC<FormProviderProps<UseFormReturnType<Values, TransformValues>>>, () => UseFormReturnType<Values, TransformValues>, UseForm<Values, TransformValues>];
|
|
8
|
+
//# sourceMappingURL=FormProvider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FormProvider.d.ts","sourceRoot":"","sources":["../../../src/form/FormProvider.tsx"],"names":[],"mappings":"AACA,OAAO,EAAC,gBAAgB,EAAE,OAAO,EAAE,iBAAiB,EAAC,MAAM,yBAAyB,CAAC;AACrF,OAAO,KAAkC,MAAM,OAAO,CAAC;AAIvD,MAAM,WAAW,iBAAiB,CAAC,IAAI;IACnC,IAAI,EAAE,IAAI,CAAC;IACX,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;CAC7B;AAED,wBAAgB,iBAAiB,CAC7B,MAAM,EACN,eAAe,SAAS,gBAAgB,CAAC,MAAM,CAAC,GAAG,CAAC,MAAM,EAAE,MAAM,KAAK,MAAM,mKAwBhF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/form/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,gBAAgB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useForm.d.ts","sourceRoot":"","sources":["../../../src/form/useForm.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,OAAO,IAAI,cAAc,EAAC,MAAM,eAAe,CAAC;AAGxD,eAAO,MAAM,OAAO,EAAE,OAAO,cAsB5B,CAAC"}
|
|
@@ -2,12 +2,13 @@ import { Tuple } from '@mantine/core';
|
|
|
2
2
|
import { PlasmaColors } from './theme/PlasmaColors';
|
|
3
3
|
export * from '@mantine/carousel';
|
|
4
4
|
export * from '@mantine/core';
|
|
5
|
-
export * from '@mantine/form';
|
|
6
5
|
export type { FormValidateInput } from '@mantine/form/lib/types';
|
|
7
6
|
export * from '@mantine/hooks';
|
|
8
7
|
export { createColumnHelper, type ColumnDef } from '@tanstack/table-core';
|
|
9
8
|
export * from './components';
|
|
9
|
+
export * from '@mantine/form';
|
|
10
10
|
export { Header, Table, type HeaderProps, Modal } from './components';
|
|
11
|
+
export { useForm, createFormContext } from './form';
|
|
11
12
|
export * from './theme';
|
|
12
13
|
declare module '@mantine/core' {
|
|
13
14
|
interface MantineThemeColorsOverride {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,EAAC,MAAM,eAAe,CAAC;AAEpC,OAAO,EAAC,YAAY,EAAC,MAAM,sBAAsB,CAAC;AAElD,cAAc,mBAAmB,CAAC;AAClC,cAAc,eAAe,CAAC;AAC9B,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,EAAC,MAAM,eAAe,CAAC;AAEpC,OAAO,EAAC,YAAY,EAAC,MAAM,sBAAsB,CAAC;AAElD,cAAc,mBAAmB,CAAC;AAClC,cAAc,eAAe,CAAC;AAC9B,YAAY,EAAC,iBAAiB,EAAC,MAAM,yBAAyB,CAAC;AAC/D,cAAc,gBAAgB,CAAC;AAC/B,OAAO,EAAC,kBAAkB,EAAE,KAAK,SAAS,EAAC,MAAM,sBAAsB,CAAC;AACxE,cAAc,cAAc,CAAC;AAC7B,cAAc,eAAe,CAAC;AAE9B,OAAO,EAAC,MAAM,EAAE,KAAK,EAAE,KAAK,WAAW,EAAE,KAAK,EAAC,MAAM,cAAc,CAAC;AACpE,OAAO,EAAC,OAAO,EAAE,iBAAiB,EAAC,MAAM,QAAQ,CAAC;AAElD,cAAc,SAAS,CAAC;AAExB,OAAO,QAAQ,eAAe,CAAC;IAC3B,UAAiB,0BAA0B;QAEvC,MAAM,EAAE,MAAM,CAAC,MAAM,OAAO,YAAY,GAAG,CAAC,MAAM,GAAG,EAAE,CAAC,EAAE,KAAK,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,CAAC;KAChF;CACJ"}
|
|
@@ -1,15 +1,13 @@
|
|
|
1
1
|
import _object_spread from "@swc/helpers/src/_object_spread.mjs";
|
|
2
2
|
import _object_spread_props from "@swc/helpers/src/_object_spread_props.mjs";
|
|
3
3
|
import _object_without_properties from "@swc/helpers/src/_object_without_properties.mjs";
|
|
4
|
-
import _sliced_to_array from "@swc/helpers/src/_sliced_to_array.mjs";
|
|
5
4
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
6
5
|
import { AddSize16Px } from "@coveord/plasma-react-icons";
|
|
7
6
|
import { Box, Button, Group, Input, Stack, Tooltip, useComponentDefaultProps } from "@mantine/core";
|
|
8
|
-
import { useId } from "@mantine/hooks";
|
|
7
|
+
import { useDidUpdate, useId } from "@mantine/hooks";
|
|
9
8
|
import { DragDropContext, Droppable } from "react-beautiful-dnd";
|
|
10
|
-
import { useControlledList } from "../../hooks";
|
|
11
|
-
import { CollectionItem } from "./CollectionItem";
|
|
12
9
|
import useStyles from "./Collection.styles";
|
|
10
|
+
import { CollectionItem } from "./CollectionItem";
|
|
13
11
|
var defaultProps = {
|
|
14
12
|
draggable: false,
|
|
15
13
|
addLabel: "Add item",
|
|
@@ -19,12 +17,13 @@ var defaultProps = {
|
|
|
19
17
|
required: false
|
|
20
18
|
};
|
|
21
19
|
export var Collection = function(props) {
|
|
22
|
-
var _useComponentDefaultProps = useComponentDefaultProps("Collection", defaultProps, props), value = _useComponentDefaultProps.value,
|
|
20
|
+
var _useComponentDefaultProps = useComponentDefaultProps("Collection", defaultProps, props), value = _useComponentDefaultProps.value, onChange = _useComponentDefaultProps.onChange, onRemoveItem = _useComponentDefaultProps.onRemoveItem, onReorderItem = _useComponentDefaultProps.onReorderItem, onInsertItem = _useComponentDefaultProps.onInsertItem, disabled = _useComponentDefaultProps.disabled, draggable = _useComponentDefaultProps.draggable, children = _useComponentDefaultProps.children, spacing = _useComponentDefaultProps.spacing, required = _useComponentDefaultProps.required, newItem = _useComponentDefaultProps.newItem, addLabel = _useComponentDefaultProps.addLabel, addDisabledTooltip = _useComponentDefaultProps.addDisabledTooltip, allowAdd = _useComponentDefaultProps.allowAdd, label = _useComponentDefaultProps.label, labelProps = _useComponentDefaultProps.labelProps, description = _useComponentDefaultProps.description, descriptionProps = _useComponentDefaultProps.descriptionProps, error = _useComponentDefaultProps.error, errorProps = _useComponentDefaultProps.errorProps, // Style props
|
|
23
21
|
classNames = _useComponentDefaultProps.classNames, className = _useComponentDefaultProps.className, styles = _useComponentDefaultProps.styles, unstyled = _useComponentDefaultProps.unstyled, others = _object_without_properties(_useComponentDefaultProps, [
|
|
24
22
|
"value",
|
|
25
|
-
"defaultValue",
|
|
26
23
|
"onChange",
|
|
27
24
|
"onRemoveItem",
|
|
25
|
+
"onReorderItem",
|
|
26
|
+
"onInsertItem",
|
|
28
27
|
"disabled",
|
|
29
28
|
"draggable",
|
|
30
29
|
"children",
|
|
@@ -52,18 +51,14 @@ export var Collection = function(props) {
|
|
|
52
51
|
unstyled: unstyled
|
|
53
52
|
}), classes = _useStyles.classes, cx = _useStyles.cx;
|
|
54
53
|
var collectionID = useId("dnd-droppable");
|
|
55
|
-
var
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
remove(index);
|
|
64
|
-
onRemoveItem === null || onRemoveItem === void 0 ? void 0 : onRemoveItem(index);
|
|
65
|
-
};
|
|
66
|
-
};
|
|
54
|
+
var hasOnlyOneItem = value.length === 1;
|
|
55
|
+
/**
|
|
56
|
+
* Enforcing onChange when the value is modified will make sure the errors are carried through.
|
|
57
|
+
*/ useDidUpdate(function() {
|
|
58
|
+
onChange === null || onChange === void 0 ? void 0 : onChange(value);
|
|
59
|
+
}, [
|
|
60
|
+
JSON.stringify(value)
|
|
61
|
+
]);
|
|
67
62
|
var _label = label ? /*#__PURE__*/ _jsx(Input.Label, _object_spread_props(_object_spread({
|
|
68
63
|
required: required
|
|
69
64
|
}, labelProps), {
|
|
@@ -82,19 +77,22 @@ export var Collection = function(props) {
|
|
|
82
77
|
_description
|
|
83
78
|
]
|
|
84
79
|
}) : null;
|
|
85
|
-
var items =
|
|
86
|
-
|
|
80
|
+
var items = value.map(function(item, index) {
|
|
81
|
+
/*#__PURE__*/ return _jsx(CollectionItem, {
|
|
87
82
|
disabled: disabled,
|
|
88
83
|
draggable: draggable,
|
|
89
84
|
index: index,
|
|
90
|
-
onRemove:
|
|
85
|
+
onRemove: function() {
|
|
86
|
+
return onRemoveItem === null || onRemoveItem === void 0 ? void 0 : onRemoveItem(index);
|
|
87
|
+
},
|
|
91
88
|
styles: styles,
|
|
92
89
|
removable: !(required && hasOnlyOneItem),
|
|
93
90
|
children: children(item, index)
|
|
94
91
|
}, index);
|
|
95
92
|
});
|
|
96
93
|
var _allowAdd;
|
|
97
|
-
var addAllowed = (_allowAdd = allowAdd === null || allowAdd === void 0 ? void 0 : allowAdd(
|
|
94
|
+
var addAllowed = (_allowAdd = allowAdd === null || allowAdd === void 0 ? void 0 : allowAdd(value)) !== null && _allowAdd !== void 0 ? _allowAdd : true;
|
|
95
|
+
var _value_length;
|
|
98
96
|
var _addButton = disabled ? null : /*#__PURE__*/ _jsx(Group, {
|
|
99
97
|
children: /*#__PURE__*/ _jsx(Tooltip, {
|
|
100
98
|
label: addDisabledTooltip,
|
|
@@ -106,7 +104,7 @@ export var Collection = function(props) {
|
|
|
106
104
|
height: 16
|
|
107
105
|
}),
|
|
108
106
|
onClick: function() {
|
|
109
|
-
return
|
|
107
|
+
return onInsertItem(newItem, (_value_length = value === null || value === void 0 ? void 0 : value.length) !== null && _value_length !== void 0 ? _value_length : 0);
|
|
110
108
|
},
|
|
111
109
|
disabled: !addAllowed,
|
|
112
110
|
children: addLabel
|
|
@@ -117,7 +115,7 @@ export var Collection = function(props) {
|
|
|
117
115
|
return /*#__PURE__*/ _jsx(DragDropContext, {
|
|
118
116
|
onDragEnd: function(param) {
|
|
119
117
|
var destination = param.destination, source = param.source;
|
|
120
|
-
return
|
|
118
|
+
return onReorderItem({
|
|
121
119
|
from: source.index,
|
|
122
120
|
to: (destination === null || destination === void 0 ? void 0 : destination.index) || 0
|
|
123
121
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/components/collection/Collection.tsx"],"sourcesContent":["import {AddSize16Px} from '@coveord/plasma-react-icons';\nimport {\n Box,\n Button,\n DefaultProps,\n Group,\n Input,\n InputWrapperBaseProps,\n MantineNumberSize,\n Selectors,\n Stack,\n Tooltip,\n useComponentDefaultProps,\n} from '@mantine/core';\nimport {useId} from '@mantine/hooks';\nimport {ReactNode} from 'react';\nimport {DragDropContext, Droppable} from 'react-beautiful-dnd';\nimport {useControlledList} from '../../hooks';\nimport {CollectionItem} from './CollectionItem';\nimport useStyles from './Collection.styles';\n\ninterface CollectionProps<T>\n extends Omit<InputWrapperBaseProps, 'inputContainer' | 'inputWrapperOrder'>,\n DefaultProps<Selectors<typeof useStyles>> {\n /**\n * The default value each new item should have\n */\n newItem: T;\n /**\n * A render function called for each item passed in the `value` prop.\n *\n * @param item The current item's value\n * @param index The current item's index\n */\n children: (item: T, index: number) => ReactNode;\n /**\n * The list of items to display inside the collection\n *\n * @default []\n */\n value?: T[];\n /**\n * The initial items of the collection (for uncontrolled usage only)\n */\n defaultValue?: T[];\n /**\n * Unused, has no effect\n */\n onFocus?: () => void;\n /**\n * Function called whenever the value needs to be updated\n *\n * @param value The whole list of items after the change\n */\n onChange?: (value: T[]) => void;\n /**\n * Function called after an item is removed from the collection using the remove button\n *\n * @param itemIndex The index of the item that was removed\n */\n onRemoveItem?: (itemIndex: number) => void;\n /**\n * Whether the collection should have drag and drop behavior enabled\n *\n * @default false\n */\n draggable?: boolean;\n /**\n * Whether the collection is disabled, or in other words in read only mode\n *\n * @default false\n */\n disabled?: boolean;\n /**\n * Function that determines if the add item button should be enabled given the current items of the collection.\n * The button is always enabled if this props remains undefined\n *\n * @param values The current items of the collection\n */\n allowAdd?: (values: T[]) => boolean;\n /**\n * The label of the add item button\n *\n * @default \"Add item\"\n */\n addLabel?: string;\n /**\n * The tooltip text displayed when hovering over the disabled add item button\n *\n * @default 'There is already an empty item'\n */\n addDisabledTooltip?: string;\n /**\n * The spacing between the colleciton items\n *\n * @default 'xs'\n */\n spacing?: MantineNumberSize;\n /**\n * Whether the collection is required. When required is true, the collection will hide the remove button if there is only one item\n *\n * @default false\n */\n required?: boolean;\n}\n\nconst defaultProps: Partial<CollectionProps<unknown>> = {\n draggable: false,\n addLabel: 'Add item',\n addDisabledTooltip: 'There is already an empty item',\n disabled: false,\n spacing: 'xs',\n required: false,\n};\n\nexport const Collection = <T,>(props: CollectionProps<T>) => {\n const {\n value,\n defaultValue,\n onChange,\n onRemoveItem,\n disabled,\n draggable,\n children,\n spacing,\n required,\n newItem,\n addLabel,\n addDisabledTooltip,\n allowAdd,\n label,\n labelProps,\n description,\n descriptionProps,\n error,\n errorProps,\n\n // Style props\n classNames,\n className,\n styles,\n unstyled,\n\n ...others\n } = useComponentDefaultProps('Collection', defaultProps as CollectionProps<T>, props);\n const {classes, cx} = useStyles(null, {classNames, name: 'Collection', styles, unstyled});\n const collectionID = useId('dnd-droppable');\n\n const [values, {append, remove, reorder}] = useControlledList({value, onChange, defaultValue});\n const hasOnlyOneItem = values.length === 1;\n const removeItem = (index: number) => () => {\n remove(index);\n onRemoveItem?.(index);\n };\n\n const _label = label ? (\n <Input.Label required={required} {...labelProps}>\n {label}\n </Input.Label>\n ) : null;\n\n const _description = description ? (\n <Input.Description {...descriptionProps}>{description}</Input.Description>\n ) : null;\n const _error = error ? <Input.Error {...errorProps}>{error}</Input.Error> : null;\n const _header =\n _label || _description ? (\n <Box mb=\"sm\">\n {_label}\n {_description}\n </Box>\n ) : null;\n\n const items = values.map((item, index) => (\n <CollectionItem\n key={index}\n disabled={disabled}\n draggable={draggable}\n index={index}\n onRemove={removeItem(index)}\n styles={styles}\n removable={!(required && hasOnlyOneItem)}\n >\n {children(item, index)}\n </CollectionItem>\n ));\n\n const addAllowed = allowAdd?.(values) ?? true;\n\n const _addButton = disabled ? null : (\n <Group>\n <Tooltip label={addDisabledTooltip} disabled={addAllowed}>\n <Box>\n <Button\n variant=\"subtle\"\n leftIcon={<AddSize16Px height={16} />}\n onClick={() => append(newItem)}\n disabled={!addAllowed}\n >\n {addLabel}\n </Button>\n </Box>\n </Tooltip>\n </Group>\n );\n\n return (\n <DragDropContext\n onDragEnd={({destination, source}) => reorder({from: source.index, to: destination?.index || 0})}\n >\n <Droppable direction=\"vertical\" droppableId={collectionID}>\n {(provided) => (\n <Box\n {...provided.droppableProps}\n ref={provided.innerRef}\n className={cx(classes.root, className)}\n {...others}\n >\n {_header}\n <Stack spacing={spacing}>\n {items}\n {provided.placeholder}\n {_addButton}\n {_error}\n </Stack>\n </Box>\n )}\n </Droppable>\n </DragDropContext>\n );\n};\n"],"names":["AddSize16Px","Box","Button","Group","Input","Stack","Tooltip","useComponentDefaultProps","useId","DragDropContext","Droppable","useControlledList","CollectionItem","useStyles","defaultProps","draggable","addLabel","addDisabledTooltip","disabled","spacing","required","Collection","props","value","defaultValue","onChange","onRemoveItem","children","newItem","allowAdd","label","labelProps","description","descriptionProps","error","errorProps","classNames","className","styles","unstyled","others","name","classes","cx","collectionID","values","append","remove","reorder","hasOnlyOneItem","length","removeItem","index","_label","Label","_description","Description","_error","Error","_header","mb","items","map","item","onRemove","removable","addAllowed","_addButton","variant","leftIcon","height","onClick","onDragEnd","destination","source","from","to","direction","droppableId","provided","droppableProps","ref","innerRef","root","placeholder"],"mappings":"AAAA;;;;;AAAA,SAAQA,WAAW,QAAO,8BAA8B;AACxD,SACIC,GAAG,EACHC,MAAM,EAENC,KAAK,EACLC,KAAK,EAILC,KAAK,EACLC,OAAO,EACPC,wBAAwB,QACrB,gBAAgB;AACvB,SAAQC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AU,KAACrE;wBAAYsE,QAAQ;;oBAC/BC,SAAS;+BAAMzB,OAAOlB;;oBACtBV,UAAU,CAACgD;8BAEVlD;;;;MAKpB;IAED,qBACI,KAACP;QACG+D,WAAW;gBAAEC,oBAAAA,aAAaC,eAAAA;YAAY1B,OAAAA,QAAQ;gBAAC2B,MAAMD,OAAOtB,KAAK;gBAAEwB,IAAIH,CAAAA,wBAAAA,yBAAAA,KAAAA,IAAAA,YAAarB,KAAK,AAAD,KAAK;YAAC;;kBAE9F,cAAA,KAAC1C;YAAUmE,WAAU;YAAWC,aAAalC;sBACxC,SAACmC;qCACE,MAAC9E,iFACO8E,SAASC,cAAc;oBAC3BC,KAAKF,SAASG,QAAQ;oBACtB7C,WAAWM,GAAGD,QAAQyC,IAAI,EAAE9C;oBACxBG;;wBAEHmB;sCACD,MAACtD;4BAAMc,SAASA;;gCACX0C;gCACAkB,SAASK,WAAW;gCACpBjB;gCACAV;;;;;;;;AAO7B,EAAE"}
|
|
1
|
+
{"version":3,"sources":["../../../../src/components/collection/Collection.tsx"],"sourcesContent":["import {AddSize16Px} from '@coveord/plasma-react-icons';\nimport {\n Box,\n Button,\n DefaultProps,\n Group,\n Input,\n InputWrapperBaseProps,\n MantineNumberSize,\n Selectors,\n Stack,\n Tooltip,\n useComponentDefaultProps,\n} from '@mantine/core';\nimport {ReorderPayload} from '@mantine/form/lib/types';\nimport {useDidUpdate, useId} from '@mantine/hooks';\nimport {ReactNode} from 'react';\nimport {DragDropContext, Droppable} from 'react-beautiful-dnd';\n\nimport useStyles from './Collection.styles';\nimport {CollectionItem} from './CollectionItem';\n\ninterface CollectionProps<T>\n extends Omit<InputWrapperBaseProps, 'inputContainer' | 'inputWrapperOrder'>,\n DefaultProps<Selectors<typeof useStyles>> {\n /**\n * The default value each new item should have\n */\n newItem: T;\n /**\n * A render function called for each item passed in the `value` prop.\n *\n * @param item The current item's value\n * @param index The current item's index\n */\n children: (item: T, index: number) => ReactNode;\n /**\n * The list of items to display inside the collection\n *\n * @default []\n */\n value?: T[];\n /**\n * Unused, has no effect\n */\n onFocus?: () => void;\n /**\n * Function called whenever the value needs to be updated\n *\n * @param value The whole list of items after the change\n */\n onChange?: (value: T[]) => void;\n /**\n * Function called after an item is removed from the collection using the remove button\n *\n * @param itemIndex The index of the item that was removed\n */\n onRemoveItem?: (itemIndex: number) => void;\n /**\n * Function that gets called whenever a collection item needs to be reordered\n *\n * @param payload The origin and destination index of the item to reorder\n */\n onReorderItem?: (payload: ReorderPayload) => void;\n /**\n * Function that gets called when a new item needs to be added to the collection\n *\n * @param value The the value of the item to insert\n * @param index The index of the new item to insert\n */\n onInsertItem?: (value: T, index: number) => void;\n /**\n * Whether the collection should have drag and drop behavior enabled\n *\n * @default false\n */\n draggable?: boolean;\n /**\n * Whether the collection is disabled, or in other words in read only mode\n *\n * @default false\n */\n disabled?: boolean;\n /**\n * Function that determines if the add item button should be enabled given the current items of the collection.\n * The button is always enabled if this props remains undefined\n *\n * @param values The current items of the collection\n */\n allowAdd?: (values: T[]) => boolean;\n /**\n * The label of the add item button\n *\n * @default \"Add item\"\n */\n addLabel?: string;\n /**\n * The tooltip text displayed when hovering over the disabled add item button\n *\n * @default 'There is already an empty item'\n */\n addDisabledTooltip?: string;\n /**\n * The spacing between the colleciton items\n *\n * @default 'xs'\n */\n spacing?: MantineNumberSize;\n /**\n * Whether the collection is required. When required is true, the collection will hide the remove button if there is only one item\n *\n * @default false\n */\n required?: boolean;\n}\n\nconst defaultProps: Partial<CollectionProps<unknown>> = {\n draggable: false,\n addLabel: 'Add item',\n addDisabledTooltip: 'There is already an empty item',\n disabled: false,\n spacing: 'xs',\n required: false,\n};\n\nexport const Collection = <T,>(props: CollectionProps<T>) => {\n const {\n value,\n onChange,\n onRemoveItem,\n onReorderItem,\n onInsertItem,\n disabled,\n draggable,\n children,\n spacing,\n required,\n newItem,\n addLabel,\n addDisabledTooltip,\n allowAdd,\n label,\n labelProps,\n description,\n descriptionProps,\n error,\n errorProps,\n\n // Style props\n classNames,\n className,\n styles,\n unstyled,\n\n ...others\n } = useComponentDefaultProps('Collection', defaultProps as CollectionProps<T>, props);\n const {classes, cx} = useStyles(null, {classNames, name: 'Collection', styles, unstyled});\n const collectionID = useId('dnd-droppable');\n\n const hasOnlyOneItem = value.length === 1;\n\n /**\n * Enforcing onChange when the value is modified will make sure the errors are carried through.\n */\n useDidUpdate(() => {\n onChange?.(value);\n }, [JSON.stringify(value)]);\n\n const _label = label ? (\n <Input.Label required={required} {...labelProps}>\n {label}\n </Input.Label>\n ) : null;\n\n const _description = description ? (\n <Input.Description {...descriptionProps}>{description}</Input.Description>\n ) : null;\n const _error = error ? <Input.Error {...errorProps}>{error}</Input.Error> : null;\n const _header =\n _label || _description ? (\n <Box mb=\"sm\">\n {_label}\n {_description}\n </Box>\n ) : null;\n\n const items = value.map((item, index) => (\n <CollectionItem\n key={index}\n disabled={disabled}\n draggable={draggable}\n index={index}\n onRemove={() => onRemoveItem?.(index)}\n styles={styles}\n removable={!(required && hasOnlyOneItem)}\n >\n {children(item, index)}\n </CollectionItem>\n ));\n\n const addAllowed = allowAdd?.(value) ?? true;\n\n const _addButton = disabled ? null : (\n <Group>\n <Tooltip label={addDisabledTooltip} disabled={addAllowed}>\n <Box>\n <Button\n variant=\"subtle\"\n leftIcon={<AddSize16Px height={16} />}\n onClick={() => onInsertItem(newItem, value?.length ?? 0)}\n disabled={!addAllowed}\n >\n {addLabel}\n </Button>\n </Box>\n </Tooltip>\n </Group>\n );\n\n return (\n <DragDropContext\n onDragEnd={({destination, source}) => onReorderItem({from: source.index, to: destination?.index || 0})}\n >\n <Droppable direction=\"vertical\" droppableId={collectionID}>\n {(provided) => (\n <Box\n {...provided.droppableProps}\n ref={provided.innerRef}\n className={cx(classes.root, className)}\n {...others}\n >\n {_header}\n <Stack spacing={spacing}>\n {items}\n {provided.placeholder}\n {_addButton}\n {_error}\n </Stack>\n </Box>\n )}\n </Droppable>\n </DragDropContext>\n );\n};\n"],"names":["AddSize16Px","Box","Button","Group","Input","Stack","Tooltip","useComponentDefaultProps","useDidUpdate","useId","DragDropContext","Droppable","useStyles","CollectionItem","defaultProps","draggable","addLabel","addDisabledTooltip","disabled","spacing","required","Collection","props","value","onChange","onRemoveItem","onReorderItem","onInsertItem","children","newItem","allowAdd","label","labelProps","description","descriptionProps","error","errorProps","classNames","className","styles","unstyled","others","name","classes","cx","collectionID","hasOnlyOneItem","length","JSON","stringify","_label","Label","_description","Description","_error","Error","_header","mb","items","map","item","index","onRemove","removable","addAllowed","_addButton","variant","leftIcon","height","onClick","onDragEnd","destination","source","from","to","direction","droppableId","provided","droppableProps","ref","innerRef","root","placeholder"],"mappings":"AAAA;;;;AAAA,SAAQA,WAAW,QAAO,8BAA8B;AACxD,SACIC,GAAG,EACHC,MAAM,EAENC,KAAK,EACLC,KAAK,EAILC,KAAK,EACLC,OAAO,EACPC,wBAAwB,QACrB,gBAAgB;AAEvB,SAAQC,YAAY,EAAEC,KAAK,QAAO,iBAAiB;AAEnD,SAAQC,eAAe,EAAEC,SAAS,QAAO,sBAAsB;AAE/D,OAAOC,eAAe,sBAAsB;AAC5C,SAAQC,cAAc,QAAO,mBAAmB;AAgGhD,IAAMC,eAAkD;IACpDC,WAAW,KAAK;IAChBC,UAAU;IACVC,oBAAoB;IACpBC,UAAU,KAAK;IACfC,SAAS;IACTC,UAAU,KAAK;AACnB;AAEA,OAAO,IAAMC,aAAa,SAAKC,OAA8B;IACzD,IA6BIf,4BAAAA,yBAAyB,cAAcO,cAAoCQ,QA5B3EC,QA4BAhB,0BA5BAgB,OACAC,WA2BAjB,0BA3BAiB,UACAC,eA0BAlB,0BA1BAkB,cACAC,gBAyBAnB,0BAzBAmB,eACAC,eAwBApB,0BAxBAoB,cACAT,WAuBAX,0BAvBAW,UACAH,YAsBAR,0BAtBAQ,WACAa,WAqBArB,0BArBAqB,UACAT,UAoBAZ,0BApBAY,SACAC,WAmBAb,0BAnBAa,UACAS,UAkBAtB,0BAlBAsB,SACAb,WAiBAT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27 @@
|
|
|
1
|
+
/* eslint-disable prefer-arrow/prefer-arrow-functions */ import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import React, { createContext, useContext } from "react";
|
|
3
|
+
import { useForm } from "./useForm";
|
|
4
|
+
export function createFormContext() {
|
|
5
|
+
var FormProvider = function FormProvider(param) {
|
|
6
|
+
var form = param.form, children = param.children;
|
|
7
|
+
return /*#__PURE__*/ _jsx(FormContext.Provider, {
|
|
8
|
+
value: form,
|
|
9
|
+
children: children
|
|
10
|
+
});
|
|
11
|
+
};
|
|
12
|
+
var useFormContext = function useFormContext() {
|
|
13
|
+
var ctx = useContext(FormContext);
|
|
14
|
+
if (!ctx) {
|
|
15
|
+
throw new Error("useFormContext was called outside of FormProvider context");
|
|
16
|
+
}
|
|
17
|
+
return ctx;
|
|
18
|
+
};
|
|
19
|
+
var FormContext = /*#__PURE__*/ createContext(null);
|
|
20
|
+
return [
|
|
21
|
+
FormProvider,
|
|
22
|
+
useFormContext,
|
|
23
|
+
useForm
|
|
24
|
+
];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
//# sourceMappingURL=FormProvider.js.map
|