|
1 |
| -import { |
2 |
| - ClearIndicator, |
3 |
| - DropdownIndicator, |
4 |
| - MenuList, |
5 |
| - Option, |
6 |
| - customSelectStyles, |
7 |
| - selectTheme, |
8 |
| -} from '@plone/volto/components/manage/Widgets/SelectStyling'; |
9 |
| -import { Input, Pagination, Table } from 'semantic-ui-react'; |
| 1 | +import { Pagination, Table } from 'semantic-ui-react'; |
10 | 2 | import { usePagination, useTable } from 'react-table';
|
11 |
| -import { useSelector } from 'react-redux'; |
12 | 3 | import { Icon } from '@plone/volto/components';
|
13 | 4 | import React from 'react';
|
14 | 5 | import { compose } from 'redux';
|
15 | 6 | import deleteSVG from '@plone/volto/icons/delete.svg';
|
16 | 7 | import { injectLazyLibs } from '@plone/volto/helpers/Loadable/Loadable';
|
17 |
| -import { map } from 'lodash'; |
18 |
| -import { normalizeValue } from '@plone/volto/components/manage/Widgets/SelectUtils'; |
19 | 8 | import paginationLeftSVG from '@plone/volto/icons/left-key.svg';
|
20 | 9 | import paginationRightSVG from '@plone/volto/icons/right-key.svg';
|
21 | 10 | import plusSVG from '@plone/volto/icons/circle-plus.svg';
|
22 |
| -import { useIntl } from 'react-intl'; |
23 |
| -import DatetimeWidget from '@plone/volto/components/manage/Widgets/DatetimeWidget'; |
24 |
| -import moment from 'moment'; |
25 |
| -const FieldEditor = (props) => { |
26 |
| - const { |
27 |
| - fieldSchema, |
28 |
| - value, |
29 |
| - onChange, |
30 |
| - onChangeSelect, |
31 |
| - onChangeDate, |
32 |
| - onBlur, |
33 |
| - reactSelect, |
34 |
| - } = props; |
35 |
| - const Select = reactSelect.default; |
36 |
| - |
37 |
| - const { |
38 |
| - type, |
39 |
| - choices, |
40 |
| - id, |
41 |
| - minimum, |
42 |
| - maximum, |
43 |
| - defaultValue, |
44 |
| - isMulti, |
45 |
| - } = fieldSchema; |
46 |
| - const intl = useIntl(); |
47 |
| - const normalizedValue = normalizeValue(choices, value, intl); |
48 |
| - if (choices) { |
49 |
| - const options = [ |
50 |
| - ...map(choices, (option) => ({ |
51 |
| - value: option[0], |
52 |
| - label: |
53 |
| - // Fix "None" on the serializer, to remove when fixed in p.restapi |
54 |
| - option[1] !== 'None' && option[1] ? option[1] : option[0], |
55 |
| - })), |
56 |
| - ]; |
57 |
| - return ( |
58 |
| - <Select |
59 |
| - id={`field-${id}`} |
60 |
| - value={normalizedValue} |
61 |
| - className="react-table-select-field" |
62 |
| - theme={selectTheme} |
63 |
| - isMulti={isMulti} |
64 |
| - components={{ |
65 |
| - ...(choices?.length > 25 && { |
66 |
| - MenuList, |
67 |
| - }), |
68 |
| - DropdownIndicator, |
69 |
| - ClearIndicator, |
70 |
| - Option: Option, |
71 |
| - }} |
72 |
| - options={options} |
73 |
| - styles={customSelectStyles} |
74 |
| - onChange={(e) => onChangeSelect(e, isMulti)} |
75 |
| - onBlur={onBlur} |
76 |
| - /> |
77 |
| - ); |
78 |
| - } |
79 |
| - if (type === 'number') { |
80 |
| - return ( |
81 |
| - <Input |
82 |
| - id={`field-${id}`} |
83 |
| - className="react-table-integer-field" |
84 |
| - type="number" |
85 |
| - min={minimum || null} |
86 |
| - max={maximum || null} |
87 |
| - value={value || defaultValue} |
88 |
| - onChange={onChange} |
89 |
| - onBlur={onBlur} |
90 |
| - /> |
91 |
| - ); |
92 |
| - } |
93 |
| - if (type === 'date') { |
94 |
| - return ( |
95 |
| - <DatetimeWidget |
96 |
| - id={`field-${id}`} |
97 |
| - title="date" |
98 |
| - value={value || defaultValue} |
99 |
| - onChange={onChangeDate} |
100 |
| - onBlur={onBlur} |
101 |
| - /> |
102 |
| - ); |
103 |
| - } |
104 |
| - return ( |
105 |
| - <Input |
106 |
| - id={`field-${id}`} |
107 |
| - type="text" |
108 |
| - className="react-table-text-field" |
109 |
| - fluid |
110 |
| - value={value} |
111 |
| - onChange={onChange} |
112 |
| - onBlur={onBlur} |
113 |
| - /> |
114 |
| - ); |
115 |
| -}; |
116 |
| -// Create an editable cell renderer |
117 |
| -const EditableCell = ({ |
118 |
| - value: initialValue, |
119 |
| - row: { index }, |
120 |
| - column: { id }, |
121 |
| - updateCell, // This is a custom function that we supplied to our table instance |
122 |
| - selectedRow, |
123 |
| - setSelectedRow, |
124 |
| - schema, |
125 |
| - reactSelect, |
126 |
| - state: { pageIndex, pageSize }, |
127 |
| -}) => { |
128 |
| - const fieldSchema = { ...schema?.properties?.[id], id: id }; |
129 |
| - const locale = useSelector((state) => state.intl.locale); |
130 |
| - moment.locale(locale); |
131 |
| - |
132 |
| - const [value, setValue] = React.useState(initialValue); |
133 |
| - const onChange = (e) => { |
134 |
| - setValue(e.target.value); |
135 |
| - }; |
136 |
| - |
137 |
| - const onChangeSelect = (e, isMulti) => { |
138 |
| - if (!isMulti) { |
139 |
| - setValue(e.value); |
140 |
| - } else { |
141 |
| - setValue(e.map((value) => value.value)); |
142 |
| - } |
143 |
| - }; |
144 |
| - |
145 |
| - const onChangeDate = (e, dateValue) => { |
146 |
| - // console.log(dateValue); |
147 |
| - setValue(dateValue); |
148 |
| - }; |
149 |
| - |
150 |
| - const onBlur = () => { |
151 |
| - updateCell(index, id, value); |
152 |
| - }; |
153 |
| - |
154 |
| - React.useEffect(() => { |
155 |
| - setValue(initialValue); |
156 |
| - }, [initialValue]); |
157 |
| - return selectedRow === index ? ( |
158 |
| - // eslint-disable-next-line jsx-a11y/no-autofocus |
159 |
| - <FieldEditor |
160 |
| - fieldSchema={fieldSchema} |
161 |
| - value={value} |
162 |
| - onChange={onChange} |
163 |
| - onChangeSelect={onChangeSelect} |
164 |
| - onChangeDate={onChangeDate} |
165 |
| - onBlur={onBlur} |
166 |
| - reactSelect={reactSelect} |
167 |
| - /> |
168 |
| - ) : ( |
169 |
| - <span |
170 |
| - role="button" |
171 |
| - className="editable-cell" |
172 |
| - tabIndex={0} |
173 |
| - onClick={() => { |
174 |
| - setSelectedRow(index); |
175 |
| - }} |
176 |
| - onKeyDown={() => { |
177 |
| - setSelectedRow(index); |
178 |
| - }} |
179 |
| - onFocus={() => { |
180 |
| - setSelectedRow(index); |
181 |
| - }} |
182 |
| - > |
183 |
| - {fieldSchema.type === 'date' && value ? ( |
184 |
| - moment(value).format('LLL') |
185 |
| - ) : !fieldSchema.isMulti ? ( |
186 |
| - value || <> </> |
187 |
| - ) : value ? ( |
188 |
| - value.join(', ') |
189 |
| - ) : ( |
190 |
| - <> </> |
191 |
| - )} |
192 |
| - </span> |
193 |
| - ); |
194 |
| -}; |
| 11 | +import { EditableCell } from './EditableCell'; |
195 | 12 |
|
196 | 13 | const defaultColumn = {
|
197 | 14 | Cell: EditableCell,
|
|
0 commit comments