drf-react-by-schema 0.6.3 → 0.6.4

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.
@@ -0,0 +1,11 @@
1
+ /// <reference types="react" />
2
+ import { CalendarPickerView } from '@mui/x-date-pickers/CalendarPicker';
3
+ type GridDateInputProps = {
4
+ field: string;
5
+ id: number | string;
6
+ value?: any;
7
+ column: object;
8
+ dateViews?: CalendarPickerView[] | undefined;
9
+ };
10
+ export declare const GridDateInput: ({ id, value, field, dateViews, }: GridDateInputProps) => JSX.Element;
11
+ export {};
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.GridDateInput = void 0;
16
+ const react_1 = __importDefault(require("react"));
17
+ const x_data_grid_1 = require("@mui/x-data-grid");
18
+ const TextField_1 = __importDefault(require("@mui/material/TextField"));
19
+ const DesktopDatePicker_1 = require("@mui/x-date-pickers/DesktopDatePicker");
20
+ const utils_1 = require("../../utils");
21
+ function buildOpenTo(dateViews) {
22
+ if (!dateViews || dateViews.includes('day')) {
23
+ return 'day';
24
+ }
25
+ if (dateViews.includes('month')) {
26
+ return 'month';
27
+ }
28
+ return 'year';
29
+ }
30
+ const GridDateInput = ({ id, value, field, dateViews, }) => {
31
+ const apiRef = (0, x_data_grid_1.useGridApiContext)();
32
+ const inputFormat = (0, utils_1.buildDateFormatBySchema)(dateViews);
33
+ const handleChange = (newValue) => __awaiter(void 0, void 0, void 0, function* () {
34
+ yield apiRef.current.setEditCellValue({ id, field, value: newValue });
35
+ apiRef.current.stopCellEditMode({ id, field });
36
+ });
37
+ const openTo = buildOpenTo(dateViews);
38
+ return (react_1.default.createElement(DesktopDatePicker_1.DesktopDatePicker, { key: field, value: value, onChange: handleChange, views: dateViews, openTo: openTo, inputFormat: inputFormat, renderInput: (params) => (react_1.default.createElement(TextField_1.default, Object.assign({}, params, { margin: "normal" }))) }));
39
+ };
40
+ exports.GridDateInput = GridDateInput;
@@ -63,7 +63,6 @@ const filter = (0, Autocomplete_1.createFilterOptions)();
63
63
  function SelectEditInputCell({ field, id, value, column, type, optionsAC, isIndexField, multiple = false, onEditModel, sx = {}, }) {
64
64
  // TODO: allow edit option label, as in formautocomplete!
65
65
  const apiRef = (0, x_data_grid_1.useGridApiContext)();
66
- console.log(column);
67
66
  const handleChange = (newValue) => __awaiter(this, void 0, void 0, function* () {
68
67
  yield apiRef.current.setEditCellValue({ id, field, value: newValue });
69
68
  apiRef.current.stopCellEditMode({ id, field });
@@ -64,6 +64,7 @@ const DRFReactBySchemaContext_1 = require("../context/DRFReactBySchemaContext");
64
64
  const utils_2 = require("./DataGridBySchemaEditable/utils");
65
65
  const CustomToolbar_1 = require("./DataGridBySchemaEditable/CustomToolbar");
66
66
  const SelectEditInputCell_1 = require("./DataGridBySchemaEditable/SelectEditInputCell");
67
+ const GridDateInput_1 = require("./DataGridBySchemaEditable/GridDateInput");
67
68
  const GridDecimalInput_1 = require("./DataGridBySchemaEditable/GridDecimalInput");
68
69
  const GridPatternInput_1 = require("./DataGridBySchemaEditable/GridPatternInput");
69
70
  const BooleanInputCell_1 = require("./DataGridBySchemaEditable/BooleanInputCell");
@@ -180,10 +181,15 @@ const DataGridBySchemaEditable = (0, react_1.forwardRef)((_a, ref) => {
180
181
  switch (schema[col.field].type) {
181
182
  case 'date':
182
183
  column.type = 'date';
183
- column.valueFormatter = (params) => params.value ? (0, moment_1.default)(params.value).format('DD/MM/YYYY') : '';
184
+ const dateFormat = (0, utils_1.buildDateFormatBySchema)(schema[col.field].date_views);
185
+ column.valueFormatter = (params) => params.value ? (0, moment_1.default)(params.value).format(dateFormat) : '';
184
186
  column.filterOperators = (0, utils_2.quantityOnlyOperators)({
185
187
  type: 'date',
186
188
  });
189
+ if (isEditable) {
190
+ column.renderEditCell = (params) => (react_1.default.createElement(GridDateInput_1.GridDateInput, Object.assign({}, params, { column: column, dateViews: schema[col.field].date_views })));
191
+ break;
192
+ }
187
193
  break;
188
194
  case 'datetime':
189
195
  column.type = 'dateTime';
package/dist/utils.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { CalendarPickerView } from '@mui/x-date-pickers/CalendarPicker';
1
2
  import { GridActionsColDef, GridColDef, GridFilterModel, GridSortModel } from '@mui/x-data-grid';
2
3
  export type Id = string | number;
3
4
  export type Item = Record<string, any>;
@@ -40,6 +41,7 @@ export interface Field {
40
41
  related_editable?: boolean;
41
42
  validators_regex?: Item[];
42
43
  many?: boolean;
44
+ date_views?: CalendarPickerView[];
43
45
  }
44
46
  interface GridBySchemaColDef extends GridColDef {
45
47
  isIndexField?: boolean;
@@ -96,4 +98,5 @@ export declare const isTmpId: (id: string | number | undefined | null) => boolea
96
98
  export declare const reducer: (state: Record<string, any> | null, newState: Record<string, any>) => Record<string, any> | null;
97
99
  export declare const getPatternFormat: (type: string) => string;
98
100
  export type AddParametersToEnd<TFunction extends (...args: any) => any, TParameters extends [...args: any]> = (...args: [...Parameters<TFunction>, ...TParameters]) => ReturnType<TFunction>;
101
+ export declare function buildDateFormatBySchema(dateViews: string[] | null | undefined): "DD/MM/yyyy" | "MM/yyyy" | "yyyy" | "MM" | "DD";
99
102
  export {};
package/dist/utils.js CHANGED
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.getPatternFormat = exports.reducer = exports.isTmpId = exports.getTmpId = exports.errorProps = exports.buildGenericYupValidationSchema = exports.populateValues = exports.getChoiceByValue = exports.emptyByType = void 0;
26
+ exports.buildDateFormatBySchema = exports.getPatternFormat = exports.reducer = exports.isTmpId = exports.getTmpId = exports.errorProps = exports.buildGenericYupValidationSchema = exports.populateValues = exports.getChoiceByValue = exports.emptyByType = void 0;
27
27
  const Yup = __importStar(require("yup"));
28
28
  ;
29
29
  ;
@@ -288,3 +288,29 @@ const getPatternFormat = (type) => {
288
288
  return format;
289
289
  };
290
290
  exports.getPatternFormat = getPatternFormat;
291
+ function buildDateFormatBySchema(dateViews) {
292
+ const defaultFormat = 'DD/MM/yyyy';
293
+ if (!dateViews) {
294
+ return defaultFormat;
295
+ }
296
+ const hasDay = dateViews.includes('day');
297
+ const hasMonth = dateViews.includes('month');
298
+ const hasYear = dateViews.includes('year');
299
+ if (hasDay && hasMonth && hasYear) {
300
+ return defaultFormat;
301
+ }
302
+ if (!hasDay && hasMonth && hasYear) {
303
+ return 'MM/yyyy';
304
+ }
305
+ if (!hasDay && !hasMonth && hasYear) {
306
+ return 'yyyy';
307
+ }
308
+ if (!hasDay && hasMonth && !hasYear) {
309
+ return 'MM';
310
+ }
311
+ if (hasDay && !hasMonth && !hasYear) {
312
+ return 'DD';
313
+ }
314
+ return defaultFormat;
315
+ }
316
+ exports.buildDateFormatBySchema = buildDateFormatBySchema;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "drf-react-by-schema",
3
- "version": "0.6.3",
3
+ "version": "0.6.4",
4
4
  "description": "Components and Tools for building a React App having Django Rest Framework (DRF) as server",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",