@nixxie-cms/fields-rating 2.1.0 → 2.1.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.
@@ -1,6 +1,11 @@
1
1
  import type { IntegerFieldConfig } from '@nixxie-cms/core/fields';
2
2
  import type { BaseCollectionTypeInfo, FieldTypeFunc } from '@nixxie-cms/core/types';
3
3
  export type RatingFieldConfig<CollectionTypeInfo extends BaseCollectionTypeInfo> = Omit<IntegerFieldConfig<CollectionTypeInfo>, 'validation' | 'defaultValue'> & {
4
+ /**
5
+ * How the rating is stored. `'integer'` (default) only allows whole numbers,
6
+ * `'float'` allows fractional values such as 4.8.
7
+ */
8
+ type?: 'integer' | 'float';
4
9
  /** Highest allowed rating. Default: 5 */
5
10
  max?: number;
6
11
  /** Lowest allowed rating. Default: 0 */
@@ -11,7 +16,9 @@ export type RatingFieldConfig<CollectionTypeInfo extends BaseCollectionTypeInfo>
11
16
  };
12
17
  };
13
18
  /**
14
- * A rating field (e.g. 0–5 stars). Built on the core `integer` field with a constrained range.
19
+ * A rating field (e.g. 0–5 stars). Built on the core `integer` field with a
20
+ * constrained range, or the `float` field when `type: 'float'` is set so it can
21
+ * store fractional ratings like 4.8.
15
22
  */
16
23
  export declare function rating<CollectionTypeInfo extends BaseCollectionTypeInfo>(config?: RatingFieldConfig<CollectionTypeInfo>): FieldTypeFunc<CollectionTypeInfo>;
17
24
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"../../../src","sources":["index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAA;AACjE,OAAO,KAAK,EAAE,sBAAsB,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAA;AAEnF,MAAM,MAAM,iBAAiB,CAAC,kBAAkB,SAAS,sBAAsB,IAAI,IAAI,CACrF,kBAAkB,CAAC,kBAAkB,CAAC,EACtC,YAAY,GAAG,cAAc,CAC9B,GAAG;IACF,yCAAyC;IACzC,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,wCAAwC;IACxC,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,YAAY,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IAC5B,UAAU,CAAC,EAAE;QAAE,UAAU,CAAC,EAAE,OAAO,CAAA;KAAE,CAAA;CACtC,CAAA;AAED;;GAEG;AACH,wBAAgB,MAAM,CAAC,kBAAkB,SAAS,sBAAsB,EACtE,MAAM,GAAE,iBAAiB,CAAC,kBAAkB,CAAM,GACjD,aAAa,CAAC,kBAAkB,CAAC,CASnC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"../../../src","sources":["index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAoB,kBAAkB,EAAE,MAAM,yBAAyB,CAAA;AACnF,OAAO,KAAK,EAAE,sBAAsB,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAA;AAEnF,MAAM,MAAM,iBAAiB,CAAC,kBAAkB,SAAS,sBAAsB,IAAI,IAAI,CACrF,kBAAkB,CAAC,kBAAkB,CAAC,EACtC,YAAY,GAAG,cAAc,CAC9B,GAAG;IACF;;;OAGG;IACH,IAAI,CAAC,EAAE,SAAS,GAAG,OAAO,CAAA;IAC1B,yCAAyC;IACzC,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,wCAAwC;IACxC,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,YAAY,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IAC5B,UAAU,CAAC,EAAE;QAAE,UAAU,CAAC,EAAE,OAAO,CAAA;KAAE,CAAA;CACtC,CAAA;AAED;;;;GAIG;AACH,wBAAgB,MAAM,CAAC,kBAAkB,SAAS,sBAAsB,EACtE,MAAM,GAAE,iBAAiB,CAAC,kBAAkB,CAAM,GACjD,aAAa,CAAC,kBAAkB,CAAC,CAoBnC"}
@@ -4,19 +4,21 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var fields = require('@nixxie-cms/core/fields');
6
6
 
7
- /**
8
- * A rating field (e.g. 0–5 stars). Built on the core `integer` field with a constrained range.
7
+ /**
8
+ * A rating field (e.g. 0–5 stars). Built on the core `integer` field with a
9
+ * constrained range, or the `float` field when `type: 'float'` is set so it can
10
+ * store fractional ratings like 4.8.
9
11
  */
10
12
  function rating(config = {}) {
11
13
  const {
14
+ type = 'integer',
12
15
  max = 5,
13
16
  min = 0,
14
17
  defaultValue,
15
18
  validation,
16
19
  ...rest
17
20
  } = config;
18
- return fields.integer({
19
- ...rest,
21
+ const shared = {
20
22
  defaultValue: defaultValue !== null && defaultValue !== void 0 ? defaultValue : null,
21
23
  validation: {
22
24
  isRequired: validation === null || validation === void 0 ? void 0 : validation.isRequired,
@@ -27,6 +29,16 @@ function rating(config = {}) {
27
29
  description: `A rating from ${min} to ${max}`,
28
30
  ...rest.ui
29
31
  }
32
+ };
33
+ if (type === 'float') {
34
+ return fields.float({
35
+ ...rest,
36
+ ...shared
37
+ });
38
+ }
39
+ return fields.integer({
40
+ ...rest,
41
+ ...shared
30
42
  });
31
43
  }
32
44
 
@@ -1,18 +1,20 @@
1
- import { integer } from '@nixxie-cms/core/fields';
1
+ import { float, integer } from '@nixxie-cms/core/fields';
2
2
 
3
- /**
4
- * A rating field (e.g. 0–5 stars). Built on the core `integer` field with a constrained range.
3
+ /**
4
+ * A rating field (e.g. 0–5 stars). Built on the core `integer` field with a
5
+ * constrained range, or the `float` field when `type: 'float'` is set so it can
6
+ * store fractional ratings like 4.8.
5
7
  */
6
8
  function rating(config = {}) {
7
9
  const {
10
+ type = 'integer',
8
11
  max = 5,
9
12
  min = 0,
10
13
  defaultValue,
11
14
  validation,
12
15
  ...rest
13
16
  } = config;
14
- return integer({
15
- ...rest,
17
+ const shared = {
16
18
  defaultValue: defaultValue !== null && defaultValue !== void 0 ? defaultValue : null,
17
19
  validation: {
18
20
  isRequired: validation === null || validation === void 0 ? void 0 : validation.isRequired,
@@ -23,6 +25,16 @@ function rating(config = {}) {
23
25
  description: `A rating from ${min} to ${max}`,
24
26
  ...rest.ui
25
27
  }
28
+ };
29
+ if (type === 'float') {
30
+ return float({
31
+ ...rest,
32
+ ...shared
33
+ });
34
+ }
35
+ return integer({
36
+ ...rest,
37
+ ...shared
26
38
  });
27
39
  }
28
40
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nixxie-cms/fields-rating",
3
- "version": "2.1.0",
3
+ "version": "2.1.1",
4
4
  "license": "MIT",
5
5
  "main": "dist/nixxie-cms-fields-rating.cjs.js",
6
6
  "module": "dist/nixxie-cms-fields-rating.esm.js",
package/src/index.ts CHANGED
@@ -1,31 +1,49 @@
1
- import { integer } from '@nixxie-cms/core/fields'
2
- import type { IntegerFieldConfig } from '@nixxie-cms/core/fields'
3
- import type { BaseCollectionTypeInfo, FieldTypeFunc } from '@nixxie-cms/core/types'
4
-
5
- export type RatingFieldConfig<CollectionTypeInfo extends BaseCollectionTypeInfo> = Omit<
6
- IntegerFieldConfig<CollectionTypeInfo>,
7
- 'validation' | 'defaultValue'
8
- > & {
9
- /** Highest allowed rating. Default: 5 */
10
- max?: number
11
- /** Lowest allowed rating. Default: 0 */
12
- min?: number
13
- defaultValue?: number | null
14
- validation?: { isRequired?: boolean }
15
- }
16
-
17
- /**
18
- * A rating field (e.g. 0–5 stars). Built on the core `integer` field with a constrained range.
19
- */
20
- export function rating<CollectionTypeInfo extends BaseCollectionTypeInfo>(
21
- config: RatingFieldConfig<CollectionTypeInfo> = {}
22
- ): FieldTypeFunc<CollectionTypeInfo> {
23
- const { max = 5, min = 0, defaultValue, validation, ...rest } = config
24
-
25
- return integer<CollectionTypeInfo>({
26
- ...(rest as IntegerFieldConfig<CollectionTypeInfo>),
27
- defaultValue: defaultValue ?? null,
28
- validation: { isRequired: validation?.isRequired, min, max },
29
- ui: { description: `A rating from ${min} to ${max}`, ...rest.ui },
30
- })
31
- }
1
+ import { float, integer } from '@nixxie-cms/core/fields'
2
+ import type { FloatFieldConfig, IntegerFieldConfig } from '@nixxie-cms/core/fields'
3
+ import type { BaseCollectionTypeInfo, FieldTypeFunc } from '@nixxie-cms/core/types'
4
+
5
+ export type RatingFieldConfig<CollectionTypeInfo extends BaseCollectionTypeInfo> = Omit<
6
+ IntegerFieldConfig<CollectionTypeInfo>,
7
+ 'validation' | 'defaultValue'
8
+ > & {
9
+ /**
10
+ * How the rating is stored. `'integer'` (default) only allows whole numbers,
11
+ * `'float'` allows fractional values such as 4.8.
12
+ */
13
+ type?: 'integer' | 'float'
14
+ /** Highest allowed rating. Default: 5 */
15
+ max?: number
16
+ /** Lowest allowed rating. Default: 0 */
17
+ min?: number
18
+ defaultValue?: number | null
19
+ validation?: { isRequired?: boolean }
20
+ }
21
+
22
+ /**
23
+ * A rating field (e.g. 0–5 stars). Built on the core `integer` field with a
24
+ * constrained range, or the `float` field when `type: 'float'` is set so it can
25
+ * store fractional ratings like 4.8.
26
+ */
27
+ export function rating<CollectionTypeInfo extends BaseCollectionTypeInfo>(
28
+ config: RatingFieldConfig<CollectionTypeInfo> = {}
29
+ ): FieldTypeFunc<CollectionTypeInfo> {
30
+ const { type = 'integer', max = 5, min = 0, defaultValue, validation, ...rest } = config
31
+
32
+ const shared = {
33
+ defaultValue: defaultValue ?? null,
34
+ validation: { isRequired: validation?.isRequired, min, max },
35
+ ui: { description: `A rating from ${min} to ${max}`, ...rest.ui },
36
+ }
37
+
38
+ if (type === 'float') {
39
+ return float<CollectionTypeInfo>({
40
+ ...(rest as FloatFieldConfig<CollectionTypeInfo>),
41
+ ...shared,
42
+ })
43
+ }
44
+
45
+ return integer<CollectionTypeInfo>({
46
+ ...(rest as IntegerFieldConfig<CollectionTypeInfo>),
47
+ ...shared,
48
+ })
49
+ }