Skip to content

Commit a791422

Browse files
committed
feat(input): add validation styling to select
fix HospitalRun#271
1 parent 53e4a55 commit a791422

File tree

3 files changed

+47
-46
lines changed

3 files changed

+47
-46
lines changed

.vscode/settings.json

+3-13
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11
{
22
"typescript.tsdk": "node_modules/typescript/lib",
3-
"eslint.autoFixOnSave": true,
4-
"eslint.validate": [
5-
"javascript",
6-
"javascriptreact",
7-
{
8-
"autoFix": true,
9-
"language": "typescript"
10-
},
11-
{
12-
"autoFix": true,
13-
"language": "typescriptreact"
14-
}
15-
]
3+
"editor.codeActionsOnSave": {
4+
"source.fixAll.eslint": true
5+
}
166
}

src/components/Select/Select.tsx

+22-9
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,13 @@ interface Props {
3333
invalidInputMessage?: string
3434
/** Defines the message for valid input. */
3535
validInputMessage?: string
36+
/** Defines the default value */
37+
defaultValue?: string | Array<string>
3638
}
37-
/**
38-
* Defines the default style of the select.
39-
*/
40-
const defaultStyles = { backgroundPosition: 'right calc(.375em + .55rem) center' }
4139

4240
const Select = (props: Props) => {
4341
const {
42+
defaultValue,
4443
value,
4544
multiple,
4645
isValid,
@@ -54,30 +53,44 @@ const Select = (props: Props) => {
5453
className,
5554
style,
5655
} = props
57-
56+
/**
57+
* Defines the default style of the select.
58+
*/
59+
const selectDefaultStyles = {
60+
backgroundPosition: 'right calc(.375em + .55rem) center',
61+
borderColor: isValid ? 'green' : isInvalid ? 'red' : 'black',
62+
}
63+
const validInputTextDefaultStyles = {
64+
color: 'green',
65+
}
5866
return (
59-
<div>
67+
<Form.Group>
6068
<Form.Control
6169
as="select"
6270
value={value}
71+
defaultValue={defaultValue}
6372
multiple={multiple}
6473
isInvalid={isInvalid}
6574
isValid={isValid}
6675
disabled={disabled}
6776
size={getControlSize(size)}
6877
onChange={onChange}
6978
className={className}
70-
style={Object.assign(style || {}, defaultStyles)}
79+
style={Object.assign({}, style, ...[selectDefaultStyles])}
7180
>
7281
{children}
7382
</Form.Control>
74-
<Form.Control.Feedback className="text-left ml-3 mt-1" type="valid">
83+
<Form.Control.Feedback
84+
style={validInputTextDefaultStyles}
85+
className="text-left ml-3 mt-1"
86+
type="valid"
87+
>
7588
{validInputMessage}
7689
</Form.Control.Feedback>
7790
<Form.Control.Feedback className="text-left ml-3 mt-1" type="invalid">
7891
{invalidInputMessage}
7992
</Form.Control.Feedback>
80-
</div>
93+
</Form.Group>
8194
)
8295
}
8396

stories/select.stories.tsx

+22-24
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ storiesOf('Select', module)
1313
))
1414
.add('Select', () => (
1515
<div>
16-
<Select>
17-
<option selected>Choose your sweet</option>
16+
<Select defaultValue="Marshmallow">
17+
<option>Choose your sweet</option>
1818
<option>Marshmallow</option>
1919
<option>Nougat</option>
2020
<option>Ice cream</option>
@@ -24,20 +24,18 @@ storiesOf('Select', module)
2424
))
2525
.add('Select multiple', () => (
2626
<div>
27-
<Select multiple>
27+
<Select defaultValue={['Nougat', 'Ice cream']} multiple>
2828
<option>Marshmallow</option>
29-
<option selected>Nougat</option>
29+
<option>Nougat</option>
3030
<option>Ice cream</option>
31-
<option selected>Gingerbread</option>
31+
<option>Gingerbread</option>
3232
</Select>
3333
</div>
3434
))
3535
.add('Disabled select', () => (
3636
<div>
3737
<Select disabled>
38-
<option selected disabled>
39-
Choose your sweet
40-
</option>
38+
<option disabled>Choose your sweet</option>
4139
<option>Marshmallow</option>
4240
<option>Nougat</option>
4341
<option>Ice cream</option>
@@ -47,10 +45,8 @@ storiesOf('Select', module)
4745
))
4846
.add('Valid select', () => (
4947
<div>
50-
<Select isValid>
51-
<option selected disabled>
52-
Choose your sweet
53-
</option>
48+
<Select defaultValue="Choose your sweet" isValid>
49+
<option disabled>Choose your sweet</option>
5450
<option>Marshmallow</option>
5551
<option>Nougat</option>
5652
<option>Ice cream</option>
@@ -60,10 +56,12 @@ storiesOf('Select', module)
6056
))
6157
.add('Valid select with custom message', () => (
6258
<div>
63-
<Select isValid validInputMessage="This is a valid select input message">
64-
<option selected disabled>
65-
Choose your sweet
66-
</option>
59+
<Select
60+
defaultValue="Choose your sweet"
61+
isValid
62+
validInputMessage="This is a valid select input message"
63+
>
64+
<option disabled>Choose your sweet</option>
6765
<option>Marshmallow</option>
6866
<option>Nougat</option>
6967
<option>Ice cream</option>
@@ -73,10 +71,8 @@ storiesOf('Select', module)
7371
))
7472
.add('Invalid select', () => (
7573
<div>
76-
<Select isInvalid>
77-
<option selected disabled>
78-
Choose your sweet
79-
</option>
74+
<Select defaultValue="Choose your sweet" isInvalid>
75+
<option disabled>Choose your sweet</option>
8076
<option>Marshmallow</option>
8177
<option>Nougat</option>
8278
<option>Ice cream</option>
@@ -86,10 +82,12 @@ storiesOf('Select', module)
8682
))
8783
.add('Invalid select with custom message', () => (
8884
<div>
89-
<Select isInvalid invalidInputMessage="This is an invalid select input error message">
90-
<option selected disabled>
91-
Choose your sweet
92-
</option>
85+
<Select
86+
defaultValue="Choose your sweet"
87+
isInvalid
88+
invalidInputMessage="This is an invalid select input error message"
89+
>
90+
<option disabled>Choose your sweet</option>
9391
<option>Marshmallow</option>
9492
<option>Nougat</option>
9593
<option>Ice cream</option>

0 commit comments

Comments
 (0)