Skip to content

Commit baf35be

Browse files
committed
Update fields to have a custom component
1 parent e8755ac commit baf35be

File tree

8 files changed

+87
-10
lines changed

8 files changed

+87
-10
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"@types/react-router": "^5.1.3",
6868
"@types/react-router-dom": "^5.1.3",
6969
"@types/react-transition-group": "^2.9.2",
70-
"@types/styled-components": "^4.4.0",
70+
"@types/styled-components": "^5.1.0",
7171
"@types/uuid": "^3.4.5",
7272
"@types/webpack-env": "^1.15.2",
7373
"@types/yup": "^0.29.3",

resources/scripts/.eslintrc.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ rules:
4848
"@typescript-eslint/no-unused-vars": 0
4949
"@typescript-eslint/no-explicit-any": 0
5050
"@typescript-eslint/no-non-null-assertion": 0
51+
"@typescript-eslint/ban-ts-comment": 0
5152
# @todo this would be nice to have, but don't want to deal with the warning spam at the moment.
5253
"@typescript-eslint/explicit-module-boundary-types": 0
5354
no-restricted-imports:

resources/scripts/components/dashboard/forms/UpdatePasswordForm.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export default () => {
3636
clearFlashes('account:password');
3737
updateAccountPassword({ ...values })
3838
.then(() => {
39+
// @ts-ignore
3940
window.location = '/auth/login';
4041
})
4142
.catch(error => addFlash({

resources/scripts/components/elements/Field.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import { Field as FormikField, FieldProps } from 'formik';
33
import classNames from 'classnames';
4+
import Input from '@/components/elements/Input';
45

56
interface OwnProps {
67
name: string;
@@ -20,13 +21,12 @@ const Field = ({ id, name, light = false, label, description, validate, classNam
2021
{label &&
2122
<label htmlFor={id} className={light ? undefined : 'input-dark-label'}>{label}</label>
2223
}
23-
<input
24+
<Input
2425
id={id}
2526
{...field}
2627
{...props}
27-
className={classNames((className || (light ? 'input' : 'input-dark')), {
28-
error: touched[field.name] && errors[field.name],
29-
})}
28+
isLight={light}
29+
hasError={!!(touched[field.name] && errors[field.name])}
3030
/>
3131
{touched[field.name] && errors[field.name] ?
3232
<p className={'input-help error'}>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import React from 'react';
2+
import styled, { css } from 'styled-components/macro';
3+
import tw from 'twin.macro';
4+
5+
export interface Props {
6+
isLight?: boolean;
7+
hasError?: boolean;
8+
}
9+
10+
const light = css<Props>`
11+
${tw`bg-white border-neutral-200 text-neutral-800`};
12+
&:focus { ${tw`border-primary-400`} }
13+
14+
&:disabled {
15+
${tw`bg-neutral-100 border-neutral-200`};
16+
}
17+
`;
18+
19+
const Input = styled.input<Props>`
20+
// Reset to normal styling.
21+
${tw`appearance-none w-full min-w-0`};
22+
${tw`p-3 border rounded text-sm transition-all duration-150`};
23+
${tw`bg-neutral-600 border-neutral-500 hover:border-neutral-400 text-neutral-200 shadow-none`};
24+
25+
${props => props.hasError && tw`text-red-600 border-red-500 hover:border-red-600`};
26+
& + .input-help {
27+
${tw`mt-1 text-xs`};
28+
${props => props.hasError ? tw`text-red-400` : tw`text-neutral-400`};
29+
}
30+
31+
&:required, &:invalid {
32+
${tw`shadow-none`};
33+
}
34+
35+
&:focus {
36+
${tw`shadow-md border-neutral-400`};
37+
}
38+
39+
&:disabled {
40+
${tw`opacity-75`};
41+
}
42+
43+
${props => props.isLight && light};
44+
`;
45+
46+
export default Input;

resources/scripts/macros.d.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
11
// This allows the use of css={} on JSX elements.
22
//
33
// @see https://github.com/DefinitelyTyped/DefinitelyTyped/issues/31245
4-
import {} from 'styled-components/cssprop';
4+
//
5+
// This is just the contents of the @types/styled-components/cssprop.d.ts file
6+
// since using the other method of just importing the one file did not work
7+
// correctly for some reason.
8+
// noinspection ES6UnusedImports
9+
import {} from 'react';
10+
// eslint-disable-next-line no-restricted-imports
11+
import { CSSProp } from 'styled-components';
12+
13+
declare module 'react' {
14+
interface Attributes {
15+
// NOTE: unlike the plain javascript version, it is not possible to get access
16+
// to the element's own attributes inside function interpolations.
17+
// Only theme will be accessible, and only with the DefaultTheme due to the global
18+
// nature of this declaration.
19+
// If you are writing this inline you already have access to all the attributes anyway,
20+
// no need for the extra indirection.
21+
/**
22+
* If present, this React element will be converted by
23+
* `babel-plugin-styled-components` into a styled component
24+
* with the given css as its styles.
25+
*/
26+
css?: CSSProp;
27+
}
28+
}

tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,8 @@
2626
},
2727
"include": [
2828
"./resources/scripts/**/*"
29+
],
30+
"exclude": [
31+
"/node_modules/"
2932
]
3033
}

yarn.lock

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -971,7 +971,7 @@
971971
version "4.7.2"
972972
resolved "https://registry.yarnpkg.com/@types/history/-/history-4.7.2.tgz#0e670ea254d559241b6eeb3894f8754991e73220"
973973

974-
"@types/hoist-non-react-statics@^3.3.0":
974+
"@types/hoist-non-react-statics@*", "@types/hoist-non-react-statics@^3.3.0":
975975
version "3.3.1"
976976
resolved "https://registry.yarnpkg.com/@types/hoist-non-react-statics/-/hoist-non-react-statics-3.3.1.tgz#1124aafe5118cb591977aeb1ceaaed1070eb039f"
977977
dependencies:
@@ -1090,10 +1090,12 @@
10901090
"@types/prop-types" "*"
10911091
csstype "^2.2.0"
10921092

1093-
"@types/styled-components@^4.4.0":
1094-
version "4.4.0"
1095-
resolved "https://registry.yarnpkg.com/@types/styled-components/-/styled-components-4.4.0.tgz#15a3d59533fd3a5bd013db4a7c4422ec542c59d2"
1093+
"@types/styled-components@^5.1.0":
1094+
version "5.1.0"
1095+
resolved "https://registry.yarnpkg.com/@types/styled-components/-/styled-components-5.1.0.tgz#24d3412ba5395aa06e14fbc93c52f9454cebd0d6"
1096+
integrity sha512-ZFlLCuwF5r+4Vb7JUmd+Yr2S0UBdBGmI7ctFTgJMypIp3xOHI4LCFVn2dKMvpk6xDB2hLRykrEWMBwJEpUAUIQ==
10961097
dependencies:
1098+
"@types/hoist-non-react-statics" "*"
10971099
"@types/react" "*"
10981100
"@types/react-native" "*"
10991101
csstype "^2.2.0"

0 commit comments

Comments
 (0)