@rulab/adminjs-components 0.2.0 → 0.2.1
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/README.md +7 -1
- package/dist/components/ColorStatus/ColorStatusEdit.js +7 -3
- package/dist/components/ColorStatus/ColorStatusFeature.d.ts +1 -0
- package/dist/components/ColorStatus/ColorStatusFeature.js +8 -2
- package/dist/components/StringList/StringList.js +5 -4
- package/dist/components/StringList/StringListFeature.d.ts +1 -0
- package/dist/components/StringList/StringListFeature.js +2 -1
- package/dist/components/StringList/StringListShow.js +6 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -49,6 +49,8 @@ You can also pass `componentLoader` into every feature instead of calling
|
|
|
49
49
|
### ColorStatus
|
|
50
50
|
|
|
51
51
|
Renders a colored badge in edit/list/show based on a list of available values.
|
|
52
|
+
By default (`nullable: false`) the first option is auto-selected in new records.
|
|
53
|
+
Set `nullable: true` to allow an empty value.
|
|
52
54
|
|
|
53
55
|
```ts
|
|
54
56
|
import { ColorStatusFeature } from "@rulab/adminjs-components";
|
|
@@ -56,6 +58,7 @@ import { ColorStatusFeature } from "@rulab/adminjs-components";
|
|
|
56
58
|
features: [
|
|
57
59
|
ColorStatusFeature({
|
|
58
60
|
key: "status",
|
|
61
|
+
nullable: false, // optional, default false
|
|
59
62
|
availableValues: [
|
|
60
63
|
{ value: "draft", label: "Draft", color: "#64748b" },
|
|
61
64
|
{ value: "review", label: "In review", color: "#f59e0b" },
|
|
@@ -123,6 +126,7 @@ features: [
|
|
|
123
126
|
### StringList
|
|
124
127
|
|
|
125
128
|
Sortable list stored as a single string (comma-separated by default).
|
|
129
|
+
Use `separator` to override the delimiter used for storing and rendering values.
|
|
126
130
|
|
|
127
131
|
```ts
|
|
128
132
|
import { StringListFeature } from "@rulab/adminjs-components";
|
|
@@ -130,6 +134,7 @@ import { StringListFeature } from "@rulab/adminjs-components";
|
|
|
130
134
|
features: [
|
|
131
135
|
StringListFeature({
|
|
132
136
|
key: "facts",
|
|
137
|
+
separator: "|", // optional, default "|"
|
|
133
138
|
}),
|
|
134
139
|
]
|
|
135
140
|
```
|
|
@@ -138,13 +143,14 @@ features: [
|
|
|
138
143
|
|
|
139
144
|
Groups edit/show fields into tabs based on property `props.tab` or
|
|
140
145
|
`custom.tab`. Fields without a tab go to a common group.
|
|
146
|
+
`commonTabLabel` is optional and defaults to `"Common"`.
|
|
141
147
|
|
|
142
148
|
```ts
|
|
143
149
|
import { TabsFeature } from "@rulab/adminjs-components";
|
|
144
150
|
|
|
145
151
|
features: [
|
|
146
152
|
TabsFeature({
|
|
147
|
-
commonTabLabel: "Common"
|
|
153
|
+
commonTabLabel: "General", // optional, default "Common"
|
|
148
154
|
}),
|
|
149
155
|
]
|
|
150
156
|
|
|
@@ -43,8 +43,12 @@ const colorStyles = {
|
|
|
43
43
|
};
|
|
44
44
|
export const ColorStatusEdit = ({ property, record, onChange, }) => {
|
|
45
45
|
const availableValues = property.availableValues;
|
|
46
|
-
const
|
|
47
|
-
const
|
|
46
|
+
const nullable = Boolean(property.custom?.nullable);
|
|
47
|
+
const isNewRecord = !record.id;
|
|
48
|
+
const currentValue = record.params[property.path];
|
|
49
|
+
const currentOption = availableValues.find((item) => item.value === currentValue);
|
|
50
|
+
const initialOption = currentOption ?? (!nullable && isNewRecord ? availableValues[0] : null);
|
|
51
|
+
const [selectOption, setCurrentOption] = useState(initialOption);
|
|
48
52
|
const handleSelectChange = (option) => {
|
|
49
53
|
setCurrentOption(option);
|
|
50
54
|
};
|
|
@@ -53,6 +57,6 @@ export const ColorStatusEdit = ({ property, record, onChange, }) => {
|
|
|
53
57
|
}, [selectOption]);
|
|
54
58
|
return (React.createElement(ColorStatusWrapper, null,
|
|
55
59
|
React.createElement(Label, null, property.label ?? property.path),
|
|
56
|
-
React.createElement(Select, { className: "basic-single", classNamePrefix: "select",
|
|
60
|
+
React.createElement(Select, { className: "basic-single", classNamePrefix: "select", value: selectOption, onChange: handleSelectChange, isClearable: nullable, name: "color", options: availableValues, styles: colorStyles })));
|
|
57
61
|
};
|
|
58
62
|
export default ColorStatusEdit;
|
|
@@ -4,6 +4,7 @@ type ColorStatusOptions = {
|
|
|
4
4
|
componentLoader?: ComponentLoader;
|
|
5
5
|
key: string;
|
|
6
6
|
availableValues?: AvailableValueType[];
|
|
7
|
+
nullable?: boolean;
|
|
7
8
|
};
|
|
8
9
|
export declare const ColorStatusFeature: (config: ColorStatusOptions) => FeatureType;
|
|
9
10
|
export default ColorStatusFeature;
|
|
@@ -2,7 +2,10 @@ import { buildFeature } from "adminjs";
|
|
|
2
2
|
import { bundleComponent } from "../../utils/bundle-component.js";
|
|
3
3
|
const COMPONENT_NAME = "ColorStatus";
|
|
4
4
|
export const ColorStatusFeature = (config) => {
|
|
5
|
-
const { componentLoader, key, availableValues = [] } = config;
|
|
5
|
+
const { componentLoader, key, availableValues = [], nullable = false } = config;
|
|
6
|
+
const values = nullable
|
|
7
|
+
? [{ value: null, label: "", color: "#ffffff" }, ...availableValues]
|
|
8
|
+
: availableValues;
|
|
6
9
|
const editComponent = bundleComponent(componentLoader, COMPONENT_NAME, "ColorStatusEdit.js");
|
|
7
10
|
const listComponent = bundleComponent(componentLoader, COMPONENT_NAME, "ColorStatusList.js");
|
|
8
11
|
const showComponent = bundleComponent(componentLoader, COMPONENT_NAME, "ColorStatusShow.js");
|
|
@@ -10,7 +13,10 @@ export const ColorStatusFeature = (config) => {
|
|
|
10
13
|
properties: {
|
|
11
14
|
[key]: {
|
|
12
15
|
isVisible: { filter: true, show: true, edit: true, list: true },
|
|
13
|
-
availableValues,
|
|
16
|
+
availableValues: values,
|
|
17
|
+
custom: {
|
|
18
|
+
nullable,
|
|
19
|
+
},
|
|
14
20
|
components: {
|
|
15
21
|
edit: editComponent,
|
|
16
22
|
list: listComponent,
|
|
@@ -3,8 +3,9 @@ import React, { useEffect, useState, } from "react";
|
|
|
3
3
|
import { ThemeProvider } from "styled-components";
|
|
4
4
|
import { StyledCustomInput, StyledInputWrapper, StyledLabel, StyledListWrapper, StyledWrapper, } from "./styles.js";
|
|
5
5
|
import { SortableList } from "./SortableList/SortableList.js";
|
|
6
|
-
|
|
7
|
-
export const StringList = ({ record, onChange, property, stringListSeparator =
|
|
6
|
+
const DEFAULT_SEPARATOR = "|";
|
|
7
|
+
export const StringList = ({ record, onChange, property, stringListSeparator = DEFAULT_SEPARATOR, }) => {
|
|
8
|
+
const separatorValue = property.props?.stringListSeparator ?? stringListSeparator;
|
|
8
9
|
const stringListValue = record.params?.[property.path] ?? property.props.value ?? "";
|
|
9
10
|
const initialList = stringListValue
|
|
10
11
|
? prepareDataForList(stringListValue)
|
|
@@ -45,10 +46,10 @@ export const StringList = ({ record, onChange, property, stringListSeparator = s
|
|
|
45
46
|
setList(newData);
|
|
46
47
|
}
|
|
47
48
|
function prepareDataForDatabase(list) {
|
|
48
|
-
return list.map(({ value }) => value).join(
|
|
49
|
+
return list.map(({ value }) => value).join(separatorValue);
|
|
49
50
|
}
|
|
50
51
|
function prepareDataForList(str) {
|
|
51
|
-
return str.split(
|
|
52
|
+
return str.split(separatorValue).map((item) => createListObject(item));
|
|
52
53
|
}
|
|
53
54
|
function createListObject(value) {
|
|
54
55
|
return {
|
|
@@ -2,6 +2,7 @@ import { ComponentLoader, FeatureType } from "adminjs";
|
|
|
2
2
|
type StringListOptions = {
|
|
3
3
|
componentLoader?: ComponentLoader;
|
|
4
4
|
key: string;
|
|
5
|
+
separator?: string;
|
|
5
6
|
};
|
|
6
7
|
export declare const StringListFeature: (config: StringListOptions) => FeatureType;
|
|
7
8
|
export default StringListFeature;
|
|
@@ -2,13 +2,14 @@ import { buildFeature } from "adminjs";
|
|
|
2
2
|
import { bundleComponent } from "../../utils/bundle-component.js";
|
|
3
3
|
const COMPONENT_NAME = "StringList";
|
|
4
4
|
export const StringListFeature = (config) => {
|
|
5
|
-
const { componentLoader, key } = config;
|
|
5
|
+
const { componentLoader, key, separator } = config;
|
|
6
6
|
const editComponent = bundleComponent(componentLoader, COMPONENT_NAME, "StringList.js");
|
|
7
7
|
const showComponent = bundleComponent(componentLoader, COMPONENT_NAME, "StringListShow.js");
|
|
8
8
|
return buildFeature({
|
|
9
9
|
properties: {
|
|
10
10
|
[key]: {
|
|
11
11
|
isVisible: { filter: true, show: true, edit: true, list: true },
|
|
12
|
+
props: separator ? { stringListSeparator: separator } : undefined,
|
|
12
13
|
components: {
|
|
13
14
|
edit: editComponent,
|
|
14
15
|
show: showComponent,
|
|
@@ -2,13 +2,15 @@ import React from "react";
|
|
|
2
2
|
import { ThemeProvider } from "styled-components";
|
|
3
3
|
import { theme } from "@adminjs/design-system";
|
|
4
4
|
import { StyledShowLabel, StyledShowWrapper, StyledListItem } from "./styles.js";
|
|
5
|
-
|
|
6
|
-
export const StringListShow = ({ property, record, stringListSeparator =
|
|
5
|
+
const DEFAULT_SEPARATOR = "|";
|
|
6
|
+
export const StringListShow = ({ property, record, stringListSeparator = DEFAULT_SEPARATOR, }) => {
|
|
7
|
+
const separatorValue = property.props?.stringListSeparator ?? stringListSeparator;
|
|
8
|
+
const value = record.params[property.path];
|
|
7
9
|
return (React.createElement(ThemeProvider, { theme: theme },
|
|
8
10
|
React.createElement(StyledShowWrapper, null,
|
|
9
11
|
React.createElement(StyledShowLabel, null, property.label ?? property.path),
|
|
10
|
-
|
|
11
|
-
.split(
|
|
12
|
+
value && (React.createElement("ul", null, value
|
|
13
|
+
.split(separatorValue)
|
|
12
14
|
.map((item, index) => (React.createElement(StyledListItem, { key: index }, `- ${item}`))))))));
|
|
13
15
|
};
|
|
14
16
|
export default StringListShow;
|