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

Convert AddressForm From Angular to React #969

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
53 changes: 53 additions & 0 deletions src/common/components/addressForm/countrySelect/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import React, { ChangeEvent, FocusEvent } from 'react';
import SelectInput, { Option } from '../../form/selectInput';

interface CountrySelectProps {
addressDisabled: boolean,
countries: Option[],
onChange: (event: ChangeEvent<HTMLSelectElement>) => void,
onBlur: (event: FocusEvent<HTMLSelectElement>) => void,
onSelectCountry: (country?: string, initial?: boolean) => void,
refreshCountries: () => void,
value: string,
error?: string,
canRetry: boolean,
}

const CountrySelect = ({
addressDisabled,
countries,
onChange,
onBlur,
onSelectCountry,
refreshCountries,
value,
error,
canRetry,
}: CountrySelectProps) => {

const handleChange = (event: ChangeEvent<HTMLSelectElement>) => {
onChange(event);
onSelectCountry(event.target.value);
};

const handleRetry = () => {
refreshCountries();
};

return (
<SelectInput
title="Country"
name="country"
required={true}
disabled={addressDisabled}
options={countries}
onChange={handleChange}
onBlur={onBlur}
value={value}
error={error}
retry={error && canRetry ? handleRetry : undefined}
/>
);
};

export default CountrySelect;
50 changes: 50 additions & 0 deletions src/common/components/addressForm/regionSelect/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import React, { ChangeEvent, FocusEvent } from 'react';
import SelectInput, { Option } from '../../form/SelectInput';

interface RegionSelectProps {
addressDisabled: boolean,
regions: Option[],
onChange: (event: ChangeEvent<HTMLSelectElement>) => void,
onBlur: (event: FocusEvent<HTMLSelectElement>) => void,
refreshRegions: () => void,
value: string,
error?: string,
canRetry: boolean,
}

const RegionSelect = ({
addressDisabled,
regions,
onChange,
onBlur,
refreshRegions,
value,
error,
canRetry,
}: RegionSelectProps) => {

const handleChange = (event: ChangeEvent<HTMLSelectElement>) => {
onChange(event);
};

const handleRetry = () => {
refreshRegions();
};

return (
<SelectInput
title="State"
name="region"
required={true}
disabled={addressDisabled}
options={regions}
onChange={handleChange}
onBlur={onBlur}
value={value}
error={error}
retry={error && canRetry ? handleRetry : undefined}
/>
);
};

export default RegionSelect;
74 changes: 74 additions & 0 deletions src/common/components/form/selectInput/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import React, { ChangeEvent, FocusEvent } from 'react';

interface SelectInputProps {
title?: string,
name: string,
required?: boolean,
disabled?: boolean,
options: Option[],
onChange: (value: ChangeEvent<HTMLSelectElement>) => void,
onBlur: (value: FocusEvent<HTMLSelectElement>) => void,
value: string,
error?: string,
retry?: () => void,
}

export interface Option {
name: string,
displayName: string
}

const SelectInput = ({
title,
name,
required,
disabled,
options,
onChange,
onBlur,
value,
error,
retry
}: SelectInputProps) => {
const optionRow = ({ name, displayName }: Option) => (
<option value={name} label={displayName} />
);

return (
<div className={`form-group${required && ' is-required' || ''}${error && ' has-error' || ''}`}>
{ title && <label>{title}</label> }
<div className="form-group">
<select
className="form-control form-control-subtle"
name={name}
required={required}
disabled={disabled}
onChange={onChange}
onBlur={onBlur}
value={value}
>
{ options.map(optionRow) }
</select>
{ error && (
<div role="alert">
<div className="help-block">
<span>{error}</span>
{ retry && (
<button
id="retryButton"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this work when there are multiples on the same page? Won't the ids clash? This might need to be overwrite-able.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, this might have to be more dynamic to account for multiple instances.

type="button"
className="btn btn-default btn-sm"
onClick={retry}
>
{'Retry'}
</button>
)}
</div>
</div>
)}
</div>
</div>
);
};

export default SelectInput;