5htp-core 0.2.6-8 → 0.2.6-9

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "5htp-core",
3
3
  "description": "Convenient TypeScript framework designed for Performance and Productivity.",
4
- "version": "0.2.6-8",
4
+ "version": "0.2.6-9",
5
5
  "author": "Gaetan Le Gac (https://github.com/gaetanlegac)",
6
6
  "repository": "git://github.com/gaetanlegac/5htp-core.git",
7
7
  "license": "MIT",
@@ -24,8 +24,8 @@ export { default as Service } from './service';
24
24
  declare global {
25
25
  interface Window {
26
26
  dev: boolean,
27
- // Defined by loading gtag.js
28
- gtag: (action: string, name: string, params?: any) => void,
27
+ /*context: ClientContext,
28
+ user: User,*/
29
29
  /*context: ClientContext,
30
30
  user: User,*/
31
31
  }
@@ -16,6 +16,7 @@ export { default as Video } from './Video';
16
16
  export { default as Number } from './input/Number';
17
17
  export { default as Slider } from './input/Slider';
18
18
  export { default as Radio } from './input/Radio';
19
+ export { default as Checkbox } from './input/Checkbox';
19
20
 
20
21
  // Data
21
22
  export { default as Amount } from './Amount';
File without changes
@@ -1,74 +1,82 @@
1
1
  /*----------------------------------
2
2
  - DEPENDANCES
3
3
  ----------------------------------*/
4
+
5
+ // Npm
4
6
  import React from 'react';
5
- import Champ from '../Base';
6
- import { ComponentChild } from 'preact';
7
+ import { JSX, ComponentChild } from 'preact';
7
8
 
8
- import uid from '@common/data/id';
9
+ // Core libs
10
+ import { useInput, InputBaseProps } from '../../inputv3/base';
9
11
 
10
12
  /*----------------------------------
11
13
  - TYPES
12
14
  ----------------------------------*/
13
- type TValeur = boolean;
14
- const valeurDefaut = false as boolean;
15
- type TValeurDefaut = typeof valeurDefaut;
16
- type TValeurOut = string;
17
-
18
15
  export type Props = {
19
- valeur: TValeur,
20
- switch?: boolean,
21
- children?: ComponentChild
16
+ id: string,
17
+ label: ComponentChild,
18
+ // State
19
+ inputRef?: React.Ref<HTMLInputElement>
22
20
  }
23
21
 
24
22
  /*----------------------------------
25
- - COMPOSANTS
23
+ - COMPOSANT
26
24
  ----------------------------------*/
27
- import './index.less';
28
- export default Champ<Props, TValeurDefaut, TValeurOut>('checkbox', { valeurDefaut }, ({
29
- label, titre, children, switch: isSwitch, attrsContChamp, attrsChamp, nom, description,
30
- readOnly
31
- }, { valeur, state, setState }, rendre) => {
25
+ export default ({
26
+ id,
27
+ // Decoration
28
+ required,
29
+ label: labelText,
30
+ // State
31
+ inputRef, errors,
32
+ ...props
33
+ }: Props & InputBaseProps<boolean> & Omit<JSX.HTMLAttributes<HTMLInputElement>, 'onChange'>) => {
32
34
 
33
- if (label === undefined)
34
- label = true;
35
- if (!titre && children)
36
- titre = children;
35
+ /*----------------------------------
36
+ - INIT
37
+ ----------------------------------*/
37
38
 
38
- if (isSwitch) {
39
- attrsContChamp.className += ' switch';
40
- if (label)
41
- attrsContChamp.className += ' avecLabel';
42
- } else
43
- attrsContChamp.className += ' classique';
39
+ const [{ value, focus, fieldProps }, setValue, commitValue, setState] = useInput(props, false, true);
44
40
 
45
- if (state.chargement)
46
- attrsContChamp.disabled = true;
41
+ const refInput = inputRef || React.useRef<HTMLInputElement>();
42
+
43
+ /*----------------------------------
44
+ - ATTRIBUTES
45
+ ----------------------------------*/
47
46
 
48
- attrsChamp.id = 'check_' + (nom || uid());
47
+ let className: string = 'input checkbox row';
49
48
 
50
- if (readOnly)
51
- return rendre(valeur ? 'Yes' : 'No', {});
49
+ if (focus)
50
+ className += ' focus';
51
+ if (errors?.length)
52
+ className += ' error';
52
53
 
53
- return rendre(<>
54
+ if (props.className !== undefined)
55
+ className += ' ' + props.className;
54
56
 
55
- <input type="checkbox" {...attrsChamp}
56
- onChange={() => {
57
- setState({ valeur: !valeur });
58
- }}
59
- checked={valeur}
60
- />
57
+ /*----------------------------------
58
+ - RENDER
59
+ ----------------------------------*/
60
+ return <>
61
+ <div class={className} onClick={() => refInput.current?.focus()}>
61
62
 
62
- {/* On ne peut pas rendre le switch + le texte du label dans le même <label> */}
63
- {(true || (isSwitch && label)) && (
64
- <label htmlFor={attrsChamp.id} />
65
- )}
63
+ <input type="checkbox"
64
+ onChange={() => {
65
+ setValue( !value );
66
+ }}
67
+ checked={value}
68
+ />
66
69
 
67
- <div className="contLabel">
68
- <label htmlFor={attrsChamp.id}>{titre}</label>
69
-
70
- {description && <p className="desc">{description}</p>}
70
+ <label htmlFor={id} class="col-1 txt-left">
71
+ {labelText}
72
+ </label>
73
+
71
74
  </div>
72
-
73
- </>, { ecraser: true });
74
- })
75
+
76
+ {errors?.length && (
77
+ <div class="fg error txt-left">
78
+ {errors.join('. ')}
79
+ </div>
80
+ )}
81
+ </>
82
+ }
@@ -0,0 +1,74 @@
1
+ /*----------------------------------
2
+ - DEPENDANCES
3
+ ----------------------------------*/
4
+ import React from 'react';
5
+ import Champ from '../Base';
6
+ import { ComponentChild } from 'preact';
7
+
8
+ import uid from '@common/data/id';
9
+
10
+ /*----------------------------------
11
+ - TYPES
12
+ ----------------------------------*/
13
+ type TValeur = boolean;
14
+ const valeurDefaut = false as boolean;
15
+ type TValeurDefaut = typeof valeurDefaut;
16
+ type TValeurOut = string;
17
+
18
+ export type Props = {
19
+ valeur: TValeur,
20
+ switch?: boolean,
21
+ children?: ComponentChild
22
+ }
23
+
24
+ /*----------------------------------
25
+ - COMPOSANTS
26
+ ----------------------------------*/
27
+ import './index.less';
28
+ export default Champ<Props, TValeurDefaut, TValeurOut>('checkbox', { valeurDefaut }, ({
29
+ label, titre, children, switch: isSwitch, attrsContChamp, attrsChamp, nom, description,
30
+ readOnly
31
+ }, { valeur, state, setState }, rendre) => {
32
+
33
+ if (label === undefined)
34
+ label = true;
35
+ if (!titre && children)
36
+ titre = children;
37
+
38
+ if (isSwitch) {
39
+ attrsContChamp.className += ' switch';
40
+ if (label)
41
+ attrsContChamp.className += ' avecLabel';
42
+ } else
43
+ attrsContChamp.className += ' classique';
44
+
45
+ if (state.chargement)
46
+ attrsContChamp.disabled = true;
47
+
48
+ attrsChamp.id = 'check_' + (nom || uid());
49
+
50
+ if (readOnly)
51
+ return rendre(valeur ? 'Yes' : 'No', {});
52
+
53
+ return rendre(<>
54
+
55
+ <input type="checkbox" {...attrsChamp}
56
+ onChange={() => {
57
+ setState({ valeur: !valeur });
58
+ }}
59
+ checked={valeur}
60
+ />
61
+
62
+ {/* On ne peut pas rendre le switch + le texte du label dans le même <label> */}
63
+ {(true || (isSwitch && label)) && (
64
+ <label htmlFor={attrsChamp.id} />
65
+ )}
66
+
67
+ <div className="contLabel">
68
+ <label htmlFor={attrsChamp.id}>{titre}</label>
69
+
70
+ {description && <p className="desc">{description}</p>}
71
+ </div>
72
+
73
+ </>, { ecraser: true });
74
+ })
@@ -37,6 +37,7 @@ export type TInputState<TValue> = {
37
37
  export function useInput<TValue>(
38
38
  { value: externalValue, onChange }: InputBaseProps<TValue>,
39
39
  defaultValue: TValue,
40
+ autoCommit: boolean = false
40
41
  ): [
41
42
  state: TInputState<TValue>,
42
43
  setValue: (value: TValue) => void,
@@ -52,7 +53,12 @@ export function useInput<TValue>(
52
53
  changed: false
53
54
  });
54
55
 
55
- const setValue = (value: TValue) => setState({ value, valueSource: 'internal', changed: true });
56
+ const setValue = (value: TValue) => {
57
+ setState({ value, valueSource: 'internal', changed: true });
58
+
59
+ if (autoCommit)
60
+ commitValue(value);
61
+ };
56
62
 
57
63
  const commitValue = () => {
58
64
 
@@ -75,6 +81,12 @@ export function useInput<TValue>(
75
81
 
76
82
  }, [externalValue]);
77
83
 
84
+ React.useEffect(() => {
85
+ if (state.valueSource === 'internal') {
86
+ commitValue();
87
+ }
88
+ }, [state.value]);
89
+
78
90
  return [state, setValue, commitValue, setState]
79
91
  }
80
92