Skip to content

Commit

Permalink
feat: add htmlFor in select component
Browse files Browse the repository at this point in the history
  • Loading branch information
AllanOliveiraM committed Oct 31, 2022
1 parent 414cb40 commit b49125e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"react",
"typescript"
],
"version": "2.1.2",
"version": "2.1.3",
"main": "./dist/index.cjs.js",
"module": "./dist/index.esm.js",
"types": "./dist/index.d.ts",
Expand Down
12 changes: 9 additions & 3 deletions src/components/molecules/Fields/AsyncSelect/AsyncSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,13 @@ const AsyncSelect = <FormType extends FieldValues>(props: AsyncSelectProps<FormT
name,
required,
placeholder,
id,
...rest
} = props

const resolvedId = id || slugify(`select-container-${label}`)
const resolvedInputId = `select-input-${id}` || slugify(`select-input-${label}`)

const [isFocused, setIsFocused] = useState<boolean>(false)
const [options, setOptions] = useState<AsyncSelectOption[]>(defaultOptions || [])

Expand Down Expand Up @@ -108,7 +112,7 @@ const AsyncSelect = <FormType extends FieldValues>(props: AsyncSelectProps<FormT
mb='0.4rem'
isFocused={isFocused}
error={error}
htmlFor={`field-${name}`}
htmlFor={resolvedId}
>
{label}
</FieldLabel>
Expand All @@ -120,7 +124,8 @@ const AsyncSelect = <FormType extends FieldValues>(props: AsyncSelectProps<FormT
if (isMulti) {
return (
<ReactSelectAsync<AsyncSelectOption, true>
id={slugify(`select-${label}`)}
id={resolvedId}
inputId={resolvedInputId}
styles={customSelectStyles}
loadOptions={loadOptionsHandler}
placeholder={placeholder === undefined ? label : placeholder}
Expand Down Expand Up @@ -152,7 +157,8 @@ const AsyncSelect = <FormType extends FieldValues>(props: AsyncSelectProps<FormT

return (
<ReactSelectAsync
id={slugify(`select-${label}`)}
id={resolvedId}
inputId={resolvedInputId}
styles={customSelectStyles}
loadOptions={loadOptionsHandler}
placeholder={placeholder === undefined ? label : placeholder}
Expand Down
16 changes: 11 additions & 5 deletions src/components/molecules/Fields/Select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,13 @@ const Select = <FormType extends FieldValues>(props: SelectProps<FormType>) => {
name,
required,
placeholder,
id,
...rest
} = props

const resolvedId = id || slugify(`select-container-${label}`)
const resolvedInputId = `select-input-${id}` || slugify(`select-input-${label}`)

const [isFocused, setIsFocused] = useState<boolean>(false)

// TODO: Aprimorar a estilização a partir do tema
Expand Down Expand Up @@ -81,7 +85,7 @@ const Select = <FormType extends FieldValues>(props: SelectProps<FormType>) => {
mb='0.4rem'
isFocused={isFocused}
error={error}
htmlFor={`field-${name}`}
htmlFor={resolvedId}
>
{label}
</FieldLabel>
Expand All @@ -93,7 +97,8 @@ const Select = <FormType extends FieldValues>(props: SelectProps<FormType>) => {
if (isMulti) {
return (
<ReactSelect<SelectOption, true>
id={slugify(`select-${label}`)}
id={resolvedId}
inputId={resolvedInputId}
styles={customSelectStyles}
options={options}
placeholder={placeholder === undefined ? label : placeholder}
Expand All @@ -111,7 +116,7 @@ const Select = <FormType extends FieldValues>(props: SelectProps<FormType>) => {
isClearable={isClearable}
aria-label={label}
isDisabled={disabled}
data-cy={slugify(`select-${label}`)}
data-cy={resolvedId}
isSearchable={Boolean(enableSearch)}
noOptionsMessage={() => enableSearch?.noOptionsMessage}
/>
Expand All @@ -120,7 +125,8 @@ const Select = <FormType extends FieldValues>(props: SelectProps<FormType>) => {

return (
<ReactSelect
id={slugify(`select-${label}`)}
id={resolvedId}
inputId={resolvedInputId}
styles={customSelectStyles}
options={options}
placeholder={placeholder === undefined ? label : placeholder}
Expand All @@ -137,7 +143,7 @@ const Select = <FormType extends FieldValues>(props: SelectProps<FormType>) => {
isClearable={isClearable}
aria-label={label}
isDisabled={disabled}
data-cy={slugify(`select-${label}`)}
data-cy={resolvedId}
isSearchable={Boolean(enableSearch)}
noOptionsMessage={() => enableSearch?.noOptionsMessage}
/>
Expand Down

0 comments on commit b49125e

Please sign in to comment.