Skip to content

Commit 6324c7d

Browse files
author
billgil
authored
Select HTML5 & InputNew fixes (#1038)
* fix(Select): HTML5 default value and style fixes * fix(Select/InputNew): controlled by props, minor style fixes * fix(InputNew): id should not be optional
1 parent 91d1ccd commit 6324c7d

File tree

6 files changed

+56
-59
lines changed

6 files changed

+56
-59
lines changed

packages/core/src/lib/Input/Input.styles.ts

-6
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,6 @@ const DivWrapperStyled = styled.div<Pick<InputProps, 'state' | 'size'>>`
6969
}
7070
}
7171
72-
:not(&:focus-within) {
73-
&.input_filled {
74-
outline-color: ${color.navy30} !important;
75-
}
76-
}
77-
7872
&:focus-within,
7973
&.input_filled {
8074
outline: 2px solid ${color.navy40};

packages/core/src/lib/InputNew/Input.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -134,21 +134,21 @@ const Input: React.FC<IInputProps> = ({
134134
<InputBase
135135
autoComplete={autoComplete}
136136
autoFocus={autoFocus}
137-
maxLength={validation?.maxLength || props.maxLength}
138-
minLength={validation?.minLength || props.minLength}
139-
defaultValue={value || props.defaultValue}
140137
disabled={disabled || state === 'disabled'}
141138
id={id || 'web3uiKit-input'}
142139
max={validation?.max || props.max}
140+
maxLength={validation?.maxLength || props.maxLength}
143141
min={validation?.min || props.min}
142+
minLength={validation?.minLength || props.minLength}
144143
onBlur={(e) => onBlurEvent(e)}
145144
onChange={(e) => onChangeEvent(e)}
146145
onFocus={(e) => onFocusEvent(e)}
147-
placeholder={placeholder}
148146
pattern={validation?.pattern || props.pattern}
147+
placeholder={placeholder}
149148
required={validation?.required || props.required}
150149
testid="test-input-input"
151150
type={inputType}
151+
value={currentValue}
152152
/>
153153

154154
{slots &&

packages/core/src/lib/InputNew/atoms/InputBase.tsx

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@ const { InputStyled } = styles;
77
const InputBase: FC<IInputBaseProps> = ({
88
autoComplete = true,
99
autoFocus = false,
10-
maxLength,
11-
minLength,
12-
defaultValue,
1310
disabled = false,
1411
id,
15-
name,
1612
max,
13+
maxLength,
1714
min,
15+
minLength,
16+
name,
1817
onBlur,
1918
onChange,
2019
onFocus,
@@ -23,6 +22,7 @@ const InputBase: FC<IInputBaseProps> = ({
2322
required,
2423
testid,
2524
type = 'text',
25+
value,
2626
...props
2727
}) => {
2828
return (
@@ -31,7 +31,6 @@ const InputBase: FC<IInputBaseProps> = ({
3131
autoFocus={autoFocus}
3232
disabled={disabled}
3333
data-testid={testid}
34-
defaultValue={defaultValue}
3534
id={id || 'input-base'}
3635
max={type === 'number' ? max : undefined}
3736
maxLength={maxLength}
@@ -51,6 +50,7 @@ const InputBase: FC<IInputBaseProps> = ({
5150
placeholder={placeholder}
5251
required={required}
5352
type={type}
53+
value={value}
5454
{...props}
5555
/>
5656
);

packages/core/src/lib/InputNew/atoms/types.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,6 @@ export interface IInputBaseProps extends TValidateInput {
5050
*/
5151
autoFocus?: boolean;
5252

53-
/**
54-
* pass a value to be rendered in the input
55-
*/
56-
defaultValue?: string | number;
57-
5853
/**
5954
* disable the input so the user cannot interact with it
6055
*/
@@ -63,7 +58,7 @@ export interface IInputBaseProps extends TValidateInput {
6358
/**
6459
* input needs an ID so it can relate to its label
6560
*/
66-
id?: string;
61+
id: string;
6762

6863
/**
6964
* name text for input accessibility
@@ -99,6 +94,11 @@ export interface IInputBaseProps extends TValidateInput {
9994
* the type of the HTML input: text | number | email | tel | password;
10095
*/
10196
type?: 'text' | 'number' | 'email' | 'tel' | 'password';
97+
98+
/**
99+
* pass a value to be rendered in the input
100+
*/
101+
value?: string | number;
102102
}
103103

104104
export interface ILabelBaseProps {

packages/core/src/lib/Select/Select.stories.tsx

+11-25
Original file line numberDiff line numberDiff line change
@@ -234,55 +234,41 @@ Description.args = {
234234
export const HTML5Select = Template.bind({});
235235
HTML5Select.args = {
236236
label: 'Good old HTML5',
237-
onChangeTraditional: onTestOptionChange,
238237
options: smallOptionsList,
239238
traditionalHTML5: true,
240239
validation: { required: true },
241240
};
242241

243-
export const HTML5ValueBinded = Template.bind({});
244-
HTML5Select.args = {
245-
label: 'Good old HTML5',
246-
onChangeTraditional: onTestOptionChange,
247-
options: smallOptionsList,
248-
traditionalHTML5: true,
249-
validation: { required: true },
250-
value: 'dapp',
251-
};
252-
253-
export const HTML5DescriptionAndPlaceholder = Template.bind({});
254-
HTML5DescriptionAndPlaceholder.args = {
255-
description: 'Much Needed',
242+
export const HTML5SelectValue = Template.bind({});
243+
HTML5SelectValue.args = {
256244
label: 'Good old HTML5',
257-
placeholder: 'Select',
258-
onChangeTraditional: onTestOptionChange,
259-
options: smallOptionsList,
245+
options: testOptionsHTML5,
260246
traditionalHTML5: true,
261-
validation: { required: true },
247+
value: testOptionsHTML5[1]?.label,
262248
};
263249

264250
export const HTML5SelectDefault = Template.bind({});
265251
HTML5SelectDefault.args = {
266252
label: 'Good old HTML5',
267-
onChangeTraditional: onTestOptionChange,
268253
options: testOptionsHTML5,
269254
traditionalHTML5: true,
270255
defaultOptionIndex: 0,
271256
};
272257

273-
export const HTML5SelectValue = Template.bind({});
274-
HTML5SelectValue.args = {
258+
export const HTML5DescriptionAndPlaceholder = Template.bind({});
259+
HTML5DescriptionAndPlaceholder.args = {
260+
description: 'Much Needed',
275261
label: 'Good old HTML5',
276-
onChangeTraditional: onTestOptionChange,
277-
options: testOptionsHTML5,
262+
placeholder: 'Select',
263+
options: smallOptionsList,
278264
traditionalHTML5: true,
279-
value: testOptionsHTML5[1]?.label,
265+
validation: { required: true },
280266
};
281267

282268
export const HTML5SelectDisabled = Template.bind({});
283269
HTML5SelectDisabled.args = {
270+
disabled: true,
284271
label: 'Good old HTML5',
285-
onChangeTraditional: onTestOptionChange,
286272
options: testDisabledOptions,
287273
placeholder: 'Title 1',
288274
traditionalHTML5: true,

packages/core/src/lib/Select/TraditionalSelect/TraditionalSelect.tsx

+30-13
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const TraditionalSelect: React.FC<ISelectProps> = ({
1111
description,
1212
disabled = false,
1313
errorMessage = '',
14-
id,
14+
id = 'web3uikit-select',
1515
label,
1616
onBlurTraditional,
1717
onChange,
@@ -30,48 +30,65 @@ const TraditionalSelect: React.FC<ISelectProps> = ({
3030
width = '200px',
3131
...props
3232
}: ISelectProps) => {
33-
const defaultValue =
34-
(defaultOptionIndex !== undefined &&
35-
options[defaultOptionIndex]?.label) ||
36-
value ||
37-
placeholder ||
38-
'Please choose';
33+
const [currentValue, setCurrentValue] = useState<
34+
string | number | string[]
35+
>();
36+
37+
useEffect(() => {
38+
if (!value) return;
39+
setCurrentValue(value);
40+
}, [value]);
41+
42+
useEffect(() => {
43+
if (Number(defaultOptionIndex) < 0) return;
44+
if (Number(defaultOptionIndex) > options.length) return;
45+
46+
setCurrentValue(options[Number(defaultOptionIndex)]?.label || '');
47+
}, [defaultOptionIndex]);
48+
49+
const handleChange = (event: React.ChangeEvent<HTMLSelectElement>) => {
50+
setCurrentValue(event.currentTarget.value);
51+
onChangeTraditional && onChangeTraditional(event);
52+
};
53+
3954
return (
4055
<DivWrapperStyled
41-
className="input_filled"
56+
className="input_filled select"
4257
data-testid="test-select"
58+
state={state}
4359
style={{ ...style, width }}
4460
{...props}
4561
>
4662
<SelectStyled
4763
data-testid="test-select-select"
48-
defaultValue={defaultValue}
64+
disabled={disabled}
4965
id={id}
5066
ref={refTraditional}
5167
onChange={(event: React.ChangeEvent<HTMLSelectElement>) =>
52-
onChangeTraditional && onChangeTraditional(event)
68+
handleChange(event)
5369
}
5470
onBlur={(event: React.FocusEvent<HTMLSelectElement>) =>
5571
onBlurTraditional && onBlurTraditional(event)
5672
}
5773
required={validation?.required}
58-
value={value}
74+
value={currentValue || placeholder || 'Please choose'}
5975
>
6076
<option disabled>{placeholder || 'Please choose'}</option>
6177

6278
{options.map((option, i) => (
6379
<option
6480
data-testid={`test-select-option-${i}`}
81+
disabled={option.disabled || false}
6582
id={String(option?.id)}
6683
key={option?.id}
67-
value={option?.id}
68-
disabled={option.disabled || false}
84+
value={option?.label}
6985
>
7086
{option?.prefix}
7187
{option?.label}
7288
</option>
7389
))}
7490
</SelectStyled>
91+
7592
{label && (
7693
<LabelStyledTrad
7794
data-testid="test-select-label"

0 commit comments

Comments
 (0)