idea-react 3.0.0-alpha.1 → 3.0.0-beta.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/ReadMe.md +68 -41
- package/dist/index.d.mts +153 -3
- package/dist/index.d.ts +153 -3
- package/dist/index.js +13 -13
- package/dist/index.mjs +15 -15
- package/package.json +4 -2
- package/source/Card.tsx +8 -0
- package/source/Form/Adorn.tsx +38 -0
- package/source/Form/Checkbox.tsx +73 -0
- package/source/Form/Chips.tsx +51 -0
- package/source/Form/Combobox.tsx +109 -0
- package/source/Form/Control.tsx +59 -0
- package/source/Form/DatePicker.tsx +89 -0
- package/source/Form/Feedback.tsx +29 -0
- package/source/Form/FloatingLabel.tsx +15 -0
- package/source/Form/InputGroup.tsx +23 -0
- package/source/Form/OTP.tsx +48 -0
- package/source/Form/PasswordStrength.tsx +157 -0
- package/source/Form/Primitives.tsx +46 -0
- package/source/Form/Radio.tsx +51 -0
- package/source/Form/Range.tsx +65 -0
- package/source/Form/Switch.tsx +35 -0
- package/source/Form/index.ts +15 -0
- package/source/Navbar.tsx +13 -3
- package/source/Toast.tsx +19 -13
- package/source/index.ts +1 -0
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import classNames from 'classnames';
|
|
2
|
+
import { ComponentPropsWithRef, FC } from 'react';
|
|
3
|
+
|
|
4
|
+
import type { ComponentTheme } from '../Button';
|
|
5
|
+
|
|
6
|
+
export type ChipProps = ComponentPropsWithRef<'button'> & {
|
|
7
|
+
active?: boolean;
|
|
8
|
+
theme?: ComponentTheme;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export const Chip: FC<ChipProps> = ({ className, active, disabled, theme, ...props }) => (
|
|
12
|
+
<button
|
|
13
|
+
{...props}
|
|
14
|
+
className={classNames('chip', active && 'active', theme && `theme-${theme}`, className)}
|
|
15
|
+
type="button"
|
|
16
|
+
aria-pressed={active}
|
|
17
|
+
disabled={disabled}
|
|
18
|
+
/>
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
Chip.displayName = 'Chip';
|
|
22
|
+
|
|
23
|
+
export type ChipInputProps = ComponentPropsWithRef<'div'> & {
|
|
24
|
+
theme?: ComponentTheme;
|
|
25
|
+
separator?: string;
|
|
26
|
+
allowDuplicates?: boolean;
|
|
27
|
+
maxChips?: number;
|
|
28
|
+
createOnBlur?: boolean;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export const ChipInput: FC<ChipInputProps> = ({
|
|
32
|
+
className,
|
|
33
|
+
theme,
|
|
34
|
+
separator,
|
|
35
|
+
allowDuplicates,
|
|
36
|
+
maxChips,
|
|
37
|
+
createOnBlur,
|
|
38
|
+
...props
|
|
39
|
+
}) => (
|
|
40
|
+
<div
|
|
41
|
+
{...props}
|
|
42
|
+
className={classNames('chip-input', theme && `theme-${theme}`, className)}
|
|
43
|
+
data-bs-chips=""
|
|
44
|
+
data-bs-separator={separator}
|
|
45
|
+
data-bs-allow-duplicates={allowDuplicates}
|
|
46
|
+
data-bs-max-chips={maxChips}
|
|
47
|
+
data-bs-create-on-blur={createOnBlur}
|
|
48
|
+
/>
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
ChipInput.displayName = 'ChipInput';
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import classNames from 'classnames';
|
|
4
|
+
import { Children, cloneElement, ComponentPropsWithRef, FC, isValidElement } from 'react';
|
|
5
|
+
|
|
6
|
+
import { Menu } from '../Menu';
|
|
7
|
+
|
|
8
|
+
export interface ComboboxOptionProps extends ComponentPropsWithRef<'button'> {
|
|
9
|
+
value: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export const ComboboxOption: FC<ComboboxOptionProps> = ({ className, value, ...props }) => (
|
|
13
|
+
<button
|
|
14
|
+
{...props}
|
|
15
|
+
type="button"
|
|
16
|
+
className={classNames('menu-item', className)}
|
|
17
|
+
data-bs-value={value}
|
|
18
|
+
/>
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
ComboboxOption.displayName = 'ComboboxOption';
|
|
22
|
+
|
|
23
|
+
export interface ComboboxProps extends Omit<ComponentPropsWithRef<'div'>, 'onChange'> {
|
|
24
|
+
name: string;
|
|
25
|
+
value: string | string[];
|
|
26
|
+
placeholder?: string;
|
|
27
|
+
multiple?: boolean;
|
|
28
|
+
menuClassName?: string;
|
|
29
|
+
disabled?: boolean;
|
|
30
|
+
onChange?: (value: string | string[]) => unknown;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export const Combobox: FC<ComboboxProps> = ({
|
|
34
|
+
className,
|
|
35
|
+
name,
|
|
36
|
+
value,
|
|
37
|
+
placeholder = 'Select an item…',
|
|
38
|
+
disabled,
|
|
39
|
+
multiple,
|
|
40
|
+
onChange,
|
|
41
|
+
menuClassName,
|
|
42
|
+
children,
|
|
43
|
+
...props
|
|
44
|
+
}) => {
|
|
45
|
+
const values = Array.isArray(value) ? value : value ? [value] : [];
|
|
46
|
+
const options = Children.toArray(children).filter(
|
|
47
|
+
(option): option is React.ReactElement<ComboboxOptionProps> =>
|
|
48
|
+
isValidElement<ComboboxOptionProps>(option)
|
|
49
|
+
);
|
|
50
|
+
const selected = options.filter(option => values.includes(option.props.value));
|
|
51
|
+
const hasSelection = selected.length > 0;
|
|
52
|
+
|
|
53
|
+
const handleClick: ComponentPropsWithRef<'div'>['onClick'] = ({ target }) => {
|
|
54
|
+
if (!(target instanceof Element)) return;
|
|
55
|
+
|
|
56
|
+
const option = target.closest<HTMLButtonElement>('[data-bs-value]');
|
|
57
|
+
if (!option || !onChange) return;
|
|
58
|
+
|
|
59
|
+
const next = option.dataset.bsValue || '';
|
|
60
|
+
const value = multiple
|
|
61
|
+
? values.includes(next)
|
|
62
|
+
? values.filter(item => item !== next)
|
|
63
|
+
: [...values, next]
|
|
64
|
+
: next;
|
|
65
|
+
|
|
66
|
+
onChange(value);
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
return (
|
|
70
|
+
<div {...props} className={classNames('combobox', className)}>
|
|
71
|
+
<Menu
|
|
72
|
+
label={
|
|
73
|
+
<span
|
|
74
|
+
className={classNames(
|
|
75
|
+
'combobox-value',
|
|
76
|
+
!hasSelection && 'combobox-placeholder'
|
|
77
|
+
)}
|
|
78
|
+
>
|
|
79
|
+
{hasSelection ? selected.map(option => option.props.children) : placeholder}
|
|
80
|
+
</span>
|
|
81
|
+
}
|
|
82
|
+
toggleProps={{
|
|
83
|
+
className: 'form-control combobox-toggle',
|
|
84
|
+
variant: 'text',
|
|
85
|
+
disabled,
|
|
86
|
+
'aria-haspopup': 'listbox',
|
|
87
|
+
'aria-label': placeholder
|
|
88
|
+
}}
|
|
89
|
+
autoClose={!multiple}
|
|
90
|
+
menuClassName={classNames(menuClassName, 'combobox-menu')}
|
|
91
|
+
onClick={handleClick}
|
|
92
|
+
>
|
|
93
|
+
{options.map(option => {
|
|
94
|
+
const selected = values.includes(option.props.value);
|
|
95
|
+
|
|
96
|
+
return cloneElement(option, {
|
|
97
|
+
className: classNames(option.props.className, selected && 'selected'),
|
|
98
|
+
'aria-selected': selected
|
|
99
|
+
});
|
|
100
|
+
})}
|
|
101
|
+
</Menu>
|
|
102
|
+
{values.map(item => (
|
|
103
|
+
<input key={item} type="hidden" name={name} value={item} />
|
|
104
|
+
))}
|
|
105
|
+
</div>
|
|
106
|
+
);
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
Combobox.displayName = 'Combobox';
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import classNames from 'classnames';
|
|
2
|
+
import { ComponentPropsWithRef, FC } from 'react';
|
|
3
|
+
|
|
4
|
+
const CONTROL_SIZES = {
|
|
5
|
+
sm: 'form-control-sm',
|
|
6
|
+
lg: 'form-control-lg'
|
|
7
|
+
} as const;
|
|
8
|
+
|
|
9
|
+
export type FormControlProps = ComponentPropsWithRef<'input'> & {
|
|
10
|
+
plaintext?: boolean;
|
|
11
|
+
ghost?: boolean;
|
|
12
|
+
caret?: boolean;
|
|
13
|
+
color?: boolean;
|
|
14
|
+
controlSize?: 'sm' | 'lg';
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export const FormControl: FC<FormControlProps> = ({
|
|
18
|
+
className,
|
|
19
|
+
plaintext,
|
|
20
|
+
ghost,
|
|
21
|
+
caret,
|
|
22
|
+
color,
|
|
23
|
+
controlSize,
|
|
24
|
+
...props
|
|
25
|
+
}) => (
|
|
26
|
+
<input
|
|
27
|
+
{...props}
|
|
28
|
+
className={classNames(
|
|
29
|
+
ghost ? 'form-ghost' : plaintext ? 'form-control-plaintext' : 'form-control',
|
|
30
|
+
!ghost && caret && 'form-control-caret',
|
|
31
|
+
!ghost && !plaintext && color && 'form-control-color',
|
|
32
|
+
!ghost && !plaintext && controlSize && CONTROL_SIZES[controlSize],
|
|
33
|
+
className
|
|
34
|
+
)}
|
|
35
|
+
/>
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
FormControl.displayName = 'FormControl';
|
|
39
|
+
|
|
40
|
+
export type FormTextAreaProps = ComponentPropsWithRef<'textarea'>;
|
|
41
|
+
|
|
42
|
+
export const FormTextArea: FC<FormTextAreaProps> = ({ className, ...props }) => (
|
|
43
|
+
<textarea {...props} className={classNames('form-control', className)} />
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
FormTextArea.displayName = 'FormTextArea';
|
|
47
|
+
|
|
48
|
+
export type FormSelectProps = ComponentPropsWithRef<'select'> & {
|
|
49
|
+
controlSize?: 'sm' | 'lg';
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export const FormSelect: FC<FormSelectProps> = ({ className, controlSize, ...props }) => (
|
|
53
|
+
<select
|
|
54
|
+
{...props}
|
|
55
|
+
className={classNames('form-control', controlSize && CONTROL_SIZES[controlSize], className)}
|
|
56
|
+
/>
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
FormSelect.displayName = 'FormSelect';
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import classNames from 'classnames';
|
|
4
|
+
import { Component, ComponentPropsWithoutRef, forwardRef, Ref } from 'react';
|
|
5
|
+
|
|
6
|
+
export type DatePickerProps = Omit<ComponentPropsWithoutRef<'input'>, 'type'> & {
|
|
7
|
+
enhanced?: boolean;
|
|
8
|
+
inline?: boolean;
|
|
9
|
+
selectionMode?: 'single' | 'multiple' | 'multiple-ranged';
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
interface DatePickerComponentProps extends DatePickerProps {
|
|
13
|
+
forwardedRef?: Ref<HTMLInputElement>;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
interface DatepickerInstance {
|
|
17
|
+
dispose(): void;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
class DatePickerComponent extends Component<DatePickerComponentProps> {
|
|
21
|
+
private input?: HTMLInputElement;
|
|
22
|
+
private instance?: DatepickerInstance;
|
|
23
|
+
private mounted = false;
|
|
24
|
+
|
|
25
|
+
componentDidMount() {
|
|
26
|
+
this.mounted = true;
|
|
27
|
+
this.initialize();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
componentWillUnmount() {
|
|
31
|
+
this.mounted = false;
|
|
32
|
+
const { instance } = this;
|
|
33
|
+
this.instance = undefined;
|
|
34
|
+
|
|
35
|
+
queueMicrotask(() => instance?.dispose());
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
private initialize = async () => {
|
|
39
|
+
if (!this.props.enhanced) return;
|
|
40
|
+
|
|
41
|
+
const { default: Datepicker } = await import('bootstrap/js/src/datepicker.js');
|
|
42
|
+
|
|
43
|
+
if (this.mounted && this.input) this.instance = Datepicker.getOrCreateInstance(this.input);
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
private setInputRef = (input: HTMLInputElement | null) => {
|
|
47
|
+
this.input = input || undefined;
|
|
48
|
+
|
|
49
|
+
const { forwardedRef } = this.props;
|
|
50
|
+
|
|
51
|
+
if (typeof forwardedRef === 'function') forwardedRef(input);
|
|
52
|
+
else if (forwardedRef) forwardedRef.current = input;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
render() {
|
|
56
|
+
const {
|
|
57
|
+
className,
|
|
58
|
+
enhanced,
|
|
59
|
+
inline,
|
|
60
|
+
selectionMode,
|
|
61
|
+
min,
|
|
62
|
+
max,
|
|
63
|
+
forwardedRef: _forwardedRef,
|
|
64
|
+
...props
|
|
65
|
+
} = this.props;
|
|
66
|
+
|
|
67
|
+
return (
|
|
68
|
+
<input
|
|
69
|
+
{...props}
|
|
70
|
+
ref={this.setInputRef}
|
|
71
|
+
className={classNames('form-control', className)}
|
|
72
|
+
type={enhanced ? 'text' : 'date'}
|
|
73
|
+
min={min}
|
|
74
|
+
max={max}
|
|
75
|
+
data-bs-toggle={enhanced ? 'datepicker' : undefined}
|
|
76
|
+
data-bs-date-min={enhanced ? min : undefined}
|
|
77
|
+
data-bs-date-max={enhanced ? max : undefined}
|
|
78
|
+
data-bs-inline={enhanced ? inline || undefined : undefined}
|
|
79
|
+
data-bs-selection-mode={enhanced ? selectionMode : undefined}
|
|
80
|
+
/>
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export const DatePicker = forwardRef<HTMLInputElement, DatePickerProps>((props, forwardedRef) => (
|
|
86
|
+
<DatePickerComponent {...props} forwardedRef={forwardedRef} />
|
|
87
|
+
));
|
|
88
|
+
|
|
89
|
+
DatePicker.displayName = 'DatePicker';
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import classNames from 'classnames';
|
|
2
|
+
import { ComponentPropsWithRef, FC } from 'react';
|
|
3
|
+
|
|
4
|
+
export type FormTextProps = ComponentPropsWithRef<'small'>;
|
|
5
|
+
|
|
6
|
+
export const FormText: FC<FormTextProps> = ({ className, ...props }) => (
|
|
7
|
+
<small {...props} className={classNames('form-text', className)} />
|
|
8
|
+
);
|
|
9
|
+
|
|
10
|
+
FormText.displayName = 'FormText';
|
|
11
|
+
|
|
12
|
+
export type FormFeedbackProps = Omit<ComponentPropsWithRef<'div'>, 'type'> & {
|
|
13
|
+
type?: 'valid' | 'invalid';
|
|
14
|
+
tooltip?: boolean;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export const FormFeedback: FC<FormFeedbackProps> = ({
|
|
18
|
+
className,
|
|
19
|
+
type = 'invalid',
|
|
20
|
+
tooltip,
|
|
21
|
+
...props
|
|
22
|
+
}) => (
|
|
23
|
+
<div
|
|
24
|
+
{...props}
|
|
25
|
+
className={classNames(tooltip ? `${type}-tooltip` : `${type}-feedback`, className)}
|
|
26
|
+
/>
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
FormFeedback.displayName = 'FormFeedback';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import classNames from 'classnames';
|
|
2
|
+
import { ComponentPropsWithRef, FC } from 'react';
|
|
3
|
+
|
|
4
|
+
export type FormFloatingLabelProps = ComponentPropsWithRef<'div'> & {
|
|
5
|
+
always?: boolean;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export const FormFloatingLabel: FC<FormFloatingLabelProps> = ({ className, always, ...props }) => (
|
|
9
|
+
<div
|
|
10
|
+
{...props}
|
|
11
|
+
className={classNames('form-floating', always && 'form-floating-always', className)}
|
|
12
|
+
/>
|
|
13
|
+
);
|
|
14
|
+
|
|
15
|
+
FormFloatingLabel.displayName = 'FormFloatingLabel';
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import classNames from 'classnames';
|
|
2
|
+
import { ComponentPropsWithRef, FC } from 'react';
|
|
3
|
+
|
|
4
|
+
export type InputGroupProps = ComponentPropsWithRef<'div'> & {
|
|
5
|
+
size?: 'sm' | 'lg';
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export const InputGroup: FC<InputGroupProps> = ({ className, size, ...props }) => (
|
|
9
|
+
<div
|
|
10
|
+
{...props}
|
|
11
|
+
className={classNames('input-group', size && `input-group-${size}`, className)}
|
|
12
|
+
/>
|
|
13
|
+
);
|
|
14
|
+
|
|
15
|
+
InputGroup.displayName = 'InputGroup';
|
|
16
|
+
|
|
17
|
+
export type InputGroupTextProps = ComponentPropsWithRef<'span'>;
|
|
18
|
+
|
|
19
|
+
export const InputGroupText: FC<InputGroupTextProps> = ({ className, ...props }) => (
|
|
20
|
+
<span {...props} className={classNames('input-group-text', className)} />
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
InputGroupText.displayName = 'InputGroupText';
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import classNames from 'classnames';
|
|
2
|
+
import { ComponentPropsWithRef, FC } from 'react';
|
|
3
|
+
|
|
4
|
+
export type OTPInputProps = Omit<ComponentPropsWithRef<'input'>, 'type' | 'maxLength'> & {
|
|
5
|
+
length: number;
|
|
6
|
+
type?: 'numeric' | 'alphanumeric' | 'alpha';
|
|
7
|
+
mask?: boolean;
|
|
8
|
+
groups?: number[];
|
|
9
|
+
separator?: string;
|
|
10
|
+
connected?: boolean;
|
|
11
|
+
size?: 'sm' | 'lg';
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export const OTPInput: FC<OTPInputProps> = ({
|
|
15
|
+
className,
|
|
16
|
+
length,
|
|
17
|
+
type = 'numeric',
|
|
18
|
+
mask,
|
|
19
|
+
groups,
|
|
20
|
+
separator,
|
|
21
|
+
connected,
|
|
22
|
+
size,
|
|
23
|
+
...props
|
|
24
|
+
}) => (
|
|
25
|
+
<div
|
|
26
|
+
className={classNames('otp', connected && 'otp-connected', size && `otp-${size}`)}
|
|
27
|
+
data-bs-otp=""
|
|
28
|
+
data-bs-length={length}
|
|
29
|
+
data-bs-type={type}
|
|
30
|
+
data-bs-mask={mask || undefined}
|
|
31
|
+
data-bs-groups={groups ? JSON.stringify(groups) : undefined}
|
|
32
|
+
data-bs-separator={separator}
|
|
33
|
+
>
|
|
34
|
+
<input
|
|
35
|
+
{...props}
|
|
36
|
+
type="text"
|
|
37
|
+
maxLength={length}
|
|
38
|
+
autoComplete={props.autoComplete || 'one-time-code'}
|
|
39
|
+
inputMode={type === 'numeric' ? 'numeric' : 'text'}
|
|
40
|
+
pattern={
|
|
41
|
+
type === 'numeric' ? '[0-9]*' : type === 'alpha' ? '[a-zA-Z]*' : '[a-zA-Z0-9]*'
|
|
42
|
+
}
|
|
43
|
+
className={classNames('otp-input', className)}
|
|
44
|
+
/>
|
|
45
|
+
</div>
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
OTPInput.displayName = 'OTPInput';
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import classNames from 'classnames';
|
|
4
|
+
import { Component, ComponentPropsWithoutRef, forwardRef, Ref } from 'react';
|
|
5
|
+
|
|
6
|
+
export type PasswordStrengthLevel = 'weak' | 'fair' | 'good' | 'strong';
|
|
7
|
+
|
|
8
|
+
export interface PasswordStrengthProps extends ComponentPropsWithoutRef<'div'> {
|
|
9
|
+
input?: string | HTMLInputElement | null;
|
|
10
|
+
variant?: 'segments' | 'bar';
|
|
11
|
+
minLength?: number;
|
|
12
|
+
messages?: Partial<Record<PasswordStrengthLevel, string>>;
|
|
13
|
+
weights?: Record<string, number>;
|
|
14
|
+
thresholds?: number[];
|
|
15
|
+
scorer?: (password: string) => number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface PasswordStrengthComponentProps extends PasswordStrengthProps {
|
|
19
|
+
forwardedRef?: Ref<HTMLDivElement>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface StrengthInstance {
|
|
23
|
+
dispose(): void;
|
|
24
|
+
getStrength(): string | null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const levels: PasswordStrengthLevel[] = ['weak', 'fair', 'good', 'strong'];
|
|
28
|
+
|
|
29
|
+
class PasswordStrengthComponent extends Component<PasswordStrengthComponentProps> {
|
|
30
|
+
private meter?: HTMLDivElement;
|
|
31
|
+
private instance?: StrengthInstance;
|
|
32
|
+
private instanceElement?: HTMLDivElement;
|
|
33
|
+
private passwordInput?: HTMLInputElement;
|
|
34
|
+
private mounted = false;
|
|
35
|
+
|
|
36
|
+
componentDidMount() {
|
|
37
|
+
this.mounted = true;
|
|
38
|
+
this.initialize();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
componentDidUpdate(previousProps: PasswordStrengthProps) {
|
|
42
|
+
if (
|
|
43
|
+
previousProps.input !== this.props.input ||
|
|
44
|
+
previousProps.minLength !== this.props.minLength ||
|
|
45
|
+
previousProps.messages !== this.props.messages ||
|
|
46
|
+
previousProps.weights !== this.props.weights ||
|
|
47
|
+
previousProps.thresholds !== this.props.thresholds ||
|
|
48
|
+
previousProps.scorer !== this.props.scorer ||
|
|
49
|
+
previousProps.variant !== this.props.variant
|
|
50
|
+
) {
|
|
51
|
+
this.dispose();
|
|
52
|
+
this.initialize();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
componentWillUnmount() {
|
|
57
|
+
this.mounted = false;
|
|
58
|
+
this.dispose();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
private initialize = async () => {
|
|
62
|
+
const element = this.meter;
|
|
63
|
+
if (!element) return;
|
|
64
|
+
|
|
65
|
+
const { default: Strength } = await import('bootstrap/js/src/strength.js');
|
|
66
|
+
const { input, minLength, messages, weights, thresholds, scorer } = this.props;
|
|
67
|
+
const config = {
|
|
68
|
+
...(input !== undefined && { input }),
|
|
69
|
+
...(minLength !== undefined && { minLength }),
|
|
70
|
+
...(messages !== undefined && {
|
|
71
|
+
messages: { ...Strength.Default.messages, ...messages }
|
|
72
|
+
}),
|
|
73
|
+
...(weights !== undefined && { weights }),
|
|
74
|
+
...(thresholds !== undefined && { thresholds }),
|
|
75
|
+
...(scorer !== undefined && { scorer })
|
|
76
|
+
};
|
|
77
|
+
if (!this.mounted || this.meter !== element) return;
|
|
78
|
+
|
|
79
|
+
this.instanceElement = element;
|
|
80
|
+
this.instance = Strength.getOrCreateInstance(element, config);
|
|
81
|
+
this.passwordInput =
|
|
82
|
+
typeof input === 'string'
|
|
83
|
+
? document.querySelector<HTMLInputElement>(input) || undefined
|
|
84
|
+
: input ||
|
|
85
|
+
element.parentElement?.querySelector<HTMLInputElement>(
|
|
86
|
+
'input[type="password"]'
|
|
87
|
+
) ||
|
|
88
|
+
undefined;
|
|
89
|
+
this.passwordInput?.addEventListener('input', this.handleInput);
|
|
90
|
+
this.passwordInput?.addEventListener('change', this.handleInput);
|
|
91
|
+
this.handleInput();
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
private handleInput = () => {
|
|
95
|
+
const strength = this.instance?.getStrength();
|
|
96
|
+
const levelIndex = strength ? levels.indexOf(strength as PasswordStrengthLevel) + 1 : 0;
|
|
97
|
+
const element = this.instanceElement;
|
|
98
|
+
|
|
99
|
+
element?.setAttribute('aria-valuenow', String(levelIndex));
|
|
100
|
+
element?.setAttribute('aria-valuetext', strength || 'Empty');
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
private dispose = () => {
|
|
104
|
+
this.passwordInput?.removeEventListener('input', this.handleInput);
|
|
105
|
+
this.passwordInput?.removeEventListener('change', this.handleInput);
|
|
106
|
+
this.instance?.dispose();
|
|
107
|
+
this.instance = undefined;
|
|
108
|
+
this.instanceElement = undefined;
|
|
109
|
+
this.passwordInput = undefined;
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
private setMeterRef = (element: HTMLDivElement | null) => {
|
|
113
|
+
this.meter = element || undefined;
|
|
114
|
+
const { forwardedRef } = this.props;
|
|
115
|
+
|
|
116
|
+
if (typeof forwardedRef === 'function') forwardedRef(element);
|
|
117
|
+
else if (forwardedRef) forwardedRef.current = element;
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
render() {
|
|
121
|
+
const {
|
|
122
|
+
className,
|
|
123
|
+
input: _input,
|
|
124
|
+
minLength: _minLength,
|
|
125
|
+
messages: _messages,
|
|
126
|
+
weights: _weights,
|
|
127
|
+
thresholds: _thresholds,
|
|
128
|
+
scorer: _scorer,
|
|
129
|
+
forwardedRef: _forwardedRef,
|
|
130
|
+
variant = 'segments',
|
|
131
|
+
...props
|
|
132
|
+
} = this.props;
|
|
133
|
+
const isBar = variant === 'bar';
|
|
134
|
+
|
|
135
|
+
return (
|
|
136
|
+
<div
|
|
137
|
+
{...props}
|
|
138
|
+
ref={this.setMeterRef}
|
|
139
|
+
className={classNames(isBar ? 'strength-bar' : 'strength', className)}
|
|
140
|
+
role="meter"
|
|
141
|
+
aria-valuemin={0}
|
|
142
|
+
aria-valuemax={levels.length}
|
|
143
|
+
aria-valuenow={0}
|
|
144
|
+
aria-valuetext="Empty"
|
|
145
|
+
>
|
|
146
|
+
{!isBar && levels.map(level => <div key={level} className="strength-segment" />)}
|
|
147
|
+
{!isBar && <span className="strength-text" />}
|
|
148
|
+
</div>
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export const PasswordStrength = forwardRef<HTMLDivElement, PasswordStrengthProps>(
|
|
154
|
+
(props, forwardedRef) => <PasswordStrengthComponent {...props} forwardedRef={forwardedRef} />
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
PasswordStrength.displayName = 'PasswordStrength';
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import classNames from 'classnames';
|
|
2
|
+
import { ComponentPropsWithRef, FC } from 'react';
|
|
3
|
+
|
|
4
|
+
export type FormProps = ComponentPropsWithRef<'form'>;
|
|
5
|
+
|
|
6
|
+
export const Form: FC<FormProps> = props => <form {...props} />;
|
|
7
|
+
|
|
8
|
+
Form.displayName = 'Form';
|
|
9
|
+
|
|
10
|
+
export type FormFieldsetProps = ComponentPropsWithRef<'fieldset'>;
|
|
11
|
+
|
|
12
|
+
export const FormFieldset: FC<FormFieldsetProps> = props => <fieldset {...props} />;
|
|
13
|
+
|
|
14
|
+
FormFieldset.displayName = 'FormFieldset';
|
|
15
|
+
|
|
16
|
+
export type FormFieldProps = ComponentPropsWithRef<'div'>;
|
|
17
|
+
|
|
18
|
+
export const FormField: FC<FormFieldProps> = ({ className, ...props }) => (
|
|
19
|
+
<div {...props} className={classNames('form-field', className)} />
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
FormField.displayName = 'FormField';
|
|
23
|
+
|
|
24
|
+
export type FormGroupProps = ComponentPropsWithRef<'div'>;
|
|
25
|
+
|
|
26
|
+
export const FormGroup: FC<FormGroupProps> = ({ className, ...props }) => (
|
|
27
|
+
<div {...props} className={classNames('form-group', className)} />
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
FormGroup.displayName = 'FormGroup';
|
|
31
|
+
|
|
32
|
+
export type FormFieldContentProps = ComponentPropsWithRef<'div'>;
|
|
33
|
+
|
|
34
|
+
export const FormFieldContent: FC<FormFieldContentProps> = ({ className, ...props }) => (
|
|
35
|
+
<div {...props} className={classNames('form-field-content', className)} />
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
FormFieldContent.displayName = 'FormFieldContent';
|
|
39
|
+
|
|
40
|
+
export type FormLabelProps = ComponentPropsWithRef<'label'>;
|
|
41
|
+
|
|
42
|
+
export const FormLabel: FC<FormLabelProps> = ({ className, ...props }) => (
|
|
43
|
+
<label {...props} className={classNames('form-label', className)} />
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
FormLabel.displayName = 'FormLabel';
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import classNames from 'classnames';
|
|
2
|
+
import { ComponentPropsWithRef, FC } from 'react';
|
|
3
|
+
|
|
4
|
+
import type { ComponentTheme } from '../Button';
|
|
5
|
+
|
|
6
|
+
export type RadioProps = Omit<ComponentPropsWithRef<'input'>, 'type' | 'size'> & {
|
|
7
|
+
size?: 'sm' | 'lg';
|
|
8
|
+
theme?: ComponentTheme;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export const Radio: FC<RadioProps> = ({ className, size, theme, ...props }) => (
|
|
12
|
+
<input
|
|
13
|
+
{...props}
|
|
14
|
+
type="radio"
|
|
15
|
+
className={classNames(
|
|
16
|
+
'radio',
|
|
17
|
+
size && `radio-${size}`,
|
|
18
|
+
theme && `theme-${theme}`,
|
|
19
|
+
className
|
|
20
|
+
)}
|
|
21
|
+
/>
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
Radio.displayName = 'Radio';
|
|
25
|
+
|
|
26
|
+
export type FormCheckProps = Omit<ComponentPropsWithRef<'input'>, 'type' | 'size'> & {
|
|
27
|
+
type?: 'checkbox' | 'radio';
|
|
28
|
+
size?: 'sm' | 'lg';
|
|
29
|
+
theme?: ComponentTheme;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export const FormCheck: FC<FormCheckProps> = ({
|
|
33
|
+
className,
|
|
34
|
+
type = 'checkbox',
|
|
35
|
+
size,
|
|
36
|
+
theme,
|
|
37
|
+
...props
|
|
38
|
+
}) => (
|
|
39
|
+
<input
|
|
40
|
+
{...props}
|
|
41
|
+
type={type}
|
|
42
|
+
className={classNames(
|
|
43
|
+
type === 'checkbox' ? 'check' : 'radio',
|
|
44
|
+
size && `${type === 'checkbox' ? 'check' : 'radio'}-${size}`,
|
|
45
|
+
theme && `theme-${theme}`,
|
|
46
|
+
className
|
|
47
|
+
)}
|
|
48
|
+
/>
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
FormCheck.displayName = 'FormCheck';
|