@stephanluis/react-input-duration 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +39 -2
- package/index.d.ts +24 -0
- package/index.js +84 -0
- package/package.json +44 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 StephanLuis
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,40 @@
|
|
|
1
|
-
#
|
|
1
|
+
# React input-duration
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A React wrapper around the [`input-duration`](https://github.com/StephanLuis/input-duration) web component.
|
|
4
|
+
It supports React 18.2 and 19 and loads the component in the browser after mounting.
|
|
5
|
+
|
|
6
|
+
```sh
|
|
7
|
+
npm install @stephanluis/react-input-duration
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
```jsx
|
|
11
|
+
import { useState } from 'react';
|
|
12
|
+
import InputDuration from '@stephanluis/react-input-duration';
|
|
13
|
+
|
|
14
|
+
export function DurationExample() {
|
|
15
|
+
const [duration, setDuration] = useState('0:00:00:10.000');
|
|
16
|
+
return <InputDuration value={duration} onChange={setDuration} showDays />;
|
|
17
|
+
}
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
`onChange` receives the committed duration string, not an event. With `value`, the component is
|
|
21
|
+
controlled: update the value in `onChange` to retain edits. Alternatively use `defaultValue` for
|
|
22
|
+
an uncontrolled input. Use either `value` or `defaultValue`, not both.
|
|
23
|
+
|
|
24
|
+
The `showDays`, `noHours`, `noMinutes`, `noSeconds`, and `noMicroseconds` boolean props control
|
|
25
|
+
visible fields. Separator props are `daysDivider`, `hoursDivider`, `minutesDivider`,
|
|
26
|
+
`secondsDivider`, and `microsecondsDivider`. Standard attributes such as `name`, `required`,
|
|
27
|
+
`disabled`, `className`, and `aria-label` are supported. A ref points to the underlying custom
|
|
28
|
+
element, exposing its methods and properties. See the [component documentation](https://github.com/StephanLuis/input-duration#readme)
|
|
29
|
+
for formats, styling, and methods.
|
|
30
|
+
|
|
31
|
+
The browser loads and registers the web component after the wrapper mounts. Server-rendered HTML
|
|
32
|
+
contains the custom element but its editable fields appear only after client-side initialization.
|
|
33
|
+
|
|
34
|
+
## Development
|
|
35
|
+
|
|
36
|
+
From this directory, run `npm ci` followed by `npm test` to test the wrapper and its TypeScript types.
|
|
37
|
+
|
|
38
|
+
To try the local wrapper, run `python3 -m http.server 8000` from the repository root and open
|
|
39
|
+
`http://localhost:8000/packages/react-input-duration/demo.html`. The demo uses the local wrapper and
|
|
40
|
+
web component; React is loaded from a CDN, so it requires an internet connection.
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { ForwardRefExoticComponent, HTMLAttributes, RefAttributes } from 'react';
|
|
2
|
+
import type InputDurationElement from 'input-duration';
|
|
3
|
+
|
|
4
|
+
export interface InputDurationProps extends Omit<HTMLAttributes<InputDurationElement>, 'onChange' | 'defaultValue'> {
|
|
5
|
+
value?: string;
|
|
6
|
+
defaultValue?: string;
|
|
7
|
+
onChange?: (value: string) => void;
|
|
8
|
+
disabled?: boolean;
|
|
9
|
+
required?: boolean;
|
|
10
|
+
name?: string;
|
|
11
|
+
showDays?: boolean;
|
|
12
|
+
noHours?: boolean;
|
|
13
|
+
noMinutes?: boolean;
|
|
14
|
+
noSeconds?: boolean;
|
|
15
|
+
noMicroseconds?: boolean;
|
|
16
|
+
daysDivider?: string;
|
|
17
|
+
hoursDivider?: string;
|
|
18
|
+
minutesDivider?: string;
|
|
19
|
+
secondsDivider?: string;
|
|
20
|
+
microsecondsDivider?: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
declare const InputDuration: ForwardRefExoticComponent<InputDurationProps & RefAttributes<InputDurationElement>>;
|
|
24
|
+
export default InputDuration;
|
package/index.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { createElement, forwardRef, useCallback, useEffect, useRef, useState } from 'react';
|
|
2
|
+
|
|
3
|
+
const InputDuration = forwardRef(function InputDuration({
|
|
4
|
+
value,
|
|
5
|
+
defaultValue,
|
|
6
|
+
onChange,
|
|
7
|
+
showDays,
|
|
8
|
+
noHours,
|
|
9
|
+
noMinutes,
|
|
10
|
+
noSeconds,
|
|
11
|
+
noMicroseconds,
|
|
12
|
+
daysDivider,
|
|
13
|
+
hoursDivider,
|
|
14
|
+
minutesDivider,
|
|
15
|
+
secondsDivider,
|
|
16
|
+
microsecondsDivider,
|
|
17
|
+
disabled,
|
|
18
|
+
required,
|
|
19
|
+
...props
|
|
20
|
+
}, forwardedRef) {
|
|
21
|
+
const elementRef = useRef(null);
|
|
22
|
+
const initialValue = useRef(value ?? defaultValue);
|
|
23
|
+
const current = useRef({ value, onChange });
|
|
24
|
+
const [ready, setReady] = useState(false);
|
|
25
|
+
const [loadError, setLoadError] = useState(null);
|
|
26
|
+
current.current = { value, onChange };
|
|
27
|
+
const setRef = useCallback(element => {
|
|
28
|
+
elementRef.current = element;
|
|
29
|
+
if (typeof forwardedRef === 'function') forwardedRef(element);
|
|
30
|
+
else if (forwardedRef) forwardedRef.current = element;
|
|
31
|
+
}, [forwardedRef]);
|
|
32
|
+
|
|
33
|
+
useEffect(() => {
|
|
34
|
+
let mounted = true;
|
|
35
|
+
import('input-duration').then(() => {
|
|
36
|
+
if (mounted) setReady(true);
|
|
37
|
+
}, error => {
|
|
38
|
+
if (mounted) setLoadError(error);
|
|
39
|
+
});
|
|
40
|
+
return () => { mounted = false; };
|
|
41
|
+
}, []);
|
|
42
|
+
|
|
43
|
+
useEffect(() => {
|
|
44
|
+
if (!ready) return;
|
|
45
|
+
const element = elementRef.current;
|
|
46
|
+
if (initialValue.current !== undefined) element.value = initialValue.current;
|
|
47
|
+
const handleChange = () => {
|
|
48
|
+
const nextValue = element.value;
|
|
49
|
+
current.current.onChange?.(nextValue);
|
|
50
|
+
if (current.current.value !== undefined) element.value = current.current.value;
|
|
51
|
+
};
|
|
52
|
+
element.addEventListener('change', handleChange);
|
|
53
|
+
return () => element.removeEventListener('change', handleChange);
|
|
54
|
+
}, [ready]);
|
|
55
|
+
|
|
56
|
+
useEffect(() => {
|
|
57
|
+
if (ready && value !== undefined) elementRef.current.value = value;
|
|
58
|
+
}, [ready, value]);
|
|
59
|
+
|
|
60
|
+
useEffect(() => {
|
|
61
|
+
if (ready) elementRef.current.disabled = Boolean(disabled);
|
|
62
|
+
}, [ready, disabled]);
|
|
63
|
+
|
|
64
|
+
if (loadError) throw loadError;
|
|
65
|
+
|
|
66
|
+
return createElement('input-duration', {
|
|
67
|
+
...props,
|
|
68
|
+
ref: setRef,
|
|
69
|
+
'show-days': showDays ? '' : undefined,
|
|
70
|
+
'no-hours': noHours ? '' : undefined,
|
|
71
|
+
'no-minutes': noMinutes ? '' : undefined,
|
|
72
|
+
'no-seconds': noSeconds ? '' : undefined,
|
|
73
|
+
'no-microseconds': noMicroseconds ? '' : undefined,
|
|
74
|
+
'days-divider': daysDivider,
|
|
75
|
+
'hours-divider': hoursDivider,
|
|
76
|
+
'minutes-divider': minutesDivider,
|
|
77
|
+
'seconds-divider': secondsDivider,
|
|
78
|
+
'microseconds-divider': microsecondsDivider,
|
|
79
|
+
disabled: disabled ? '' : undefined,
|
|
80
|
+
required: required ? '' : undefined,
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
export default InputDuration;
|
package/package.json
CHANGED
|
@@ -1,6 +1,46 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stephanluis/react-input-duration",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "React wrapper for the input-duration web component.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./index.js",
|
|
7
|
+
"types": "./index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./index.d.ts",
|
|
11
|
+
"import": "./index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"index.js",
|
|
16
|
+
"index.d.ts",
|
|
17
|
+
"README.md",
|
|
18
|
+
"LICENSE"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"test": "node --test test/*.test.js && tsc --noEmit -p test/tsconfig.json"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"input-duration": "^3.6.0"
|
|
25
|
+
},
|
|
26
|
+
"peerDependencies": {
|
|
27
|
+
"react": "^18.2.0 || ^19.0.0"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@types/react": "^18.3.0",
|
|
31
|
+
"@types/react-dom": "^18.3.0",
|
|
32
|
+
"jsdom": "^26.1.0",
|
|
33
|
+
"react": "^18.2.0",
|
|
34
|
+
"react-dom": "^18.2.0",
|
|
35
|
+
"typescript": "^5.0.0"
|
|
36
|
+
},
|
|
37
|
+
"license": "MIT",
|
|
38
|
+
"repository": {
|
|
39
|
+
"type": "git",
|
|
40
|
+
"url": "git+https://github.com/StephanLuis/input-duration.git",
|
|
41
|
+
"directory": "packages/react-input-duration"
|
|
42
|
+
},
|
|
43
|
+
"publishConfig": {
|
|
44
|
+
"access": "public"
|
|
45
|
+
}
|
|
46
|
+
}
|