diff --git a/packages/grafana-ui/src/components/Forms/Form.mdx b/packages/grafana-ui/src/components/Forms/Form.mdx index 827a8daa04d..fdfcb66dec3 100644 --- a/packages/grafana-ui/src/components/Forms/Form.mdx +++ b/packages/grafana-ui/src/components/Forms/Form.mdx @@ -29,8 +29,8 @@ const defaultUser: Partial = { >{({register, errors}) => { return ( - - + + ) @@ -46,13 +46,13 @@ const defaultUser: Partial = { `register` allows to register form elements(inputs, selects, radios, etc) in the form. In order to do that you need to pass `register` as a `ref` property to the form input. For example: ```jsx - + ``` Register accepts an object which describes validation rules for a given input: ```jsx - - + ``` @@ -179,7 +179,7 @@ const defaultValues: FormDto {
{ ({register}) => ( <> - + )} @@ -196,7 +196,7 @@ Validation can be performed either synchronously or asynchronously. What's impor ({register, errors}) => ( <> ( <> { ({register, errors}) => ( <> void; -} - -export const PasswordInput = forwardRef((props, ref) => ( - <> - {props.label} - ) => props.onChange(event.target.value)} - value={props.value} - /> - -)); diff --git a/public/app/features/profile/ChangePasswordForm.tsx b/public/app/features/profile/ChangePasswordForm.tsx index 0aea8b14936..31ebd512797 100644 --- a/public/app/features/profile/ChangePasswordForm.tsx +++ b/public/app/features/profile/ChangePasswordForm.tsx @@ -1,75 +1,70 @@ -import React, { PureComponent, MouseEvent } from 'react'; +import React, { FC } from 'react'; import config from 'app/core/config'; -import { Button, LinkButton } from '@grafana/ui'; +import { Button, LinkButton, Form, Field, Input, HorizontalGroup } from '@grafana/ui'; import { ChangePasswordFields } from 'app/core/utils/UserProvider'; -import { PasswordInput } from 'app/core/components/PasswordInput/PasswordInput'; +import { css } from 'emotion'; export interface Props { isSaving: boolean; onChangePassword: (payload: ChangePasswordFields) => void; } -export interface State { - oldPassword: string; - newPassword: string; - confirmNew: string; -} +export const ChangePasswordForm: FC = ({ onChangePassword, isSaving }) => { + const { ldapEnabled, authProxyEnabled } = config; -export class ChangePasswordForm extends PureComponent { - state: State = { - oldPassword: '', - newPassword: '', - confirmNew: '', - }; - - onOldPasswordChange = (oldPassword: string) => { - this.setState({ oldPassword }); - }; - - onNewPasswordChange = (newPassword: string) => { - this.setState({ newPassword }); - }; - - onConfirmPasswordChange = (confirmNew: string) => { - this.setState({ confirmNew }); - }; - - onSubmitChangePassword = (event: MouseEvent) => { - event.preventDefault(); - this.props.onChangePassword({ ...this.state }); - }; - - render() { - const { oldPassword, newPassword, confirmNew } = this.state; - const { isSaving } = this.props; - const { ldapEnabled, authProxyEnabled } = config; - - if (ldapEnabled || authProxyEnabled) { - return

You cannot change password when ldap or auth proxy authentication is enabled.

; - } - - return ( -
-
- -
-
- -
-
- -
-
- - - Cancel - -
-
- ); + if (ldapEnabled || authProxyEnabled) { + return

You cannot change password when ldap or auth proxy authentication is enabled.

; } -} + return ( +
+
+ {({ register, errors, getValues }) => { + return ( + <> + + + -export default ChangePasswordForm; + + v === getValues().confirmNew || 'Passwords must match', + old: v => v !== getValues().oldPassword || `New password can't be the same as the old one.`, + }, + })} + /> + + + + v === getValues().newPassword || 'Passwords must match', + })} + /> + + + + + Cancel + + + + ); + }} +
+
+ ); +};