Skip to content

Commit

Permalink
chore(react): support external state values
Browse files Browse the repository at this point in the history
  • Loading branch information
savindi7 committed Mar 14, 2023
1 parent 11e3eec commit af4720d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 22 deletions.
4 changes: 4 additions & 0 deletions packages/react/src/components/Image/Image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export type ImageProps = ImgHTMLAttributes<HTMLImageElement>;

const COMPONENT_NAME: string = 'Image';

/**
* TODO: Refer improvement issue if this Image component is required.
* @see {@link https://github.com/wso2/oxygen-ui/issues/65}
*/
const Image: FC<ImageProps> & WithWrapperProps = (props: ImageProps): ReactElement => {
const {className, alt, ...rest} = props;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ Import and use the `PhoneNumberInput` component in your components as follows.
code={dedent`
import PhoneNumberInput from '@oxygen-ui/react/PhoneNumberInput';\n
function Demo() {
return <PhoneNumberInput onChange={handlePhoneNumberInputChange} />;
const [dialCode, setDialCode] = useState('');
const [phoneNumber, setPhoneNumber] = useState('');\n
const handlePhoneNumberInputChange = (dialCode, phoneNumber) => {
setDialCode(dialCode);
setPhoneNumber(phoneNumber);
};\n
return (
<PhoneNumberInput
dialCodeValue={dialCode}
phoneNumberValue={phoneNumber}
onChange={handlePhoneNumberInputChange}
/>
);
}`}
/>
61 changes: 40 additions & 21 deletions packages/react/src/components/PhoneNumberInput/PhoneNumberInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,21 @@ export interface PhoneNumberInputProps extends BoxProps {
*/
SelectProps?: Omit<MuiSelectProps, 'labelId' | 'id' | 'value' | 'onChange' | 'placeholder'>;
/**
* Default country selected for the dialCode.
* Dial code state value.
*
* @example {code: 'US', dialCode: '+1', name: 'United States'}
* @example '+94'
*/
defaultCountry?: Country;
dialCodeValue?: string;
/**
* Callback function to be called when the dialCode or phoneNumber changes.
*/
onChange?: (dialCode: string, phoneNumber: string) => void;
/**
* Phone number state value.
*
* @example '787878787'
*/
phoneNumberValue?: string;
/**
* Placeholder text for the phone number input.
*/
Expand All @@ -67,29 +73,49 @@ const PhoneNumberInput: ForwardRefExoticComponent<PhoneNumberInputProps> & WithW
(props: PhoneNumberInputProps, ref: MutableRefObject<HTMLDivElement>): ReactElement => {
const {
className,
defaultCountry,
dialCodeValue,
label,
InputLabelProps,
OutlinedInputProps,
onChange,
phoneNumberValue,
placeholder,
SelectProps,
...rest
} = props;

const classes: string = clsx('oxygen-phone-number-input', className);

const [country, setCountry] = useState<Country>(defaultCountry ?? countries[0]);
const [phoneNumber, setPhoneNumber] = useState<string>('');
const [dialCode, setDialCode] = useState<string>(dialCodeValue ?? countries[0].dialCode);

const [phoneNumber, setPhoneNumber] = useState<string>(phoneNumberValue ?? '');

const handleDialCodeChange = (event: SelectChangeEvent): void => {
setCountry(countries.find((item: Country) => item.dialCode === event.target.value));
setDialCode(event.target.value);
onChange?.(event.target.value, phoneNumber);
};

const handlePhoneNumberChange = (event: ChangeEvent<HTMLInputElement>): void => {
setPhoneNumber(event.target.value);
onChange?.(country.dialCode, event.target.value);
onChange?.(dialCode, event.target.value);
};

const renderValue = (value: string): ReactElement => {
const selectedCountry: Country = countries.find((item: Country) => item.dialCode === dialCode);

return (
<>
<ListItemIcon>
<Flag
className="oxygen-image"
alt={selectedCountry.name}
code={selectedCountry.code}
fallback={<FlagOutlined />}
/>
</ListItemIcon>
{value}
</>
);
};

return (
Expand All @@ -102,30 +128,23 @@ const PhoneNumberInput: ForwardRefExoticComponent<PhoneNumberInputProps> & WithW
className="oxygen-select"
labelId="phone-number-label"
id="phone-number-select"
value={country.dialCode}
value={dialCode}
onChange={handleDialCodeChange}
renderValue={(value: string): ReactElement => (
<>
<ListItemIcon>
<Flag className="oxygen-image" alt={country.name} code={country.code} fallback={<FlagOutlined />} />
</ListItemIcon>
{value}
</>
)}
renderValue={renderValue}
inputProps={{
className: 'oxygen-select-input-root',
}}
{...SelectProps}
>
{countries?.map((countryItem: Country) => {
const {dialCode, code, name} = countryItem;
const {dialCode: phoneCode, code, name} = countryItem;
return (
<MenuItem value={dialCode} key={code} className="oxygen-dial-code-menu-item">
<MenuItem value={phoneCode} key={code} className="oxygen-dial-code-menu-item">
<ListItemIcon>
<Flag className="oxygen-image" alt={country.name} code={code} fallback={<FlagOutlined />} />
<Flag className="oxygen-image" alt={name} code={code} fallback={<FlagOutlined />} />
</ListItemIcon>
<Typography>{name}</Typography>&nbsp;
<Typography variant="body2">{dialCode}</Typography>
<Typography variant="body2">{phoneCode}</Typography>
</MenuItem>
);
})}
Expand Down

0 comments on commit af4720d

Please sign in to comment.