Skip to content

Commit

Permalink
Change: Extract ErrorMarker from useFormValidation hook module
Browse files Browse the repository at this point in the history
Hooks should not contain JSX code and should be plain JS only. Therefore
extract the ErrorMarker component from the useFormValidation module and
use `js` file suffix for the module.
  • Loading branch information
bjoernricks committed Jun 18, 2024
1 parent 564309d commit 01a0cbd
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 35 deletions.
29 changes: 29 additions & 0 deletions src/web/components/form/ErrorMarker.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* SPDX-FileCopyrightText: 2024 Greenbone AG
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import React from 'react';

import styled from 'styled-components';

import {styledExcludeProps} from 'web/utils/styledConfig';
import Theme from 'web/utils/theme';

const StyledMarker = styledExcludeProps(styled.div, ['isVisible'])`
color: ${Theme.darkRed};
color: ${props => props.color};
font-weight: bold;
font-size: 19px;
padding-bottom: 1px;
padding-left: 4px;
display: ${props => (props.isVisible ? 'inline' : 'none')};
`;

const ErrorMarker = props => (
<StyledMarker {...props} data-testid="error-marker">
×
</StyledMarker>
);

export default ErrorMarker;
5 changes: 2 additions & 3 deletions src/web/components/form/multiselect.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/


import React from 'react';

import styled from 'styled-components';
Expand Down Expand Up @@ -33,7 +32,7 @@ import {
SelectedValue,
} from './selectelements';

import {Marker} from './useFormValidation';
import ErrorMarker from './ErrorMarker';

const DEFAULT_WIDTH = '250px';

Expand Down Expand Up @@ -278,7 +277,7 @@ class MultiSelect extends React.Component {
</Menu>
)}
</SelectContainer>
<Marker isVisible={hasError}>×</Marker>
<ErrorMarker isVisible={hasError}>×</ErrorMarker>
</Div>
);
}}
Expand Down
5 changes: 2 additions & 3 deletions src/web/components/form/select.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/


import React from 'react';

import Downshift from 'downshift';
Expand Down Expand Up @@ -31,7 +30,7 @@ import {
SelectedValue,
} from './selectelements';

import {Marker} from './useFormValidation';
import ErrorMarker from './ErrorMarker';
import {styledExcludeProps} from 'web/utils/styledConfig';

const SingleSelectedValue = styled(SelectedValue)`
Expand Down Expand Up @@ -279,7 +278,7 @@ class Select extends React.Component {
</Menu>
)}
</SelectContainer>
<Marker isVisible={hasError}>×</Marker>
<ErrorMarker isVisible={hasError}>×</ErrorMarker>
</Div>
);
}}
Expand Down
5 changes: 2 additions & 3 deletions src/web/components/form/textarea.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/


import React, {useCallback} from 'react';
import {isDefined} from 'gmp/utils/identity';
import PropTypes from 'web/utils/proptypes';
Expand All @@ -15,7 +14,7 @@ import Theme from 'web/utils/theme';
import withLayout from 'web/components/layout/withLayout';

import {DISABLED_OPACITY} from './field';
import {Marker} from './useFormValidation';
import ErrorMarker from './ErrorMarker';
import {styledExcludeProps} from 'web/utils/styledConfig';

const StyledTextArea = styledExcludeProps(styled.textarea, [
Expand Down Expand Up @@ -80,7 +79,7 @@ const TextArea = ({
value={value}
onChange={handleChange}
/>
<Marker isVisible={hasError}>×</Marker>
<ErrorMarker isVisible={hasError}>×</ErrorMarker>
</React.Fragment>
);
};
Expand Down
5 changes: 2 additions & 3 deletions src/web/components/form/textfield.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/


import React from 'react';

import PropTypes from 'web/utils/proptypes';

import Field from './field';
import {Marker} from './useFormValidation';
import ErrorMarker from './ErrorMarker';

const TextField = ({hasError = false, errorContent, title, ...props}) => {
return (
Expand All @@ -20,7 +19,7 @@ const TextField = ({hasError = false, errorContent, title, ...props}) => {
title={hasError ? `${errorContent}` : title}
type="text"
/>
<Marker isVisible={hasError}>×</Marker>
<ErrorMarker isVisible={hasError}>×</ErrorMarker>
</React.Fragment>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,12 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/


import React, {useState, useEffect, useCallback} from 'react';

import styled from 'styled-components';
import {useState, useEffect, useCallback} from 'react';

import {_, _l} from 'gmp/locale/lang';

import {hasValue, isDefined, isFunction} from 'gmp/utils/identity';

import {styledExcludeProps} from 'web/utils/styledConfig';
import Theme from 'web/utils/theme';

const StyledMarker = styledExcludeProps(styled.div, ['isVisible'])`
color: ${Theme.darkRed};
color: ${props => props.color};
font-weight: bold;
font-size: 19px;
padding-bottom: 1px;
padding-left: 4px;
display: ${props => (props.isVisible ? 'inline' : 'none')};
`;

export const Marker = props => (
<StyledMarker {...props} data-testid="error-marker">
×
</StyledMarker>
);

const useFormValidation = (
validationRules,
values,
Expand Down

0 comments on commit 01a0cbd

Please sign in to comment.