jrs-react 1.2.48 → 1.2.50

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.
@@ -5,307 +5,308 @@ import styled from 'styled-components'
5
5
  import { showMask } from './JRMask/JRMask'
6
6
 
7
7
  const axiosSubmit = axios.create({
8
- authorization: `Bearer ${localStorage.getItem("accessToken")}`
9
- , timeout: 120000
10
- , maxBodyLength: 104857600 //100mb 104857600
11
- , maxContentLength: 104857600 //100mb
8
+ authorization: `Bearer ${localStorage.getItem("accessToken")}`
9
+ ,timeout: 120000
10
+ ,maxBodyLength: 104857600 //100mb 104857600
11
+ ,maxContentLength: 104857600 //100mb
12
12
  })
13
13
 
14
- const StyledCover = styled.div`
14
+ const StyledCover=styled.div`
15
15
  border:2px solid red;
16
16
  `
17
- const Cover = ({ children }) => {
18
- return <StyledCover>{children}</StyledCover>
17
+ const Cover=({children})=>{
18
+ return <StyledCover>{children}</StyledCover>
19
19
  }
20
20
 
21
21
  export default class JRSubmit extends React.Component {
22
- #methods = ['get', 'post', 'put', 'patch', 'delete', 'download']
23
-
24
- constructor(props) {
25
- super(props)
26
- if (this.props.value === undefined && this.props.initValue !== undefined) {
27
- if (this.props.onChange === undefined) {
28
- this.state = {
29
- value: props.initValue
30
- , rawValue: JSON.parse(JSON.stringify(props.initValue))
22
+ #methods = ['get', 'post', 'put','patch','delete','download']
23
+ constructor(props) {
24
+ super(props)
25
+ if(this.props.value===undefined && this.props.initValue!==undefined ){
26
+ if(this.props.onChange===undefined){
27
+ this.state={
28
+ value:props.initValue
29
+ ,rawValue:JSON.parse(JSON.stringify(props.initValue))
30
+ }
31
+ }
31
32
  }
32
- }
33
33
  }
34
- }
35
- componentDidMount() {
36
- this.#methods
37
- .filter((method) => this[method] && this.props[method] && this.props[method].autoRun)
38
- .forEach((method) => {
39
- if (this.props[method].autoRun === true) {
40
- this[method]()
41
- } else if (this.props[method].autoRun > -1) {
42
- setTimeout(() => {
43
- this[method]()
44
- }, this.props[method].autoRun)
45
- }
46
- })
47
- }
48
- get(_config = {}) {
49
- const config = Object.assign(
50
- {
51
- method: 'get'
52
- , updateValue: true
53
- // ,updateRawValue:true
54
- }
55
- , this.props['get']
56
- , _config
57
- )
58
- this.submit(config)
59
- }
60
-
61
- post(config = {}) {
62
- config = Object.assign(
63
- {
64
- method: 'post'
65
- , updateValue: false
66
- }
67
- , this.props['post']
68
- , config
69
- )
70
- this.submit(config)
71
- }
72
-
73
- put(config = {}) {
74
- config = Object.assign(
75
- {
76
- method: 'put'
77
- , updateValue: false
78
- }
79
- , this.props['put']
80
- , config
81
- )
82
- this.submit(config)
83
- }
84
-
85
- delete(config = {}) {
86
- config = Object.assign(
87
- {
88
- method: 'delete'
89
- , updateValue: false
90
- }
91
- , this.props['delete']
92
- , config
93
- )
94
- this.submit(config)
95
- }
96
-
97
- get from() {
98
- return this.props.value === undefined ? 'state' : 'props'
99
- }
100
-
101
- get isDirty() {
102
- return this.state?.isDirty
103
- }
104
- set isDirty(isDirty) {
105
- this.setState({ isDirty })
106
- }
107
- get rawValue() {
108
- return this.state?.rawValue
109
- }
110
- setRawValue(rawValue) {
111
- this.setState({ rawValue: rawValue != null ? JSON.parse(JSON.stringify(rawValue)) : null })
112
- }
113
- reset() {
114
- this.setValue(this.rawValue, true)
115
- }
116
- setValue(value, reset = false) {
117
- if (this.props.onChange) {
118
- this.props.onChange(value)
119
- } else {
120
- this.setState({ value })
121
- }
122
-
123
- if (reset) {
124
- this.setRawValue(value)
34
+ componentDidMount(){
35
+ this.#methods
36
+ .filter((method) => this[method] && this.props[method] && this.props[method].autoRun)
37
+ .forEach((method) => {
38
+ if(this.props[method].autoRun===true){
39
+ this[method]()
40
+ } else if(this.props[method].autoRun>-1){
41
+ setTimeout(()=>{
42
+ this[method]()
43
+ },this.props[method].autoRun)
44
+ }
45
+ })
125
46
  }
126
- this.isDirty = !reset
127
- }
128
- setRes(isSuccess, response, config) {
129
- if (isSuccess && config.updateValue) {
130
- const value = config.formatValue?.bind(this)(response.data) ?? response.data
131
- response.data = value
132
- this.setValue(value, true)
133
- } else if (config.updateValue) {
134
- this.setValue(null, true)
47
+ get(_config = {}) {
48
+ const config = Object.assign(
49
+ {
50
+ method:'get'
51
+ ,updateValue:true
52
+ // ,updateRawValue:true
53
+ }
54
+ ,this.props['get']
55
+ ,_config
56
+ )
57
+ this.submit(config)
135
58
  }
136
- }
137
- #getValueByName(fullnamList, record) {
138
- const name = fullnamList.shift()
139
- if (fullnamList.length == 0) {
140
- return record?.[name]
141
- } else if (record?.[name] != null) {
142
- return this.#getValueByName(fullnamList, record[name])
59
+
60
+ post(config = {}) {
61
+ config = Object.assign(
62
+ {
63
+ method:'post'
64
+ ,updateValue:false
65
+ }
66
+ ,this.props['post']
67
+ ,config
68
+ )
69
+ this.submit(config)
143
70
  }
144
- }
145
71
 
146
- getValue(fullname) {
147
- if (fullname) {
148
- const fullnamList = fullname.split('.')
149
- return this.#getValueByName(fullnamList, this[this.from]?.value)
150
- } else {
151
- // 忘記為甚麼這樣寫了 XDD
152
- return flexType(this[this.from]?.value, this, null, null)
72
+ put(config = {}) {
73
+ config = Object.assign(
74
+ {
75
+ method:'put'
76
+ ,updateValue:false
77
+ }
78
+ ,this.props['put']
79
+ ,config
80
+ )
81
+ this.submit(config)
153
82
  }
154
- }
155
-
156
- getSubmitValue() {
157
- return this.getValue()
158
- }
159
83
 
160
- getAxiosParams({ url, method, value, extraValue, sendValue, transformValue, responseType = 'json', ...params }) {
161
-
162
- const _extraValue = extraValue?.bind?.(this)() ?? extraValue
163
- // let payload=typeof value === 'function'
164
- // ?value.bind(this)({
165
- // ...this.getSubmitValue()
166
- // ,..._extraValue
167
- // })
168
- // :value==null
169
- // ?{
170
- // ...this.getSubmitValue()
171
- // ,..._extraValue
172
- // }
173
- // :{
174
- // ...value
175
- // ,..._extraValue
176
- // }
177
- let payload = value?.bind?.(this)({
178
- ...this.getSubmitValue()
179
- , ..._extraValue
180
- })
181
- ?? value == null
182
- ? {
183
- ...this.getSubmitValue()
184
- , ..._extraValue
185
- }
186
- : {
187
- ...value
188
- , ..._extraValue
189
- }
84
+ delete(config = {}) {
85
+ config = Object.assign(
86
+ {
87
+ method:'delete'
88
+ ,updateValue:false
89
+ }
90
+ ,this.props['delete']
91
+ ,config
92
+ )
93
+ this.submit(config)
94
+ }
190
95
 
191
- if (transformValue) {
192
- payload = transformValue.bind(this)(payload)
96
+ get from(){
97
+ return this.props.value===undefined?'state':'props'
193
98
  }
194
- const headers = {
195
- authorization: `Bearer ${localStorage.getItem("accessToken")}`
99
+
100
+ get isDirty(){
101
+ return this.state?.isDirty
196
102
  }
197
- let params1 = { responseType }
198
- const params2 = {}
199
- if (method == 'get') {
200
- if (sendValue == null || sendValue) params1.params = payload
201
- params1.headers = headers
202
- } else if (method == 'delete') {
203
- if (sendValue == null || sendValue) params1.data = payload
204
- params1.headers = headers
205
- } else {
206
- if (sendValue == null || sendValue) {
207
- params1 = payload
208
- } else {
209
- params1 = null
210
- }
211
- params2.headers = headers
103
+ set isDirty(isDirty){
104
+ this.setState({isDirty})
212
105
  }
213
- return {
214
- url: colonValueString(url, payload)
215
- , method
216
- , params1
217
- , params2
218
- , payload
106
+ get rawValue(){
107
+ return this.state?.rawValue
219
108
  }
220
- }
221
-
222
- reload() {
223
- po('reload 還沒有做 ')
224
- }
225
-
226
- submit(config) {
227
- if (config.mask) {
228
- config.removeMaskFunction = showMask(config.mask, this.maskId ?? 'jr-mask')
109
+ setRawValue(rawValue){
110
+ this.setState({rawValue:rawValue!=null?JSON.parse(JSON.stringify(rawValue)):null})
229
111
  }
230
-
231
- let _payload
232
- ; (() => {
233
- if (Array.isArray(config.url)) {
234
- _payload = []
235
- return Promise.allSettled(
236
- config.url.map((_url) => {
237
- const urlParams = typeof _url === 'object'
238
- ? _url
239
- : { url: _url }
240
- const finalConfig = Object.assign(config, urlParams)
241
- const { url, method, params1, params2, payload } = this.getAxiosParams(finalConfig)
242
- _payload.push(payload)
243
- return axiosSubmit[method](url, params1, params2)
244
- })
245
- )
246
- } else {
247
- const { url, method, params1, params2, payload } = this.getAxiosParams(config)
248
- _payload = payload
249
- return axiosSubmit[method](url, params1, params2)
112
+ reset(){
113
+ this.setValue(this.rawValue,true)
114
+ }
115
+ setValue(value,reset=false){
116
+ if(this.props.onChange){
117
+ this.props.onChange(value)
118
+ }else{
119
+ this.setState({value})
250
120
  }
251
- })()
252
- .then((res) => {
253
- this.handleResponse(res, _payload, config)
254
- })
255
- .catch((res) => {
256
- this.handleResponse(res, _payload, config)
257
- })
258
- // .finally(()=> {
259
- // config.removeMaskFunction?.()
260
- // })
261
- }
262
121
 
263
- handleResponse = (response, payload, config) => {
264
- if (Array.isArray(response)) {
265
- response = response.map((re, index) => {
266
- if (re.value) {
267
- re.value.payload = payload[index]
268
- } else if (re.reason) {
269
- re.reason.payload = payload[index]
122
+ if(reset){
123
+ this.setRawValue(value)
124
+ }
125
+ this.isDirty=!reset
126
+ }
127
+ setRes(isSuccess,response,config){
128
+ if (isSuccess && config.updateValue) {
129
+ const value=config.formatValue?.bind(this)(response.data)??response.data
130
+ response.data=value
131
+ this.setValue(value,true)
132
+ } else if(config.updateValue){
133
+ this.setValue(null,true)
134
+ }
135
+ }
136
+ #getValueByName(fullnamList,record){
137
+ const name=fullnamList.shift()
138
+ if(fullnamList.length==0){
139
+ return record?.[name]
140
+ }else if(record?.[name]!=null){
141
+ return this.#getValueByName(fullnamList,record[name])
270
142
  }
271
- return re
272
- })
273
- } else {
274
- response.payload = payload
275
143
  }
276
144
 
277
- response = config?.response?.bind(this)(response, payload) ?? response
278
- const isSuccess = response.status >= 200 && response.status <= 299
279
-
280
- this.setRes(isSuccess, response, config)
145
+ getValue(fullname){
146
+ if(fullname){
147
+ const fullnamList=fullname.split('.')
148
+ return this.#getValueByName(fullnamList,this[this.from]?.value)
149
+ }else{
150
+ // 忘記為甚麼這樣寫了 XDD
151
+ return flexType(this[this.from]?.value,this,null,null)
152
+ }
153
+ }
281
154
 
282
- if (config.delay != null) {
283
- setTimeout(() => {
284
- this.showMessage(isSuccess, isSuccess ? config.successMessage : config.failedMessage)
285
- config.callback?.bind(this)(isSuccess, response, payload)
286
- config.removeMaskFunction?.()
287
- }, config.delay)
288
- } else {
289
- this.showMessage(isSuccess, isSuccess ? config.successMessage : config.failedMessage)
290
- config.callback?.bind(this)(isSuccess, response, payload)
291
- config.removeMaskFunction?.()
155
+ getSubmitValue(){
156
+ return this.getValue()
292
157
  }
293
158
 
159
+ getAxiosParams({url,method,value,extraValue,sendValue,transformValue,paramsSerializer
160
+ ,...params
161
+ }){
162
+ po('params',params)
163
+ const _extraValue=extraValue?.bind?.(this)() ?? extraValue
164
+ // let payload=typeof value === 'function'
165
+ // ?value.bind(this)({
166
+ // ...this.getSubmitValue()
167
+ // ,..._extraValue
168
+ // })
169
+ // :value==null
170
+ // ?{
171
+ // ...this.getSubmitValue()
172
+ // ,..._extraValue
173
+ // }
174
+ // :{
175
+ // ...value
176
+ // ,..._extraValue
177
+ // }
178
+ let payload=value?.bind?.(this)({
179
+ ...this.getSubmitValue()
180
+ ,..._extraValue
181
+ })
182
+ ?? value==null
183
+ ?{
184
+ ...this.getSubmitValue()
185
+ ,..._extraValue
186
+ }
187
+ :{
188
+ ...value
189
+ ,..._extraValue
190
+ }
191
+
192
+ if(transformValue){
193
+ payload=transformValue.bind(this)(payload)
194
+ }
195
+ const headers={
196
+ authorization: `Bearer ${localStorage.getItem("accessToken")}`
197
+ }
198
+ let params1={}
199
+ const params2={}
200
+ if(method=='get'){
201
+ if(sendValue==null || sendValue) params1.params=payload
202
+ params1.headers=headers
203
+ params1.paramsSerializer={
204
+ indexes: null
205
+ ,...paramsSerializer
206
+ }
207
+ }else if(method=='delete'){
208
+ if(sendValue==null || sendValue) params1.data=payload
209
+ params1.headers=headers
210
+ }else{
211
+ if(sendValue==null || sendValue){
212
+ params1=payload
213
+ }else{
214
+ params1=null
215
+ }
216
+ params2.headers=headers
217
+ }
218
+ return {
219
+ url:colonValueString(url,payload)
220
+ ,method
221
+ ,params1
222
+ ,params2
223
+ ,payload
224
+ }
225
+ }
294
226
 
295
- }
227
+ reload(){
228
+ po('reload 還沒有做 ')
229
+ }
296
230
 
297
- showMessage(success, message) {
298
- if (message) {
299
- po('success', success)
300
- po('message', message)
231
+ submit(config){
232
+ if(config.mask) {
233
+ config.removeMaskFunction=showMask(config.mask,this.maskId??'jr-mask')
234
+ }
235
+
236
+ let _payload
237
+ ;(()=>{
238
+ if (Array.isArray(config.url)) {
239
+ _payload=[]
240
+ return Promise.allSettled(
241
+ config.url.map((_url) => {
242
+ const urlParams=typeof _url === 'object'
243
+ ?_url
244
+ :{url: _url}
245
+ const finalConfig=Object.assign(config,urlParams)
246
+ const {url,method,params1,params2,payload}=this.getAxiosParams(finalConfig)
247
+ _payload.push(payload)
248
+ return axiosSubmit[method](url,params1,params2)
249
+ })
250
+ )
251
+ }else{
252
+ const {url,method,params1,params2,payload}=this.getAxiosParams(config)
253
+ _payload=payload
254
+ return axiosSubmit[method](url,params1,params2)
255
+ }
256
+ })()
257
+ .then((res)=>{
258
+ this.handleResponse(res,_payload,config)
259
+ })
260
+ .catch((res)=>{
261
+ this.handleResponse(res,_payload,config)
262
+ })
263
+ // .finally(()=> {
264
+ // config.removeMaskFunction?.()
265
+ // })
301
266
  }
302
- }
303
267
 
304
- renderer() {
305
- return
306
- }
268
+ handleResponse=(response,payload,config)=>{
269
+ if(Array.isArray(response)){
270
+ response=response.map((re,index)=>{
271
+ if(re.value){
272
+ re.value.payload=payload[index]
273
+ }else if(re.reason){
274
+ re.reason.payload=payload[index]
275
+ }
276
+ return re
277
+ })
278
+ }else{
279
+ response.payload=payload
280
+ }
281
+
282
+ response = config?.response?.bind(this)(response,payload) ?? response
283
+ const isSuccess = response.status >= 200 && response.status <= 299
284
+ this.setRes(isSuccess,response,config)
285
+ if(config.delay!=null){
286
+ setTimeout(()=>{
287
+ this.showMessage(isSuccess,isSuccess?config.successMessage:config.failedMessage)
288
+ config.callback?.bind(this)(isSuccess,response,payload)
289
+ config.removeMaskFunction?.()
290
+ },config.delay)
291
+ }else{
292
+ this.showMessage(isSuccess,isSuccess?config.successMessage:config.failedMessage)
293
+ config.callback?.bind(this)(isSuccess,response,payload)
294
+ config.removeMaskFunction?.()
295
+ }
296
+ }
307
297
 
308
- render() {
309
- return this.renderer()
310
- }
298
+ showMessage(success,message){
299
+ if(message){
300
+ po('success',success)
301
+ po('message',message)
302
+ }
303
+ }
304
+
305
+ renderer(){
306
+ return
307
+ }
308
+
309
+ render() {
310
+ return this.renderer()
311
+ }
311
312
  }
@@ -74,13 +74,16 @@ class Backdrop extends JRHTML{
74
74
  Object.assign(this.backdropConfig.style,params.backdropStyle)
75
75
  }
76
76
 
77
-
78
77
  show(){
79
78
  this.backdrop=this.agregarHijo(this.backdropConfig)
79
+ this.on=true
80
80
  }
81
81
  hide(){
82
- this.yo.removeChild(this.yo.children[this.yo.children.length-1])
83
- this.hijos=[]
82
+ if(this.on){
83
+ this.yo.removeChild(this.yo.children[this.yo.children.length-1])
84
+ this.hijos=[]
85
+ this.on=false
86
+ }
84
87
  }
85
88
  }
86
89
 
@@ -147,6 +150,10 @@ export default class JRWindow extends JRFrame {
147
150
  }
148
151
  }
149
152
 
153
+ componentWillUnmount(){
154
+ this.backdropTarget?.hide()
155
+ }
156
+
150
157
  open(){
151
158
  this.backdropTarget?.show()
152
159
  this.props.onOpen?.bind(this)()
package/src/main.jsx CHANGED
@@ -1,10 +1,10 @@
1
- import { StrictMode } from 'react'
2
- import { createRoot } from 'react-dom/client'
3
- import './app/index.css'
4
- import App from './app/App.jsx'
5
- import JRFields from './components/JRFields/JRFields.jsx'
6
- import JRText from './components/JRInput/JRText.jsx'
7
-
8
- const root = createRoot(document.getElementById('root'))
9
- root.render(<App/>)
10
-
1
+ import { StrictMode } from 'react'
2
+ import { createRoot } from 'react-dom/client'
3
+ import './app/index.css'
4
+ import App from './app/App.jsx'
5
+ import JRFields from './components/JRFields/JRFields.jsx'
6
+ import JRText from './components/JRInput/JRText.jsx'
7
+
8
+ const root = createRoot(document.getElementById('root'))
9
+ root.render(<App/>)
10
+