From 03309f6eab74142260f96df2db98cc383b97e914 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20D=C3=ADaz=20Gonz=C3=A1lez?= Date: Tue, 20 Feb 2024 09:54:44 +0000 Subject: [PATCH] [web] Add missing file test --- web/src/components/core/Selector.jsx.comp | 107 ++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 web/src/components/core/Selector.jsx.comp diff --git a/web/src/components/core/Selector.jsx.comp b/web/src/components/core/Selector.jsx.comp new file mode 100644 index 0000000000..7c0507043d --- /dev/null +++ b/web/src/components/core/Selector.jsx.comp @@ -0,0 +1,107 @@ +/* + * Copyright (c) [2024] SUSE LLC + * + * All Rights Reserved. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, contact SUSE LLC. + * + * To contact SUSE LLC about this file by physical or electronic mail, you may + * find current contact information at www.suse.com. + */ + +// @ts-check + +import React from "react"; +import { + useContextProps, + CheckboxContext, + GridList, GridListContext, + GridListItem +} from "react-aria-components"; +import { useToggleState } from 'react-stately'; +import { useCheckbox } from 'react-aria'; + +const MyCustomCheckbox = React.forwardRef((props, ref) => { + // Merge the local props and ref with the ones provided via context. + [props, ref] = useContextProps(props, ref, CheckboxContext); + const state = useToggleState(props); + const { inputProps } = useCheckbox(props, state, ref); + return ; +}); + +// // TODO: Using "slot-based" components would help to simplify this. +// const SelectionComponent = ({ allowsSelection, selectionMode, isSelected, isDisabled }) => { +// // NOTE: using a noop onChange callback to avoid a React complaint about +// // providing a `checked` prop to a form field without `onChange` handler. +// // Setting the proposed readOnly prop adds the `readonly` attribute to the +// // DOM element, which does not apply for checkboxes. +// // +// // https://html.spec.whatwg.org/#attr-input-readonly +// // https://github.com/facebook/react/blob/60a927d04ad3888facebcdf7da620aa1cfc9528f/packages/react-dom-bindings/src/shared/ReactControlledValuePropTypes.js#L50-L66 +// // https://github.com/facebook/react/blob/60a927d04ad3888facebcdf7da620aa1cfc9528f/packages/react-dom/src/__tests__/ReactDOMInput-test.js#L150-L167 +// const type = selectionMode === "multiple" ? "checkbox" : "radio"; +// +// return ( +// {}} +// /> +// ); +// }; +// +const Option = React.forwardRef((props, ref) => { + const state = React.useContext(GridListContext); + console.log("option", props, state); + [props, ref] = useContextProps(props, ref, GridListContext); + const { children } = props; + return ( + + + { children } + + ); +}); + +/** + * Renders children into an HTML section + */ +const Selector = ({ selected, onChange, allowsSelection = false, selectionMode = "multiple", disabledBehavior = "selection", children, ...listProps }) => { + const isMultiple = selectionMode === "multiple"; + const selectedKeys = selected ? Array(selected).flat() : []; + + const onSelectionChange = (selection) => { + const selected = Array.from(selection); + console.log("selected", selection, selected); + const nextSelection = isMultiple ? selected : selected[0]; + typeof onChange === "function" && onChange(nextSelection); + }; + + return ( + + { children } + + ); +}; + +Selector.Option = Option; +export default Selector;