Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Geosuggest): fix input id and label for #491

Merged
merged 9 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Geosuggest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,9 @@ export default class GeoSuggest extends React.Component<Props, State> {
);

return (
<div className={classes} id={this.props.id}>
<div
className={classes}
id={`geosuggest__wrapper${this.props.id ? `--${this.props.id}` : ''}`}>
<div className="geosuggest__input-wrapper">{input}</div>
<div className="geosuggest__suggests-wrapper">{suggestionsList}</div>
</div>
Expand Down
7 changes: 5 additions & 2 deletions src/input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,16 @@ export default class Input extends React.PureComponent<Props, unknown> {
return (
<>
{shouldRenderLabel && (
<label className="geosuggest__label" htmlFor={this.props.id}>
<label
className="geosuggest__label"
htmlFor={this.props.id ? this.props.id : 'geosuggest__input'}
>
{this.props.label}
</label>
)}
<input
className={classes}
id={`geosuggest__input${this.props.id ? `--${this.props.id}` : ''}`}
id={this.props.id ? this.props.id : 'geosuggest__input'}
joaogarin marked this conversation as resolved.
Show resolved Hide resolved
ref={(i): HTMLInputElement | null => (this.input = i)}
type={this.props.inputType}
{...attributes}
Expand Down
19 changes: 19 additions & 0 deletions test/Geosuggest_spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,25 @@ describe('Component: Geosuggest', () => {

expect(label).to.not.be.null;
});

it('should render a <label> pointing to the input if `label` and `id` props were supplied', () => {
const props = {
id: 'geosuggest-id',
label: 'some label'
};

renderGeosuggest(props);

const label = component.container.querySelector(
'label'
) as HTMLLabelElement;

const input = component.container.querySelector(
'input'
) as HTMLInputElement;

expect(label).to.be.equal(input);
joaogarin marked this conversation as resolved.
Show resolved Hide resolved
});
});

describe('without label and id props', () => {
Expand Down