mirror of
https://github.com/logto-io/logto.git
synced 2025-01-06 20:40:08 -05:00
fix(console): change checkbox to controlled comp (#1235)
This commit is contained in:
parent
a7642cec98
commit
9a72a34cef
2 changed files with 29 additions and 9 deletions
|
@ -1,23 +1,36 @@
|
|||
import { nanoid } from 'nanoid';
|
||||
import React, { forwardRef, InputHTMLAttributes, ReactNode, Ref, useState } from 'react';
|
||||
import React, { ReactNode, useState } from 'react';
|
||||
|
||||
import Icon from './Icon';
|
||||
import * as styles from './index.module.scss';
|
||||
|
||||
type Props = Omit<InputHTMLAttributes<HTMLInputElement>, 'type'> & {
|
||||
type Props = {
|
||||
// eslint-disable-next-line react/boolean-prop-naming
|
||||
value: boolean;
|
||||
onChange: (value: boolean) => void;
|
||||
label?: ReactNode;
|
||||
// eslint-disable-next-line react/boolean-prop-naming
|
||||
disabled: boolean;
|
||||
};
|
||||
|
||||
const Checkbox = ({ label, disabled, ...rest }: Props, ref: Ref<HTMLInputElement>) => {
|
||||
const Checkbox = ({ value, onChange, label, disabled }: Props) => {
|
||||
const [id] = useState(nanoid());
|
||||
|
||||
return (
|
||||
<div className={styles.checkbox}>
|
||||
<input id={id} type="checkbox" disabled={disabled} {...rest} ref={ref} />
|
||||
<input
|
||||
id={id}
|
||||
type="checkbox"
|
||||
checked={value}
|
||||
disabled={disabled}
|
||||
onChange={(event) => {
|
||||
onChange(event.target.checked);
|
||||
}}
|
||||
/>
|
||||
<Icon className={styles.icon} />
|
||||
{label && <label htmlFor={id}>{label}</label>}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default forwardRef<HTMLInputElement, Props>(Checkbox);
|
||||
export default Checkbox;
|
||||
|
|
|
@ -60,10 +60,17 @@ const SignInMethodsForm = () => {
|
|||
|
||||
return (
|
||||
<div key={method} className={styles.method}>
|
||||
<Checkbox
|
||||
label={label}
|
||||
disabled={primaryMethod === method}
|
||||
{...register(`signInMethods.${method}`)}
|
||||
<Controller
|
||||
name={`signInMethods.${method}`}
|
||||
control={control}
|
||||
render={({ field: { value, onChange } }) => (
|
||||
<Checkbox
|
||||
label={label}
|
||||
disabled={primaryMethod === method}
|
||||
value={value}
|
||||
onChange={onChange}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
{enabled && <ConnectorSetupWarning method={method} />}
|
||||
</div>
|
||||
|
|
Loading…
Reference in a new issue